Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a160e66a9a7c9148eaa107f7d1380b5c8aaedb2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/


package org.eclipse.etrice.runtime.java.messaging;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;


/**
 * The MessageService is the backbone of the asynchronous communication inside a SubSystem
 * It usually contains a thread a message queue and a dispatcher
 * 
 * @author Thomas Schuetz (initial contribution)
 * @author Henrik Rentz-Reichert (extending RTObject, implementing Runnable)
 *
 */
public class MessageService extends AbstractMessageService {

	private boolean running = false;
	
	private Thread thread;
	private int priority;
	private long lastMessageTimestamp;
	
	private int pollingInterval = -1;
	private ScheduledExecutorService pollingScheduler = null;

	public MessageService(IRTObject parent, ExecMode mode, int node, int thread, String name) {
		this(parent, mode, 0, node, thread, name, Thread.NORM_PRIORITY);
	}
	
	public MessageService(IRTObject parent, ExecMode mode, int nsec, int node, int thread, String name) {
		this(parent, mode, nsec, node, thread, name, Thread.NORM_PRIORITY);
	}
	
	public MessageService(IRTObject parent, ExecMode mode, int nsec, int node, int thread, String name, int priority) {
		super(parent, "MessageService_"+name, node, thread);

		// Java thread priority is limited to 1-10 and cannot be changed
		// thus priorities from physical mapping are not generated
		// priority = Thread.NORM_PRIORITY generated fixed
		this.priority = priority;

		assert priority >= Thread.MIN_PRIORITY : ("priority smaller than Thread.MIN_PRIORITY (" + "Thread.MIN_PRIORITY" + ")"); 
		assert priority <= Thread.MAX_PRIORITY : ("priority bigger than Thread.MAX_PRIORITY (" + "Thread.MAX_PRIORITY" + ")"); 
		
		if(mode == ExecMode.MIXED || mode == ExecMode.POLLED){
			pollingInterval = nsec;
			pollingScheduler = Executors.newScheduledThreadPool(1, new PollingThreadFactory());
			
			assert pollingInterval > 0 : ("polling interval is 0 or negative");
		}
	}

	public void run() {
		running = true;
		
		if(pollingScheduler != null)
			pollingScheduler.scheduleAtFixedRate(new PollingTask(), pollingInterval, pollingInterval, TimeUnit.NANOSECONDS);
		
		while (running) {
			Message msg = null;
			
			// get next Message from Queue
			synchronized(this) {
				msg = getMessageQueue().pop();
			}
			
			if (msg == null) {
				// no message in queue -> wait until Thread is notified
				try {
					synchronized(this) {
						if (!running)
							return;
						wait();
					}
				}
				catch (InterruptedException e) {
				}
			}
			else {
				// process message
				lastMessageTimestamp = System.currentTimeMillis();
				getMessageDispatcher().receive(msg);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.AbstractMessageService#receive(org.eclipse.etrice.runtime.java.messaging.Message)
	 */
	@Override
	public synchronized void receive(Message msg) {
		super.receive(msg);
		
		// wake up thread to process message
		notifyAll();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.AbstractMessageService#getFreeAddress()
	 */
	@Override
	public synchronized Address getFreeAddress() {
		return super.getFreeAddress();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.AbstractMessageService#addMessageReceiver(org.eclipse.etrice.runtime.java.messaging.IMessageReceiver)
	 */
	@Override
	public synchronized void addMessageReceiver(IMessageReceiver receiver) {
		super.addMessageReceiver(receiver);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.AbstractMessageService#removeMessageReceiver(org.eclipse.etrice.runtime.java.messaging.IMessageReceiver)
	 */
	@Override
	public synchronized void removeMessageReceiver(IMessageReceiver receiver) {
		super.removeMessageReceiver(receiver);
	}
	
	@Override
	public synchronized void addPollingMessageReceiver(IMessageReceiver receiver) {
		super.addPollingMessageReceiver(receiver);
	}
	
	@Override
	public synchronized void removePollingMessageReceiver(IMessageReceiver receiver) {
		super.removePollingMessageReceiver(receiver);
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.AbstractMessageService#freeAddress(org.eclipse.etrice.runtime.java.messaging.Address)
	 */
	@Override
	public synchronized void freeAddress(Address addr) {
		super.freeAddress(addr);
	}
	
	public synchronized void terminate() {
		if (running) {
			running = false;
			notifyAll();
		}
		
		if(pollingScheduler != null)
			pollingScheduler.shutdown();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.IMessageService#setThread(java.lang.Thread)
	 */
	public void setThread(Thread thread) {
		this.thread = thread;
		
		thread.setPriority(priority);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.etrice.runtime.java.messaging.IMessageService#getThread()
	 */
	public Thread getThread() {
		return thread;
	}

	protected long getLastMessageTimestamp() {
		return lastMessageTimestamp;
	}
	
	private class PollingTask implements Runnable{
		
		@Override
		public void run() {
			if(running){
				Message msg = new Message(getMessageDispatcher().getAddress());
				receive(msg);
			} 
		}
		
	}
	
	private class PollingThreadFactory implements ThreadFactory{

		@Override
		public Thread newThread(Runnable arg0) {
			Thread thread = new Thread(arg0, getName()+"_PollingThread");
			thread.setPriority(priority);
			
			return thread;
		}
		
	}

}

Back to the top