Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1a23bbb8ef8e01620d75648c43273b52c8423514 (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
/*******************************************************************************
 * Copyright (c) 2012 Draeger Medical GmbH (http://www.draeger.com).
 * 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:
 * 		Peter Karlitschek (initial contribution)
 *
 *******************************************************************************/

#include "SubSystemClassBase.h"
#include "common/messaging/MessageService.h"
#include "common/messaging/RTServices.h"
#include "ActorClassBase.h"
#include "common/debugging/DebuggingService.h"

namespace etRuntime {


SubSystemClassBase::~SubSystemClassBase() {
	m_testSem = 0;
	m_RTSystemPort = 0;
}

SubSystemClassBase::SubSystemClassBase(IRTObject* parent, std::string name)
	: 	RTObject(parent, name),
		IEventReceiver(),
		m_RTSystemPort(0),
		m_instances(),
		m_testSem(0),
		m_testErrorCode(0)
{
	DebuggingService::getInstance().getAsyncLogger().setMSC(name + "_Async", "");
	DebuggingService::getInstance().getAsyncLogger().open();
	DebuggingService::getInstance().getSyncLogger().setMSC(name + "_Sync", "");
	DebuggingService::getInstance().getSyncLogger().open();

	RTServices::getInstance().setSubSystem(this);
}

void SubSystemClassBase::init() {

	std::cout << "*** MainComponent " << getInstancePath() << "::init ***" << std::endl;

	// 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 m_instances
	for (unsigned int i = 0; i < m_instances.size(); i++) {
		m_instances[i]->init();
	}
}

void SubSystemClassBase::start(bool singlethreaded) {
	// start all actors instances
	m_RTSystemPort->executeInitialTransition();

	// start all message services
	RTServices::getInstance().getMsgSvcCtrl().start(singlethreaded);

}

void SubSystemClassBase::stop(bool singlethreaded) {
	std::cout << "*** MainComponent " << this->getInstancePath() << "::stop ***" << std::endl;

	RTServices::getInstance().getMsgSvcCtrl().stop(singlethreaded);
	std::cout << "=== done stop MsgSvcCtrl" << std::endl;

	// stop all actor instances
	for (unsigned int i = 0; i < m_instances.size(); i++) {
		m_instances[i]->stop();
	}
	std::cout << "=== done stop actor instances" << std::endl;
}

void SubSystemClassBase::runOnce() {
	// run all message services one time
	RTServices::getInstance().getMsgSvcCtrl().runOnce();

}

void SubSystemClassBase::destroy() {
	std::cout << "*** MainComponent " << this->getInstancePath() << "::destroy ***" << std::endl;
	for (unsigned int i = 0; i < m_instances.size(); i++) {
		m_instances[i]->destroy();
	}
	std::cout << "=== done destroy actor instances" << std::endl;

	DebuggingService::getInstance().getAsyncLogger().close();
	DebuggingService::getInstance().getSyncLogger().close();
	std::cout << "=== done close loggers" << std::endl;

	RTServices::getInstance().destroy();
	std::cout << "=== done destroy RTServices\n\n\n" << std::endl;
}

MessageService* SubSystemClassBase::getMsgService(int idx) const {
	return RTServices::getInstance().getMsgSvcCtrl().getMsgSvc(idx);
}

ActorClassBase* SubSystemClassBase::getInstance(unsigned int i) {
	if (i < 0 || i >= m_instances.size())
		return 0;

	return m_instances[i];
}

ActorClassBase* SubSystemClassBase::getInstance(std::string path) {
	for (unsigned int i = 0; i < m_instances.size(); i++) {
		if (m_instances[i]->getInstancePath() == path)
			return m_instances[i];
	}

	return 0;
}

// this is to run integration tests
// TODO synchronized
void SubSystemClassBase::setTestSemaphore(TestSemaphore& sem) {
	m_testErrorCode = -1;
	m_testSem = &sem;
}

//TODO synchronized
int SubSystemClassBase::getTestErrorCode() const {
	return m_testErrorCode;
}

void SubSystemClassBase::testFinished(int errorCode) {
	if (m_testSem != 0) {
		std::cout
			<< "org.eclipse.etrice.runtime.cpp.modelbase.SubSystemClassBase.testFinished(int): before releasing semaphore"
			<< std::endl;
		//m_testSem.printWaitingThreads();
		//TODO synchronized (this) {
		m_testErrorCode = errorCode;
		m_testSem->give();
		//}
		std::cout
			<< "org.eclipse.etrice.runtime.cpp.modelbase.SubSystemClassBase.testFinished(int): semaphore released"
			<< std::endl;
		//m_testSem.printWaitingThreads();
		//TODO
		//Thread.yield();
	}
}


} /* namespace etRuntime */

Back to the top