Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f941cadee313cacaa00c023cc47c531dcaebfd0b (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
/*******************************************************************************
 * Copyright (c) 2011 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
 * 
 * CONTRIBUTORS:
 * 		Thomas Schuetz
 * 
 *******************************************************************************/

package org.eclipse.etrice.runtime.java.messaging;

import java.util.ArrayList;
import java.util.List;

/**
 * The MessageServiceController controls lifecycle of and access to all MessageServices in one SubSystem
 * 
 * @author Thomas Schuetz
 *
 */

public class MessageServiceController {

	public MessageServiceController(/*IRTObject parent*/){
		// TODOTS: Who is parent of MessageServices and Controller?
		// this.parent = parent;
		messageServiceList = new ArrayList<MessageService>();
	}

	public void addMsgSrv(MessageService msgSrv){
		// TODOTS: Who is parent of MessageServices ?
		assert(msgSrv.getAddress().threadID == messageServiceList.size());
		messageServiceList.add(msgSrv);
	}
	
	public MessageService getMessageService(int threadID){
		assert(threadID < messageServiceList.size());
		return messageServiceList.get(threadID);
	}


	
	public void start() {
		// start all message services
		for (MessageService msgSrv : messageServiceList){
			msgSrv.start();
			// TODOTS: start in order of priorities
		}
		running = true;
	}

	public void stop() {
		terminate();
		waitTerminate();
	}

	private void terminate() {
		if (!running){
			return;
		}
		running = false;
		
		// terminate all message services
		for (MessageService msgSrv : messageServiceList){
			msgSrv.terminate();
			// TODOTS: stop in order of priorities
		}
	}

	/**
	 * waitTerminate waits blocking for all MessageServices to terminate 
	 * ! not threadsafe !
	 */
	public void waitTerminate() {
		for (MessageService msgSrv : messageServiceList){
			try {
				msgSrv.join();
				} 
			catch (InterruptedException e1) {
			}
		}
	}
	
	private List<MessageService> messageServiceList = null;
//	private IRTObject parent = null;
	private boolean running = false;
}

Back to the top