Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fbf9033138dee0c3df4233845ca7abe514957b34 (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
/*******************************************************************************
 * 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 "MessageServiceController.h"

namespace etRuntime {


MessageServiceController::~MessageServiceController() {
	// TODO Auto-generated destructor stub
}

MessageServiceController::MessageServiceController(/*IRTObject parent*/)
	: 	m_messageServiceList(),
		m_running(false) {
	// TODO: Who is parent of MessageServices and Controller?
	// this.parent = parent;
}

void MessageServiceController::addMsgSvc(MessageService& msgSvc) {
	// TODO TS: Who is parent of MessageServices ?
	//TODO assert
	//assert(msgSvc.getAddress().m_threadID == m_messageServiceList.size());
	m_messageServiceList.push_back(&msgSvc);
}

MessageService* MessageServiceController::getMsgSvc(int threadID) {
	return m_messageServiceList.at(threadID);
}

void MessageServiceController::connectAll() {
	for (size_t i = 0; i < m_messageServiceList.size(); i++) {
		MessageDispatcher& dispatcher = getMsgSvc(i)->getMessageDispatcher();
		for (size_t j = 0; j < m_messageServiceList.size(); j++) {
			if (i != j) {
				dispatcher.addMessageReceiver(*RTServices::getInstance().getMsgSvcCtrl().getMsgSvc(j));
			}
		}
	}
}

void MessageServiceController::start(bool singlethreaded) {
	// start all message services
	for (std::vector<MessageService*>::iterator it = m_messageServiceList.begin();
			it != m_messageServiceList.end(); ++it) {
		(*it)->start(singlethreaded);
		// TODO TS: start in order of priorities
	}
	m_running = true;
}

void MessageServiceController::stop(bool singlethreaded) {
	//dumpThreads("org.eclipse.etrice.runtime.java.messaging.MessageServiceController.stop()");
	if (! singlethreaded) {
		terminate();
		waitTerminate();
	}
}

void MessageServiceController::waitTerminate() {
	for (std::vector<MessageService*>::iterator it = m_messageServiceList.begin();
			it != m_messageServiceList.end(); ++it) {
		(*it)->join();
	}
}

void MessageServiceController::dumpThreads(std::string msg) {
	std::cout << "<<< begin dump threads <<<" << std::endl;
	std::cout << "=== "  << msg << std::endl;
	//TODO dump stack traces
//		Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
//		for (Thread thread : traces.keySet()) {
//			std::cout << "thread " << thread.getName() << std::endl;
//			StackTraceElement[] elements = traces.get(thread);
//			int n = 2;
//			if (elements.length < n)
//				n = elements.length;
//			for (int i = 0; i < n; i++) {
//				std::cout << " " << elements[i].toString() << std::endl;
//			}
//		}
	std::cout <<(">>> end dump threads >>>");
}

void MessageServiceController::terminate() {
	if (!m_running) {
		return;
	}
	m_running = false;

	// terminate all message services
	for (std::vector<MessageService*>::iterator it = m_messageServiceList.begin();
			it != m_messageServiceList.end(); ++it) {
		(*it)->terminate();
		//TODO TS: stop in order of priorities
	}
}

void MessageServiceController::runOnce() {
	if (!m_running) {
		return;
	}

	for (std::vector<MessageService*>::iterator it = m_messageServiceList.begin();
			it != m_messageServiceList.end(); ++it) {
		(*it)->runOnce();
	}
}

} /* namespace etRuntime */

Back to the top