Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dee0487b19cfbb44ffe81a89cd73839ac8350088 (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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*******************************************************************************
 * Copyright (c) 2013 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:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

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

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.modelbase.RTSystemProtocol.RTSystemConjPort;

/**
 * This is the abstract base class of all optional actor interfaces.
 * Concrete sub classes are {@link ScalarOptionalActorInterfaceBase} and {@link ReplicatedOptionalActorInterfaceBase}.
 * <p>
 * The code generator again derives from the concrete sub classes and adds {@link InterfaceItemBroker}s as members.
 * </p>
 * <p>
 * This generated class is instantiated as member of the containing actor (the one holding the associated optional
 * actor reference).
 * </p>
 * <p>
 * The broker items are responsible for the mediation of the port connections.
 * </p>
 * <p>
 * This interface represents a border in the instance tree. We call all direct and indirect children
 * the <i>interior</i> part of the instance tree and the remainder the <i>exterior</i> part.
 * In order to have natural path names of the interior we have to omit this interface's segment.
 * Because it is repeated by the instantiated optional actor. This is done by the overridden
 * {@link #getInstancePath(char)}. As a consequence we also have to override {@link #getObject(String)}.
 * This method turns the path into a relative one and then retrieves the object starting at this
 * instance.
 * </p>
 * 
 * @author Henrik Rentz-Reichert
 */
public abstract class OptionalActorInterfaceBase extends SystemPortOwner implements IEventReceiver {

	protected static final int IFITEM_RTSystemPort = 0;
	
	/**
	 * The name of the optional actor class. This and all sub classes of it are valid candidates
	 * for instantiation at this place.
	 */
	private String className;
	
	/**
	 * This map is set during optional actor creation by the factory.
	 */
	private PathToPeers path2peers = null;
	
	/**
	 * The path of this instance is used to determine correct mappings.
	 * It is subtracted from incoming paths, then (relative) peer paths are looked up and
	 * the result paths are turned into absolute ones by adding this again.
	 * 
	 * @see #getPeersForPath(String)
	 */
	private String parentPath;
	
	/**
	 * This is equivalent to {@link #parentPath}+{@link #getName()}
	 */
	private String ownPath;
	
	/**
	 * The thread (associated with the message service of this ID) that will be used by the
	 * optional actor instance.
	 */
	private int subtreeThread;
	
	/**
	 * This port is used to send system messages to the optional sub instance tree.
	 */
	private RTSystemConjPort RTSystemPort = null;
	
	/**
	 * The only constructor.
	 * 
	 * @param parent the containing {@link ActorClassBase}
	 * @param name the reference name
	 * @param clsname the class name of this reference
	 */
	protected OptionalActorInterfaceBase(IEventReceiver parent, String name, String clsname) {
		super(parent, name);
		className = clsname;
		subtreeThread = parent.getThread();
		parentPath = getInstancePath();
		ownPath = getInterfaceInstancePath();
		
		RTSystemPort = new RTSystemConjPort(this, IFITEM_RTSystemPort);
	}
	
	/**
	 * The regular instance path is changed here by omitting our segment.
	 * 
	 * @see org.eclipse.etrice.runtime.java.messaging.RTObject#getInstancePath(char)
	 */
	@Override
	public String getInstancePath(char delim) {
		// the parent is never null for optional actors
		return getParent().getInstancePath(delim);
	}
	
	/**
	 * This is our regular instance path including our own name as last segment.
	 * 
	 * @return
	 */
	public String getInterfaceInstancePath() {
		return super.getInstancePath(PATH_DELIM);
	}
	
	/**
	 * If the path points to our <i>interior</i> (which should be always the case)
	 * the we turn it into a relative one starting here.
	 * It is important that our own interface item brokers are treated specially.
	 * 
	 * @see org.eclipse.etrice.runtime.java.messaging.RTObject#getObject(java.lang.String)
	 */
	@Override
	public IRTObject getObject(String path) {
		if (path.startsWith(ownPath)) {
			int sep = path.indexOf(PATH_DELIM, parentPath.length()+1);
			if (sep>=0 && sep<path.length()) {
				// we turn the path into a relative one and resolve it starting at this instance
				
				// path segment of the optional actor
				String optInst = path.substring(parentPath.length(), sep);
				
				// remainder
				path = path.substring(sep);
				
				// if remainder contains more than one segment it points into the optional actor 
				if (path.indexOf(PATH_DELIM, 1)>=0)
					// we add the optional actor segment
					path = optInst+path;
				else {
					/* the port is our own port
					 * a) on the interface ==> it is a broker and direct child
					 * b) internal ==> it is a child of the optional actor
					 */
					IRTObject obj = super.getObject(getName()+path);
					if (obj==null) {
						obj = super.getObject(getName()+optInst+path);
					}
					return obj;
				}
				
				// finally we have to prefix with our own name since the relative path has to start with that
				path = getName()+path;
			}
		}
		
		return super.getObject(path);
	}
	
	/**
	 * Get list of peer paths.
	 * <p>
	 * This method delegates to its parent if {@link #getPath2peers()}{@code ==null}.
	 * </p>
	 * <p>
	 * If an optional actor instance is created the used factory will set
	 * its own path-to-peer mapping. Then the incoming path is made relative to
	 * the created actor and the paths looked up in the map are made absolute again.
	 * 
	 * @param path an absolute path
	 * @return a list of absolute peer paths or {@code null} if not mapped
	 */
	@Override
	public List<String> getPeersForPath(String path) {
		// if no mapping available we delegate to our parent (which is never null)
		if (getPath2peers()==null)
			return getParent().getPeersForPath(path);
		
		/*  remove parent path+1 up to next delimiter (thus including the optional ref name)
		 *  e.g.
		 *  incoming path = /LS/appl/cont/opt/path/to/port
		 *  rel path = /path/to/port
		 *  result of lookup (to interface port) = /port
		 *  returned = /LS/appl/cont/opt/port (one of the interface port brokers)
		 */
		int sep = path.indexOf(PATH_DELIM, parentPath.length()+1);
		if (sep<0 || sep>=path.length())
			return null;
		
		/* The optInstPath for scalar optional actors is parentPath/<name>.
		 * However, for replicated actors it is parentPath/<name>:<idx>
		 */
		String optInstPath = path.substring(0, sep);
		path = path.substring(sep);
		
		if (path.indexOf('/', 1)<0) {
			/* 
			 * This is an end port on the interface.
			 * It is directly mapped to its broker (there is no mapping for it)
			 */
			return Collections.singletonList(ownPath+path);
		}
		
		ArrayList<String> paths = getPath2peers().get(path);
		if (paths!=null) {
			ArrayList<String> result = new ArrayList<String>();
			for (String p : paths) {
				if (p.indexOf('/', 1)>=0)
					// it's a path nested in the optional instance
					p = optInstPath+p;
				else
					// its a path to one of my brokers (an immediate child)
					p = ownPath+p;
				result.add(p);
			}
			return result;
		}
		return paths;
	}
	
	/**
	 * Get thread for path. The thread is passed to the optional actor creation method.
	 * 
	 * @param path
	 * @return always the thread that was specified with the creation call
	 */
	@Override
	public int getThreadForPath(String path) {
		return subtreeThread;
	}

	/**
	 * This method is called by the optional actor factory to set the relative path mappings
	 * for the created instance sub tree
	 * 
	 * @param path2peers
	 */
	public void setPath2peers(PathToPeers path2peers) {
		this.path2peers = path2peers;
	}

	/**
	 * Returns the locally set path-to-peer mapping
	 * @return the path2peers
	 */
	protected PathToPeers getPath2peers() {
		return path2peers;
	}

	/**
	 * @return the class name for this optional actor
	 */
	public String getClassName() {
		return className;
	}

	/**
	 * {@code null} implementation since never called
	 * @see org.eclipse.etrice.runtime.java.modelbase.IEventReceiver#receiveEvent(org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase, int, java.lang.Object)
	 */
	@Override
	public void receiveEvent(InterfaceItemBase ifitem, int evt, Object data) {
		// nothing to do, never called
	}

	/**
	 * @return the thread for the optional actors to be created
	 */
	protected int getSubtreeThread() {
		return subtreeThread;
	}

	/**
	 * sets the thread for the optional actor to be created
	 * @param subtreeThread
	 */
	protected void setSubtreeThread(int subtreeThread) {
		this.subtreeThread = subtreeThread;
	}
	
	/**
	 * This method is responsible for the start-up part of the life cycle of the newly created
	 * instances:
	 * 
	 * <ul>
	 * <li>load from input (if not {@code null})</li>
	 * <li>recursively {@link ActorClassBase#init() initialize}</li>
	 * <li>send initialization messages</li>
	 * </ul>
	 * 
	 * @param actor the newly created actor (actually a whole tree)
	 * @param input an optional input source for the sub tree's data
	 */
	protected void startupSubTree(ActorClassBase actor, ObjectInput input) {
		if (input!=null)
			loadActor(actor, input);

		// recursive initialization
		actor.init();

		// send system messages for initialization (does nothing for persisted actors)
		RTSystemPort.executeInitialTransition();
	}
	
	/**
	 * This method is responsible for the shut-down part of the life cycle of the newly created
	 * instances:
	 * 
	 * <ul>
	 * <li>save to output (if not {@code null})</li>
	 * <li>recursively {@link ActorClassBase#stop() stop}</li>
	 * </ul>
	 * 
	 * @param actor
	 * @param output
	 */
	protected void shutdownSubTree(ActorClassBase actor, ObjectOutput output) {
		if (output!=null)
			saveActor(actor, output);
		
		// recursively stop
		actor.stop();
	}
	
	/**
	 * Other than the base class implementation this method doesn't delegate but
	 * returns our own {@link org.eclipse.etrice.runtime.java.modelbase.RTSystemProtocol.RTSystemPort RTSystemPort}.
	 * 
	 * @see org.eclipse.etrice.runtime.java.modelbase.SystemPortOwner#getSystemPort()
	 */
	@Override
	public IReplicatedInterfaceItem getSystemPort() {
		return RTSystemPort;
	}

	/**
	 * This is an empty implementation which can be overridden by the generator
	 * if MSC logging is enabled.
	 * 
	 * @param actorClass the name of the actor class to be instantiated
	 * @param name the name of the reference (eventually including an index for replicated actors)
	 */
	protected void logCreation(String actorClass, String name) {
		// empty implementation, may be overridden by sub class
	}

	/**
	 * This is an empty implementation which can be overridden by the generator
	 * if MSC logging is enabled.
	 * 
	 * @param name the name of the reference (eventually including an index for replicated actors)
	 */
	protected void logDeletion(String name) {
		// empty implementation, may be overridden by sub class
	}

	/**
	 * This method is called if an {@link ObjectInput} was passed to creation and after
	 * the actor instance sub tree is created and ports are bound.
	 * 
	 * <p>
	 * States, history and all attributes of the actor instances are restored.
	 * </p>
	 * 
	 * @param actor an actor class
	 * @param input the input stream
	 */
	protected void loadActor(ActorClassBase actor, ObjectInput input) {
		if (input==null || actor==null)
			return;
		
		try {
			recursivelyLoad(actor, input);
		}
		catch (Throwable e) {
		}
	}

	/**
	 * The recursive loading of an instance sub tree.
	 * 
	 * @param actor an actor class
	 * @param input the input stream
	 * @throws IOException
	 * @throws ClassNotFoundException 
	 */
	private void recursivelyLoad(ActorClassBase actor, ObjectInput input) throws IOException, ClassNotFoundException {
		if (actor instanceof IPersistable) {
			((IPersistable) actor).loadObject(input);
			
			for (IRTObject child : actor.getChildren()) {
				if (child instanceof ActorClassBase)
					recursivelyLoad((ActorClassBase) child, input);
				else if (child instanceof IPersistable) {
					((IPersistable) child).loadObject(input);
				}
			}
		}
	}
	
	/**
	 * This method is called when an {@link ObjectOutput} was passed to destruction
	 * and before the instance sub tree is destroyed.
	 * 
	 * <p>
	 * States, history and all attributes of the actor instances are stored.
	 * </p>
	 * 
	 * @param actor an actor class
	 * @param output the output stream
	 */
	protected void saveActor(ActorClassBase actor, ObjectOutput output) {
		if (output==null || actor==null)
			return;
		
		try {
			recursivelySave(actor, output);
		}
		catch (IOException e) {
		}
	}

	/**
	 * The recursive saving of an instance sub tree.
	 * 
	 * @param actor an actor class
	 * @param output the output stream
	 * @throws IOException 
	 */
	private void recursivelySave(ActorClassBase actor, ObjectOutput output) throws IOException {
		if (actor instanceof IPersistable) {
			((IPersistable) actor).saveObject(output);
			
			for (IRTObject child : actor.getChildren()) {
				if (child instanceof ActorClassBase)
					recursivelySave((ActorClassBase) child, output);
				else if (child instanceof IPersistable) {
					((IPersistable) child).saveObject(output);
				}
			}
		}
	}
}

Back to the top