Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0eb198936cae1c8c1903821d4fe249ce1b2d130a (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
/*******************************************************************************
 * 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.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;
import org.eclipse.etrice.runtime.java.messaging.RTSystemServicesProtocol.RTSystemServicesProtocolConjPortRepl;

/**
 * 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 implements IEventReceiver{

	//--------------------- ports
	protected RTSystemServicesProtocolConjPortRepl RTSystemPort = null;
	//--------------------- interface item IDs
	protected static final int IFITEM_RTSystemPort = 0;
	protected ActorClassBase[] instances = null;
	private TestSemaphore testSem=null;
	private int testErrorCode;
	
	public SubSystemClassBase(IRTObject parent, String name) {
		super(parent, name);

		DebuggingService.getInstance().getAsyncLogger()
				.setMSC(name + "_Async", "");
		DebuggingService.getInstance().getAsyncLogger().open();
		DebuggingService.getInstance().getSyncLogger()
				.setMSC(name + "_Sync", "");
		DebuggingService.getInstance().getSyncLogger().open();
		
		RTServices.getInstance().setSubSystem(this);
		
	}

	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 actors instances
		RTSystemPort.executeInitialTransition();
		
		// start all message services
		RTServices.getInstance().getMsgSvcCtrl().start();
		
	}
	
	public void stop() {
		System.out.println("*** MainComponent "+getInstancePath()+"::stop ***");
		
		RTServices.getInstance().getMsgSvcCtrl().stop();
		System.out.println("=== done stop MsgSvcCtrl");

		// stop all actor instances
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].stop();
			}
		System.out.println("=== done stop actor instances");
	}
	
	public void destroy() {
		System.out.println("*** MainComponent "+getInstancePath()+"::destroy ***");
		if (instances!=null)
			for (int i = 0; i < instances.length; i++) {
				instances[i].destroy();
			}
		System.out.println("=== done destroy actor instances");

		DebuggingService.getInstance().getAsyncLogger().close();
		DebuggingService.getInstance().getSyncLogger().close();
		System.out.println("=== done close loggers");

		RTServices.getInstance().destroy();
		System.out.println("=== done destroy RTServices\n\n\n");
	}
	
	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;
	}
	
	// this is to run integration tests
	public synchronized void setTestSemaphore(TestSemaphore sem){
		testErrorCode = -1;
		testSem=sem;
	}
	
	public synchronized int getTestErrorCode(){
		return testErrorCode;
	}
	
	public void testFinished(int errorCode){
		if (testSem != null) {
			System.out.println("org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase.testFinished(int): before releasing semaphore");
			//testSem.printWaitingThreads();
			synchronized (this) {
				testErrorCode = errorCode;
				testSem.release(1);
			}
			System.out.println("org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase.testFinished(int): semaphore released");
			//testSem.printWaitingThreads();
			Thread.yield();
		}
	}
}

Back to the top