Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7d444a0132dd97a597aa0d702bc860ad4bad0665 (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
package org.eclipse.etrice.runtime.java.modelbase;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * The base class for running components.
 * 
 * @author Henrik Rentz-Reichert
 *
 */
public class SubSystemRunnerBase {

	private static final String OPTION_RUN_AS_TEST = "-run_as_test";
	private static final String OPTION_HEADLESS = "-headless";
	
	private static boolean headless = false;
	protected static TestSemaphore terminateSem = new TestSemaphore(0);

	protected static void run(SubSystemClassBase main_component, String[] args) {
		
		System.out.println("***   T H E   B E G I N   ***");

		for (String arg : args) {
			if (arg.equals(OPTION_RUN_AS_TEST)) {
				System.out.println("*** running as test");
				headless = true;
			}
			else if (arg.equals(OPTION_HEADLESS)) {
				System.out.println("*** running headless");
				headless = true;
			}
		}

		if (headless)
			main_component.setTerminateSemaphore(terminateSem);
		
		main_component.init(); // lifecycle init
		main_component.start(); // lifecycle start

		// application runs until quit
		if (headless)
			waitForTerminate();
		else
			waitForQuit();
		
		// end the lifecycle
		main_component.stop(); // lifecycle stop
		main_component.destroy(); // lifecycle destroy

		System.out.println("***   T H E   E N D   ***");
	}
	
	/**
	 * blocks until the String "quit" is entered on the console
	 */
	protected static void waitForQuit() {
		// waiting for command line input
		BufferedReader bk = new BufferedReader(new InputStreamReader(System.in));
		String token = new String("");
		System.out.println("type 'quit' to exit");
		while (!token.equals("quit")) {
			try {
				token = bk.readLine();
				System.out.println("echo: " + token);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static void waitForTerminate() {
		try{
			System.out.println("=== waitForTestcase: before acq. semaphore, thread "+Thread.currentThread().getName());
			terminateSem.acquire(1);
			System.out.println("=== waitForTestcase: after acq. semaphore, thread "+Thread.currentThread().getName());
		}catch(InterruptedException e){
			System.out.println("Semaphore fault !");
		}
	}

}

Back to the top