Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af2211bcd1fc7e19560eca16528a0ffb37bbd9e7 (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
/*******************************************************************************
 * 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.util.ArrayList;
import java.util.LinkedList;

import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.RTServices;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class ReplicatedOptionalActorInterfaceBase extends OptionalActorInterfaceBase {

	private static final char INDEX_SEP = ':';
	private LinkedList<Integer> releasedIndices = new LinkedList<Integer>();
	private ArrayList<ActorClassBase> actors = new ArrayList<ActorClassBase>();

	/**
	 * @param parent
	 * @param name
	 * @param clsname
	 */
	public ReplicatedOptionalActorInterfaceBase(IEventReceiver parent, String name, String clsname) {
		super(parent, name, clsname);
	}
	
	public int createOptionalActor(String actorClass, int thread) {
		setSubtreeThread(thread);
		
		// make sure the path is up to date
		setOwnPath(getInstancePath());
		
		IOptionalActorFactory factory = RTServices.getInstance().getSubSystem().getFactory(getClassName(), actorClass);
		if (factory==null)
			return -1;
		
		// the factory will set our path2peers map
		int index = getFreeIndex();
		String name = getChildName(index);
		logCreation(actorClass, name);
		ActorClassBase actor = factory.create(this, name);
		if (actor==null)
			return -1;
		
		actors.add(actor);
		
		startSubTree();
		
		return index;
	}
	
	/**
	 * @param idx
	 * @return
	 */
	public boolean destroyOptionalActor(int idx) {
		String childName = getChildName(idx);
		logDeletion(childName);
		IRTObject child = getChild(childName);
		if (!(child instanceof ActorClassBase))
			return false;
		
		((ActorClassBase)child).destroy();
		releasedIndices.push(idx);
		actors.remove(child);
		
		return true;
	}
	
	public void destroyAllOptionalActors() {
		for (ActorClassBase actor : actors) {
			actor.destroy();
			int idx = Integer.parseInt(actor.getName().substring(getName().length()));
			releasedIndices.push(idx);
		}
		actors.clear();
	}
	
	public String getChildName(int idx) {
		return getName()+INDEX_SEP+idx;
	}
	
	private int getFreeIndex() {
		if (releasedIndices.isEmpty())
			return actors.size();
		else
			return releasedIndices.pop();
	}

	public String toString(){
		return "ReplicatedOptionalActorInterface(className="+getClassName()+", instancePath="+getInstancePath()+")";
	}

}

Back to the top