Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6da6db7da37bfdceda14a7e3d13de0af380593cb (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
/*******************************************************************************
 * 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
 *******************************************************************************/

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

import org.eclipse.etrice.runtime.java.debugging.DebuggingService;
import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.MessageService;
import org.eclipse.etrice.runtime.java.messaging.RTObject;
import org.eclipse.etrice.runtime.java.messaging.RTServices;

/**
 * The base class for all SubSystems.
 * It and its derived classes take care of the instantiation, binding (connection) and complete lifecycle of all Actor Classes of a SubSystem
 * 
 * @author Henrik Rentz-Reichert
 *
 */
public abstract class SubSystemClassBase extends RTObject {

	private static SubSystemClassBase instance = null;
	
	private boolean running = false;
	protected ActorClassBase[] instances = null;

	public static SubSystemClassBase getInstance() {
		return instance;
	}
	
	public SubSystemClassBase(IRTObject parent, String name) {
		super(parent, name);

		if (instance!=null)
			throw new RuntimeException("ComponentClassBase is a singleton!");
		
		instance = this;
		
		DebuggingService.getInstance().getAsyncLogger()
				.setMSC(name + "_Async", "");
		DebuggingService.getInstance().getAsyncLogger().open();
		DebuggingService.getInstance().getSyncLogger()
				.setMSC(name + "_Sync", "");
		DebuggingService.getInstance().getSyncLogger().open();
	}

	public void init() {

		System.out.println("*** MainComponent "+getInstancePath()+"::init ***");

		// MessageService
		instantiateMessageServices();
		
		// this is the place to connect the message services if necessary
		// normaly the ports will address the correct target message service directly
		// it is just for test purposes 
		// RTServices.getInstance().getMsgSvcCtrl().connectAll();
		
		instantiateActors();

		// initialize all actor instances
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].init();
			}
	}

	public abstract void instantiateMessageServices();
	public abstract void instantiateActors();
	
	
	public void start() {
		// start all actor instances
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].start();
			}

		// start all message services
		RTServices.getInstance().getMsgSvcCtrl().start();
		
		running = true;
	}
	
	public void stop() {
		RTServices.getInstance().getMsgSvcCtrl().stop();

		// stop all actor instances
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].stop();
			}
	}
	
	public void destroy() {
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].destroy();
			}

		DebuggingService.getInstance().getAsyncLogger().close();
		DebuggingService.getInstance().getSyncLogger().close();

		System.out.println("*** MainComponent "+getInstancePath()+"::destroy ***");
	}
	
	public MessageService getMsgService(int idx) {
		return RTServices.getInstance().getMsgSvcCtrl().getMsgSvc(idx);
	}
	
	public ActorClassBase getInstance(int i) {
		if (instances==null || i<0 || i>= instances.length)
			return null;
		
		return instances[i];
	}
	
	public ActorClassBase getInstance(String path) {
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				if (instances[i].getInstancePath().equals(path))
					return instances[i];
			}
		
		return null;
	}
}

Back to the top