Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9348c0d840451c35e44f1b1086a6552d4acd307a (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
/*******************************************************************************
 * 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 "common/modelbase/SubSystemRunnerBase.h"
#include "common/modelbase/SubSystemClassBase.h"
#include "common/platform/etTimer.h"
#include <unistd.h>
#include <iostream>
#include <sstream>

namespace etRuntime {

const std::string SubSystemRunnerBase::OPTION_RUN_AS_TEST = "-run_as_test";
const std::string SubSystemRunnerBase::OPTION_RUN_AS_TEST_SINGLETHREADED = "-run_as_test_single_threaded";
const std::string SubSystemRunnerBase::OPTION_RUN_SINGLETHREADED = "-run_single_threaded";

TestSemaphore SubSystemRunnerBase::s_testSem;

SubSystemRunnerBase::SubSystemRunnerBase() {
}

SubSystemRunnerBase::~SubSystemRunnerBase() {
}

void SubSystemRunnerBase::run(SubSystemClassBase& mainComponent, int argc, char* argv[] ) {

	//etUserEntry(); /* platform specific */

	std::cout << "***   T H E   B E G I N   ***" << std::endl;

	bool test = false;
	bool singleThreaded = false;
	int cycles = 100;

	for (int i=1; i<argc; ++i) { // omit first argument, which is the program name
		if (SubSystemRunnerBase::OPTION_RUN_AS_TEST.compare(argv[i]) == 0) {
			std::cout << "*** running as test" << std::endl;
			test = true;
		}
		else if (SubSystemRunnerBase::OPTION_RUN_AS_TEST_SINGLETHREADED.compare(argv[i]) == 0) {

			singleThreaded = true;
			i++;
			if (i < argc) {
				std::stringstream sstr(argv[i]);
			    sstr >> cycles;
			}
			std::cout << "*** running as test singlethreaded " << cycles << " cycles" << std::endl;
		}
		else if (SubSystemRunnerBase::OPTION_RUN_SINGLETHREADED.compare(argv[i]) == 0) {

			singleThreaded = true;
			i++;
			if (i < argc) {
				std::stringstream sstr(argv[i]);
			    sstr >> cycles;
			}
			std::cout << "*** running singlethreaded " << cycles << " cycles" << std::endl;
		}
		else {
			std::cout << "*** running multithreaded" << std::endl;
		}
	}

	if (test)
		mainComponent.setTestSemaphore(s_testSem);

	mainComponent.init(); // lifecycle init
	mainComponent.start(singleThreaded); // lifecycle start

	// application runs until quit
	if (test) {
		waitForTestcase();
	}
	else if (singleThreaded){
		waitAndPollSingleThreaded(mainComponent, cycles);
	}
	else {
		waitForQuitMultiThreaded();
	}
	// end the lifecycle
	mainComponent.stop(singleThreaded); // lifecycle stop
	mainComponent.destroy(); // lifecycle destroy

	std::cout << "***   T H E   E N D   ***" << std::endl;

	//etUserExit(); /* platform specific */
}

void SubSystemRunnerBase::waitForTestcase() {
	//std::cout << "=== waitForTestcase: before acq. semaphore, thread " << Thread.currentThread().getName() << std::endl;
	s_testSem.take();
	//std::cout << "=== waitForTestcase: after acq. semaphore, thread " << Thread.currentThread().getName() << std::endl;
}

void SubSystemRunnerBase::waitForQuitMultiThreaded() {
	// waiting for command line input
	std::string token = "";
	std::cout << "type 'quit' to exit" << std::endl;
	while (token != "quit") {
		std::getline(std::cin, token);
		std::cout << "echo: " << token << std::endl;
	}
}

void SubSystemRunnerBase::waitAndPollSingleThreaded(SubSystemClassBase& mainComponent, int cycles) {
	for (int i=0; i< cycles; ++i) {
		if (etTimer_executeNeeded()) {
			mainComponent.runOnce();
		}
		usleep(100000);
	}
}
} /* namespace etRuntime */

Back to the top