Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6bda5a9348ec0093863996ee5f0ee86a724914a7 (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
«REM»
/*******************************************************************************
 * 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)
 * 
 *******************************************************************************/
«ENDREM»
«IMPORT room»;
«IMPORT etricegen»;

«EXTENSION extensions::Extensions»
«EXTENSION org::eclipse::xtend::util::stdlib::io»

«DEFINE protocolClass FOR Root»

«FOREACH this.usedProtocolClasses AS pc»

«("generating "+getPath(pc)+pc.name+".java").info()»

«FILE getPath(pc)+pc.name+".java"»
package «getPackage(pc)»;

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;

«EXPAND ProcedureHelpers::UserCode FOR pc.userCode1»

«LET this.getReferencedModels(pc) AS models»
«FOREACH models AS model»import «model.name».*;
«ENDFOREACH»
«ENDLET»

public class «pc.name» {
	// 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
	«FOREACH pc.getAllOutgoingMessages() AS message ITERATOR iter»public static final int OUT_«message.name» = «iter.counter1»;
	«ENDFOREACH»
	//IDs for incoming messages
	«FOREACH pc.getAllIncomingMessages() AS message ITERATOR iter»public static final int IN_«message.name» = «iter.counter1+pc.getAllOutgoingMessages().size»;
	«ENDFOREACH»
	//error if msgID >= MSG_MAX
	public static final int MSG_MAX = «pc.getAllOutgoingMessages().size + pc.getAllIncomingMessages().size+1»;  

	«EXPAND ProcedureHelpers::UserCode FOR pc.userCode2»

	private static String messageStrings[] = {"MIN", «FOREACH pc.getAllOutgoingMessages() AS m»"«m.name»",«ENDFOREACH» «FOREACH pc.getAllIncomingMessages() AS m»"«m.name»",«ENDFOREACH»"MAX"};

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

	«EXPAND portClass(false) FOR pc»
	«EXPAND portClass(true) FOR pc»
}
«ENDFILE»
«ENDFOREACH»
«ENDDEFINE»

«DEFINE portClass(Boolean conj) FOR ProtocolClass»

«LET getPortClassName(this, conj) AS name»
«LET this.getPortClass(conj) AS pclass»

// port class
static public class «name» extends PortBase {
	«IF pclass!=null»«EXPAND ProcedureHelpers::UserCode FOR pclass.userCode-»
	«ENDIF-»
	// constructors
	public «name»(IEventReceiver actor, String name, int localId, Address addr, Address peerAddress) {
		super(actor, name, localId, 0, addr, peerAddress);
		DebuggingService.getInstance().addPortInstance(this);
	}
	public «name»(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()]);
				}
				«IF this.handlesReceive(conj)»
				switch (msg.getEvtId()) {
				«FOREACH this.getReceiveHandlers(conj) AS hdlr»
				case «hdlr.msg.getCodeName()»:
				{
					«FOREACH hdlr.detailCode.commands AS command»	«command»
					«ENDFOREACH»
				}
				break;
				«ENDFOREACH»
				default:
				«ENDIF»
					if (msg instanceof EventWithDataMessage)
						getActor().receiveEvent(this, msg.getEvtId(), ((EventWithDataMessage)msg).getData());
					else
						getActor().receiveEvent(this, msg.getEvtId());
				«IF this.handlesReceive(conj)»
				}«ENDIF»
			}
	}

	«EXPAND ProcedureHelpers::Attributes FOR pclass.attributes-»
	«EXPAND ProcedureHelpers::Operations FOR pclass.operations-»
	
	// sent messages
	«FOREACH this.getOutgoing(conj) AS m»«EXPAND sendMessage(conj) FOR m»«ENDFOREACH-»
}

// replicated port class
static public class «name»Repl {
	private ArrayList<«name»> ports;
	private int replication;

	public «name»Repl(IEventReceiver actor, String name, int localId, Address[] addr,
			Address[] peerAddress) {
		replication = addr.length;
		ports = new ArrayList<«this.name».«name»>(replication);
		for (int i=0; i<replication; ++i) {
			ports.add(new «name»(
					actor, name+i, localId, i, addr[i], peerAddress[i]));
		}
	}
	
	public int getReplication() {
		return replication;
	}
	
	public «name» get(int i) {
		return ports.get(i);
	}
	
	«IF conj»
	// incoming messages
	«FOREACH this.getAllIncomingMessages() AS m-»
	«EXPAND messageSignature FOR m»{
		for (int i=0; i<replication; ++i) {
			ports.get(i).«EXPAND messageCall FOR m»;
		}
	}
	«ENDFOREACH-»
	«ELSE-»
	// outgoing messages
	«FOREACH this.getAllOutgoingMessages() AS m-»
	«EXPAND messageSignature FOR m»{
		for (int i=0; i<replication; ++i) {
			ports.get(i).«EXPAND messageCall FOR m»;
		}
	}
	«ENDFOREACH-»
	«ENDIF-»
}

// interface for port class
public interface I«name»{
	«IF conj-»
	// outgoing messages
	«FOREACH this.getAllOutgoingMessages() AS m» «EXPAND messageSignature FOR m»;
	«ENDFOREACH-»
	«ELSE-»
	// incoming messages
	«FOREACH this.getAllIncomingMessages() AS m» «EXPAND messageSignature FOR m»;
	«ENDFOREACH-»
	«ENDIF-»
}

«ENDLET»
«ENDLET»
«ENDDEFINE»

«DEFINE messageSignature FOR Message»public void «this.name» («FOREACH this.arguments AS a SEPARATOR "," »«a.type.typeName()» «a.name»«ENDFOREACH»)«ENDDEFINE»
«DEFINE messageCall FOR Message»«this.name»(«FOREACH this.arguments AS a SEPARATOR ","» «a.name»«ENDFOREACH»)«ENDDEFINE»
«DEFINE sendMessage(boolean conj) FOR Message-»
«LET conj?"IN":"OUT" AS dir-»
«LET this.getSendHandler(conj) AS hdlr-»
	«EXPAND messageSignature FOR this»{
		«IF hdlr!=null-»
			«FOREACH hdlr.detailCode.commands AS command»	«command»
			«ENDFOREACH-»
		«ELSE-»
			if (messageStrings[ «dir»_«this.name»] != "timerTick"){
				// TODOTS: model switch for activation
			DebuggingService.getInstance().addMessageAsyncOut(getAddress(), getPeerAddress(), messageStrings[«dir»_«this.name»]);
			}
			if (getPeerAddress()!=null)
			«IF this.arguments.isEmpty»getPeerMsgReceiver().receive(new EventMessage(getPeerAddress(), «dir»_«this.name»));
			«ELSE» getPeerMsgReceiver().receive(new EventWithDataMessage(getPeerAddress(), «dir»_«this.name», new Object[]{«FOREACH this.arguments AS a SEPARATOR ","» «a.name»«ENDFOREACH»}));
			«ENDIF-»
		«ENDIF-»
	}
«ENDLET-»
«ENDLET-»
«ENDDEFINE»

Back to the top