Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1df8619e3a5ad9a1f4f84f4608bc7a23d96c4118 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package room.basic.service.timing;

import java.util.ArrayList;

import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.Message;
import org.eclipse.etrice.runtime.java.modelbase.*;
import org.eclipse.etrice.runtime.java.debugging.DebuggingService;

//--------------------- begin user code
	import java.util.TimerTask;
//--------------------- end user code


public class PTimeout {
	// message IDs
	// TODO: separate class for message IDs: class MSG{public static volatile int MSG_MIN = 0; ...} -> better structure
	// error if msgID <= MSG_MIN
	public static final int MSG_MIN = 0;   
	//IDs for outgoing messages
	public static final int OUT_timeoutTick = 1;
	//IDs for incoming messages
	public static final int IN_Start = 2;
	public static final int IN_Kill = 3;
	//error if msgID >= MSG_MAX
	public static final int MSG_MAX = 4;  

	//--------------------- begin user code
		static protected class FireTimeoutTask extends TimerTask {
		
					private int time;
					private int id;
					private PTimeoutPort port;
		
					public FireTimeoutTask(int time, int id, PTimeoutPort port) {
						this.time = time;
						this.id = id;
						this.port = port;
					}
		
					@Override
					public void run() {
						port.timeout(id);
					}
					
					public int getTime() {
						return time;
					}
					
					public int getId() {
						return id;
					}
				}
				
				static protected class TimeoutData {
					int time;
					int id;
					public TimeoutData(int time, int id) {
						this.time = time;
						this.id = id;
					}
				}
	//--------------------- end user code

	private static String messageStrings[] = {"MIN", "timeoutTick", "Start","Kill","MAX"};

	public String getMessageString(int msg_id) {
		if (msg_id<MSG_MIN || msg_id>MSG_MAX+1){
			// id out of range
			return "Message ID out of range";
		}
		else{
			return messageStrings[msg_id];
		}
	}

	
	// port class
	static public class PTimeoutPort extends PortBase {
		//--------------------- begin user code
			private FireTimeoutTask task = null;
						
						public TimerTask getTask() { return task; }
		//--------------------- end user code
		// constructors
		public PTimeoutPort(IEventReceiver actor, String name, int localId, Address addr, Address peerAddress) {
			super(actor, name, localId, 0, addr, peerAddress);
			DebuggingService.getInstance().addPortInstance(this);
		}
		public PTimeoutPort(IEventReceiver actor, String name, int localId, int idx, Address addr, Address peerAddress) {
			super(actor, name, localId, idx, addr, peerAddress);
			DebuggingService.getInstance().addPortInstance(this);
		}
	
		@Override
		public void receive(Message m) {
				if (!(m instanceof EventMessage))
					return;
				EventMessage msg = (EventMessage) m;
				if (msg.getEvtId() <= 0 || msg.getEvtId() >= MSG_MAX)
					System.out.println("unknown");
				else {
					if (messageStrings[msg.getEvtId()] != "timerTick"){
						// TODOTS: model switch for activation
						DebuggingService.getInstance().addMessageAsyncIn(getPeerAddress(), getAddress(), messageStrings[msg.getEvtId()]);
					}
					switch (msg.getEvtId()) {
						case IN_Start:
						{
							//regular PortClass handle start
											EventWithDataMessage dataMsg = (EventWithDataMessage) msg;
											TimeoutData td = (TimeoutData)dataMsg.getData();
											task = new FireTimeoutTask(td.time, td.id, this);
											getActor().receiveEvent(this, IN_Start, td.time);
						}
						break;
						case IN_Kill:
						{
							//regular PortClass handle kill
											EventWithDataMessage dataMsg = (EventWithDataMessage) msg;
											int id = (Integer)dataMsg.getData();
											if (task!=null && task.getId()==id) {
												task.cancel();
											}
						}
						break;
						default:
						if (msg instanceof EventWithDataMessage)
							getActor().receiveEvent(this, msg.getEvtId(), ((EventWithDataMessage)msg).getData());
						else
							getActor().receiveEvent(this, msg.getEvtId(), null);
					}
				}
		}
	
		//--------------------- attributes
		//--------------------- operations
		public void timeout(Integer id) {
			//regular PortClass Operation timeout
						DebuggingService.getInstance().addMessageAsyncOut(getAddress(),
								getPeerAddress(), messageStrings[OUT_timeoutTick]);
			
						getPeerMsgReceiver().receive(
								new EventWithDataMessage(getPeerAddress(), OUT_timeoutTick, id));
		}
		
		// sent messages
		public void timeoutTick() {
			if (messageStrings[ OUT_timeoutTick] != "timerTick"){
				// TODOTS: model switch for activation
			DebuggingService.getInstance().addMessageAsyncOut(getAddress(), getPeerAddress(), messageStrings[OUT_timeoutTick]);
			}
			if (getPeerAddress()!=null)
				getPeerMsgReceiver().receive(new EventMessage(getPeerAddress(), OUT_timeoutTick));
				}
	}
	
	// replicated port class
	static public class PTimeoutPortRepl {
		private ArrayList<PTimeoutPort> ports;
		private int replication;
	
		public PTimeoutPortRepl(IEventReceiver actor, String name, int localId, Address[] addr,
				Address[] peerAddress) {
			replication = addr.length;
			ports = new ArrayList<PTimeout.PTimeoutPort>(replication);
			for (int i=0; i<replication; ++i) {
				ports.add(new PTimeoutPort(
						actor, name+i, localId, i, addr[i], peerAddress[i]));
			}
		}
		
		public int getReplication() {
			return replication;
		}
		
		public int getIndexOf(InterfaceItemBase ifitem){
				return ifitem.getIdx();
			}
		
		public PTimeoutPort get(int i) {
			return ports.get(i);
		}
		
		// outgoing messages
		public void timeoutTick(){
			for (int i=0; i<replication; ++i) {
				ports.get(i).timeoutTick();
			}
		}
	}
	
	
	// port class
	static public class PTimeoutConjPort extends PortBase {
		//--------------------- begin user code
			private int currentId = 0;
						private boolean active = false;
		//--------------------- end user code
		// constructors
		public PTimeoutConjPort(IEventReceiver actor, String name, int localId, Address addr, Address peerAddress) {
			super(actor, name, localId, 0, addr, peerAddress);
			DebuggingService.getInstance().addPortInstance(this);
		}
		public PTimeoutConjPort(IEventReceiver actor, String name, int localId, int idx, Address addr, Address peerAddress) {
			super(actor, name, localId, idx, addr, peerAddress);
			DebuggingService.getInstance().addPortInstance(this);
		}
	
		@Override
		public void receive(Message m) {
				if (!(m instanceof EventMessage))
					return;
				EventMessage msg = (EventMessage) m;
				if (msg.getEvtId() <= 0 || msg.getEvtId() >= MSG_MAX)
					System.out.println("unknown");
				else {
					if (messageStrings[msg.getEvtId()] != "timerTick"){
						// TODOTS: model switch for activation
						DebuggingService.getInstance().addMessageAsyncIn(getPeerAddress(), getAddress(), messageStrings[msg.getEvtId()]);
					}
					switch (msg.getEvtId()) {
						case OUT_timeoutTick:
						{
							//conjugate PortClass handle timeout
											EventWithDataMessage dataMsg = (EventWithDataMessage) msg;
											int id = (Integer)dataMsg.getData();
											if (active && id==currentId) {
												active = false;
												getActor().receiveEvent(this, msg.getEvtId(), null);
											}
						}
						break;
						default:
						if (msg instanceof EventWithDataMessage)
							getActor().receiveEvent(this, msg.getEvtId(), ((EventWithDataMessage)msg).getData());
						else
							getActor().receiveEvent(this, msg.getEvtId(), null);
					}
				}
		}
	
		//--------------------- attributes
		//--------------------- operations
		
		// sent messages
		public void Start(int time_ms) {
				//conjugate PortClass handle start
							if (active)
								return;
								
							active = true;
							DebuggingService.getInstance().addMessageAsyncOut(getAddress(),
									getPeerAddress(), messageStrings[IN_Start]);
				
							getPeerMsgReceiver()
									.receive(
											new EventWithDataMessage(getPeerAddress(),
													IN_Start, new TimeoutData(time_ms, ++currentId)));
		}
		public void Kill() {
				//conjugate PortClass kill
							DebuggingService.getInstance().addMessageAsyncOut(getAddress(),
									getPeerAddress(), messageStrings[IN_Kill]);
				
							if (active) {
								active = false;
								getPeerMsgReceiver().receive(
										new EventWithDataMessage(getPeerAddress(), IN_Kill, new Integer(currentId)));
							}
		}
	}
	
	// replicated port class
	static public class PTimeoutConjPortRepl {
		private ArrayList<PTimeoutConjPort> ports;
		private int replication;
	
		public PTimeoutConjPortRepl(IEventReceiver actor, String name, int localId, Address[] addr,
				Address[] peerAddress) {
			replication = addr.length;
			ports = new ArrayList<PTimeout.PTimeoutConjPort>(replication);
			for (int i=0; i<replication; ++i) {
				ports.add(new PTimeoutConjPort(
						actor, name+i, localId, i, addr[i], peerAddress[i]));
			}
		}
		
		public int getReplication() {
			return replication;
		}
		
		public int getIndexOf(InterfaceItemBase ifitem){
				return ifitem.getIdx();
			}
		
		public PTimeoutConjPort get(int i) {
			return ports.get(i);
		}
		
		// incoming messages
		public void Start(int time_ms){
			for (int i=0; i<replication; ++i) {
				ports.get(i).Start( time_ms);
			}
		}
		public void Kill(){
			for (int i=0; i<replication; ++i) {
				ports.get(i).Kill();
			}
		}
	}
	
}

Back to the top