Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5ea196049a30b5910926c45e5cbefc9c84642212 (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
package SendingData;

import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver;
import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase;
import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase;
import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase;
import org.eclipse.etrice.runtime.java.debugging.DebuggingService;


import SendingData.PingPongProtocol.*;



public class MrPing extends ActorClassBase {

	
	//--------------------- ports
	protected PingPongProtocolConjPort PingPongPort = null;
	//--------------------- saps
	//--------------------- services

	//--------------------- interface item IDs
	protected static final int IFITEM_PingPongPort = 1;
	
	//--------------------- attributes
	//--------------------- operations
	public void printData (DemoData d){
			System.out.printf("d.int32Val: %d\n",d.int32Val);
		System.out.printf("d.float64Val: %f\n",d.float64Val);
		System.out.printf("d.int8Array: ");
		for(int i = 0; i<d.int8Array.length; i++) {
		System.out.printf("%d ",d.int8Array[i]);}
		System.out.printf("\nd.stringVal: %s\n",d.stringVal);
	}

	//--------------------- construction
	public MrPing(IRTObject parent, String name, Address[][] port_addr, Address[][] peer_addr){
		super(parent, name, port_addr[0][0], peer_addr[0][0]);
		setClassName("MrPing");
		
		// initialize attributes

		// own ports
		PingPongPort = new PingPongProtocolConjPort(this, "PingPongPort", IFITEM_PingPongPort, 0, port_addr[IFITEM_PingPongPort][0], peer_addr[IFITEM_PingPongPort][0]); 
		// own saps
		// own service implementations
	}
	

	//--------------------- lifecycle functions
	public void init(){
		initUser();
	}

	public void start(){
		startUser();
	}

	public void stop(){
		stopUser();
	}
	
	public void destroy(){
		destroyUser();
	}	

	//******************************************
	// START of generated code for FSM
	//******************************************
	
	// State IDs for FSM
	protected static final int STATE_waitForPong = 2;
	protected static final int STATE_waitForPongSimple = 3;
		protected static final String stateStrings[] = {"<no state>","<top>","waitForPong",
			"waitForPongSimple"
			};
	
	// history
	// TODOHRR: history defined in ActorClassBase, init in constructor
	//		history = new int[5];
	//		for (int i = 0; i < history.length; i++) {
	//			history[i] = NO_STATE;
	//		}
	protected int history[] = {NO_STATE,NO_STATE,NO_STATE,NO_STATE};
	
	// transition chains
	protected static final int CHAIN_TRANS_INITIAL_TO__waitForPongSimple = 1;
	protected static final int CHAIN_TRANS_waitForPongSimple_TO_waitForPongSimple_BY_pongSimplePingPongPort_wait1 = 2;
	protected static final int CHAIN_TRANS_waitForPongSimple_TO_waitForPong_BY_pongSimplePingPongPort = 3;
	protected static final int CHAIN_TRANS_waitForPong_TO_waitForPong_BY_pongPingPongPort_wait2 = 4;
	
	// triggers for FSM
	protected static final int TRIG_PingPongPort__pong = IFITEM_PingPongPort + EVT_SHIFT*PingPongProtocol.OUT_pong;
	protected static final int TRIG_PingPongPort__pongSimple = IFITEM_PingPongPort + EVT_SHIFT*PingPongProtocol.OUT_pongSimple;
	
	// receiveEvent contains the main implementation of the FSM
	@Override
	public void receiveEvent(InterfaceItemBase ifitem, int evt, Object generic_data) {
		int trigger = ifitem.getLocalId() + EVT_SHIFT*evt;
		int chain = NOT_CAUGHT;
		int catching_state = NO_STATE;
		boolean is_handler = false;
		boolean skip_entry = false;
		
		if (!handleSystemEvent(ifitem, evt, generic_data)) {
			switch (state) {
				case STATE_waitForPong:
					switch(trigger) {
					case TRIG_PingPongPort__pong:
						{
							chain = CHAIN_TRANS_waitForPong_TO_waitForPong_BY_pongPingPongPort_wait2;
							catching_state = STATE_TOP;
						}
					break;
					}
					break;
				case STATE_waitForPongSimple:
					switch(trigger) {
					case TRIG_PingPongPort__pongSimple:
						{ int data = (Integer) generic_data;
							if (data < 10
							)
						{
							chain = CHAIN_TRANS_waitForPongSimple_TO_waitForPongSimple_BY_pongSimplePingPongPort_wait1;
							catching_state = STATE_TOP;
						} else 
						{
							chain = CHAIN_TRANS_waitForPongSimple_TO_waitForPong_BY_pongSimplePingPongPort;
							catching_state = STATE_TOP;
						}
						}
					break;
					}
					break;
			}
		}
		if (chain != NOT_CAUGHT) {
			exitTo(state, catching_state, is_handler);
			int next = executeTransitionChain(chain, ifitem, generic_data);
			next = enterHistory(next, is_handler, skip_entry);
			setState(next);
		}
	}
	
	private void setState(int new_state) {
		DebuggingService.getInstance().addActorState(this,stateStrings[new_state]);
		if (stateStrings[new_state]!="Idle") {
			// TODOTS: model switch for activation
			System.out.println(getInstancePath() + " -> " + stateStrings[new_state]);
		}	
		this.state = new_state;
	}
	
	@Override
	public void executeInitTransition() {
		int chain = CHAIN_TRANS_INITIAL_TO__waitForPongSimple;
		int next = executeTransitionChain(chain, null, null);
		next = enterHistory(next, false, false);
		setState(next);
	}
	
	/**
	 * calls exit codes while exiting from the current state to one of its
	 * parent states while remembering the history
	 * @param current - the current state
	 * @param to - the final parent state
	 * @param handler - entry and exit codes are called only if not handler (for handler TransitionPoints)
	 */
	private void exitTo(int current, int to, boolean handler) {
		while (current!=to) {
			switch (current) {
				case STATE_waitForPong:
					history[STATE_TOP] = STATE_waitForPong;
					current = STATE_TOP;
					break;
				case STATE_waitForPongSimple:
					history[STATE_TOP] = STATE_waitForPongSimple;
					current = STATE_TOP;
					break;
			}
		}
	}
	/**
	 * calls action, entry and exit codes along a transition chain. The generic data are cast to typed data
	 * matching the trigger of this chain. The ID of the final state is returned
	 * @param chain - the chain ID
	 * @param generic_data - the generic data pointer
	 * @return the ID of the final state
	 */
	private int executeTransitionChain(int chain, InterfaceItemBase ifitem, Object generic_data) {
		switch (chain) {
			case CHAIN_TRANS_waitForPong_TO_waitForPong_BY_pongPingPongPort_wait2:
			{
				DemoData data = (DemoData) generic_data;
				action_TRANS_waitForPong_TO_waitForPong_BY_pongPingPongPort_wait2(ifitem, data);
				return STATE_waitForPong;
			}
			case CHAIN_TRANS_waitForPongSimple_TO_waitForPongSimple_BY_pongSimplePingPongPort_wait1:
			{
				int data = (Integer) generic_data;
				action_TRANS_waitForPongSimple_TO_waitForPongSimple_BY_pongSimplePingPongPort_wait1(ifitem, data);
				return STATE_waitForPongSimple;
			}
			case CHAIN_TRANS_waitForPongSimple_TO_waitForPong_BY_pongSimplePingPongPort:
			{
				int data = (Integer) generic_data;
				action_TRANS_waitForPongSimple_TO_waitForPong_BY_pongSimplePingPongPort(ifitem, data);
				return STATE_waitForPong;
			}
			case CHAIN_TRANS_INITIAL_TO__waitForPongSimple:
			{
				action_TRANS_INITIAL_TO__waitForPongSimple();
				return STATE_waitForPongSimple;
			}
		}
		return NO_STATE;
	}
	/**
	 * calls entry codes while entering a state's history. The ID of the final leaf state is returned
	 * @param state - the state which is entered
	 * @param handler - entry code is executed if not handler
	 * @return - the ID of the final leaf state
	 */
	private int enterHistory(int state, boolean handler, boolean skip_entry) {
		while (true) {
			switch (state) {
				case STATE_waitForPong:
					// in leaf state: return state id
					return STATE_waitForPong;
				case STATE_waitForPongSimple:
					// in leaf state: return state id
					return STATE_waitForPongSimple;
				case STATE_TOP:
					state = history[STATE_TOP];
					break;
			}
			skip_entry = false;
		}
		//return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true)
	}
	
	//*** Entry and Exit Codes
	
	//*** Action Codes
	protected void action_TRANS_waitForPong_TO_waitForPong_BY_pongPingPongPort_wait2(InterfaceItemBase ifitem, DemoData data) {
		printData(data);
		}
	protected void action_TRANS_waitForPongSimple_TO_waitForPongSimple_BY_pongSimplePingPongPort_wait1(InterfaceItemBase ifitem, int data) {
		// keep in mind that MrPong increments
		PingPongPort.pingSimple(data);
		System.out.printf("data: %d\n",data);
		}
	protected void action_TRANS_waitForPongSimple_TO_waitForPong_BY_pongSimplePingPongPort(InterfaceItemBase ifitem, int data) {
		System.out.printf("data: %d\n",data);
		
		DemoData d = new DemoData();
		// send the default values
		PingPongPort.ping(d);
		d.int32Val=815;
		for (int i = 0; i<d.int8Array.length;i++){
			d.int8Array[i]=(byte)(i+100);
			}
		d.stringVal="some contents";
		d.float64Val=3.141234;
		PingPongPort.ping(d);
		}
	protected void action_TRANS_INITIAL_TO__waitForPongSimple() {
		PingPongPort.pingSimple(0);
		}
		 
	//******************************************
	// END of generated code for FSM
	//******************************************
};

Back to the top