Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c58c09538d00cf2935520c9617da29c90143ffb3 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/**
 * SimulatorUtils.java
 * Created on Jan 22, 2013
 *
 * Copyright (c) 2013 Wind River Systems, Inc.
 *
 * The right to copy, distribute, modify, or otherwise make use
 * of this software may be licensed only pursuant to the terms
 * of an applicable Wind River license agreement.
 */
package org.eclipse.tcf.te.tcf.locator.utils;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.callback.Callback;
import org.eclipse.tcf.te.runtime.interfaces.callback.ICallback;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IService;
import org.eclipse.tcf.te.runtime.services.interfaces.ISimulatorService;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProperties;

/**
 * Simulator related utilities.
 */
public final class SimulatorUtils {

	/**
	 * Result of getSimulatorService.
	 */
	public static class Result {
		public ISimulatorService service;
		public String id;
		public String settings;
	}

	/**
	 * Returns if or if the given peer model has the simulator enabled or not.
	 *
	 * @param peerModel The peer model node. Must not be <code>null</code>.
	 * @return <code>True</code> if the simulator is enabled, <code>false</code> otherwise.
	 */
	public static boolean isSimulatorEnabled(final IPeerModel peerModel) {
		Assert.isNotNull(peerModel);

		final AtomicBoolean isEnabled = new AtomicBoolean(false);

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				String value = peerModel.getPeer().getAttributes().get(IPeerModelProperties.PROP_SIM_ENABLED);
				if (value != null) {
					isEnabled.set(Boolean.parseBoolean(value));
				}
			}
		};

		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeAndWait(runnable);

		return isEnabled.get();
	}

	/**
	 * Returns the simulator service and the settings for the simulator launch.
	 * If no simulator service is configured in the peer
	 * or the configured service is not available, <code>null</code> will be returned.
	 *
	 * @param peerModel The peer model node. Must not be <code>null</code>.
	 * @return The {@link Result} containing the simulator service and the settings or <code>null</code>.
	 */
	public static Result getSimulatorService(final IPeerModel peerModel) {
		Assert.isNotNull(peerModel);

		final AtomicBoolean isEnabled = new AtomicBoolean(false);
		final AtomicReference<String> type = new AtomicReference<String>();
		final AtomicReference<String> properties = new AtomicReference<String>();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				String value = peerModel.getPeer().getAttributes().get(IPeerModelProperties.PROP_SIM_ENABLED);
				if (value != null) {
					isEnabled.set(Boolean.parseBoolean(value));
				}

				type.set(peerModel.getPeer().getAttributes().get(IPeerModelProperties.PROP_SIM_TYPE));
				properties.set(peerModel.getPeer().getAttributes().get(IPeerModelProperties.PROP_SIM_PROPERTIES));
			}
		};

		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeAndWait(runnable);

		Result result = null;

		if (isEnabled.get()) {
			IService[] services = ServiceManager.getInstance().getServices(peerModel, ISimulatorService.class, false);
			for (IService service : services) {
				Assert.isTrue(service instanceof ISimulatorService);
				// Get the UI service which is associated with the simulator service
				String id = service.getId();
				if (id != null && id.equals(type.get())) {
					result = new Result();
					result.service = (ISimulatorService)service;
					result.id = id;
					result.settings = properties.get();
					break;
				}
			}
		}
		return result;
	}

	/**
	 * Starts the simulator if the simulator launch is enabled for the given peer
	 * model node and the configured simulator service type is available. In any
	 * other cases, the given callback is invoked immediately.
	 *
	 * @param peerModel The peer model node. Must not be <code>null</code>.
	 * @param monitor The progress monitor.
	 * @param callback The callback to invoke if finished. Must not be <code>null</code>.
	 */
	public static void start(final IPeerModel peerModel, final IProgressMonitor monitor, final ICallback callback) {
		Assert.isNotNull(peerModel);
		Assert.isNotNull(callback);

		// Determine if we have to start a simulator first
		final Result result = getSimulatorService(peerModel);
		if (result != null && result.service != null) {
			// Check if the simulator is already running
			result.service.isRunning(peerModel, result.settings, new Callback() {
				@Override
				protected void internalDone(Object caller, IStatus status) {
					Object cbResult = getResult();
					if (cbResult instanceof Boolean && !((Boolean)cbResult).booleanValue()) {
						// Start the simulator
						result.service.start(peerModel, result.settings, new Callback() {
							@Override
							protected void internalDone(Object caller, IStatus status) {
								callback.setResult(new Boolean(status.isOK()));
								callback.done(caller, status);
							}
						}, monitor);
					} else {
						callback.setResult(Boolean.FALSE);
						callback.done(this, Status.OK_STATUS);
					}
				}
			}, monitor);
		} else {
			callback.setResult(Boolean.FALSE);
			callback.done(null, Status.OK_STATUS);
		}
	}

	/**
	 * Stops the simulator if the simulator launch is enabled for the given peer
	 * model node and the configured simulator service type is available. In any
	 * other cases, the given callback is invoked immediately.
	 *
	 * @param peerModel The peer model node. Must not be <code>null</code>.
	 * @param monitor The progress monitor.
	 * @param callback The callback to invoke if finished. Must not be <code>null</code>.
	 */
	public static void stop(final IPeerModel peerModel, final IProgressMonitor monitor, final ICallback callback) {
		Assert.isNotNull(peerModel);
		Assert.isNotNull(callback);

		// Get the associated simulator service
		final Result result = getSimulatorService(peerModel);
		if (result != null && result.service != null) {
			// Determine if the simulator is at all running
			result.service.isRunning(peerModel, result.settings, new Callback() {
				@Override
				protected void internalDone(Object caller, IStatus status) {
					Object cbResult = getResult();
					if (cbResult instanceof Boolean && ((Boolean)cbResult).booleanValue()) {
						// Stop the simulator
						result.service.stop(peerModel, result.settings, new Callback(callback) {
							@Override
							protected void internalDone(Object caller, IStatus status) {
								callback.done(caller, status);
							}
						}, monitor);
					} else {
						callback.done(null, Status.OK_STATUS);
					}
				}
			}, monitor);
		} else {
			callback.done(null, Status.OK_STATUS);
		}
	}
}

Back to the top