author | dadubrow |
Tue, 16 Feb 2010 11:09:11 -0600 | |
branch | RCL_2_4 |
changeset 965 | 7942782aa571 |
parent 932 | 9eff68f52ec6 |
child 973 | bd54fb1ea34a |
permissions | -rw-r--r-- |
860 | 1 |
/** |
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 |
* All rights reserved. |
|
4 |
* This component and the accompanying materials are made available |
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
|
6 |
* which accompanies this distribution, and is available |
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 |
* |
|
9 |
* Initial Contributors: |
|
10 |
* Nokia Corporation - initial contribution. |
|
11 |
* |
|
12 |
* Contributors: |
|
13 |
* |
|
14 |
* Description: |
|
15 |
* |
|
16 |
*/ |
|
17 |
||
18 |
package com.nokia.carbide.trk.support.status; |
|
19 |
||
20 |
import java.util.ArrayList; |
|
21 |
import java.util.List; |
|
22 |
||
932
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
23 |
import org.eclipse.swt.widgets.Display; |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
24 |
import org.eclipse.swt.widgets.Shell; |
860 | 25 |
import org.eclipse.ui.PartInitException; |
26 |
||
27 |
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
|
28 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService; |
|
29 |
import com.nokia.carbide.remoteconnections.interfaces.IConnection; |
|
30 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
|
31 |
import com.nokia.carbide.remoteconnections.interfaces.AbstractConnection.ConnectionStatus; |
|
32 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus; |
|
33 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener; |
|
34 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus; |
|
35 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener; |
|
36 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2; |
|
37 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus; |
|
38 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus; |
|
39 |
import com.nokia.carbide.trk.support.Messages; |
|
40 |
import com.nokia.carbide.trk.support.connection.USBConnectionType; |
|
41 |
import com.nokia.carbide.trk.support.service.TRKConnectedService; |
|
42 |
import com.nokia.carbide.trk.support.service.TracingConnectedService; |
|
43 |
import com.nokia.cpp.internal.api.utils.ui.RunRunnableWhenWorkbenchVisibleJob; |
|
44 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
|
45 |
||
46 |
/** |
|
47 |
* A singleton object that manages the device status of dynamic connections |
|
48 |
* based on the status of the TRK and Tracing services. |
|
49 |
*/ |
|
50 |
public class ConnectionStatusReconciler { |
|
51 |
||
52 |
private static final String CONNECTIONS_VIEW_ID = |
|
53 |
"com.nokia.carbide.remoteconnections.view.ConnectionsView"; //$NON-NLS-1$ |
|
54 |
||
55 |
private class ConnectionListener implements IConnectionListener { |
|
56 |
||
57 |
public void connectionAdded(IConnection connection) { |
|
58 |
addConnection(connection); |
|
59 |
} |
|
60 |
||
61 |
public void connectionRemoved(IConnection connection) { |
|
62 |
if (connection.equals(userSetCurrentConnection)) |
|
63 |
userSetCurrentConnection = null; |
|
64 |
removeConnection(connection); |
|
65 |
} |
|
66 |
||
67 |
public void currentConnectionSet(IConnection connection) { |
|
68 |
if (connection != null && !connection.equals(reconcilerSetCurrentConnection)) |
|
69 |
userSetCurrentConnection = connection; |
|
70 |
} |
|
71 |
||
72 |
} |
|
73 |
||
74 |
private class ServiceStatusListener implements IStatusChangedListener { |
|
75 |
||
76 |
public void statusChanged(IStatus status) { |
|
77 |
handleServiceStatusChange(status); |
|
78 |
} |
|
79 |
||
80 |
} |
|
81 |
||
82 |
private static ConnectionStatusReconciler instance; |
|
83 |
private IConnectionsManager manager; |
|
84 |
private IConnectionListener connectionListener; |
|
85 |
private List<IConnection> handledConnections; |
|
86 |
private ServiceStatusListener serviceStatusListener; |
|
87 |
private IConnection reconcilerSetCurrentConnection; |
|
88 |
private IConnection userSetCurrentConnection; |
|
89 |
||
90 |
private ConnectionStatusReconciler() { |
|
91 |
connectionListener = new ConnectionListener(); |
|
92 |
manager = RemoteConnectionsActivator.getConnectionsManager(); |
|
93 |
manager.addConnectionListener(connectionListener); |
|
94 |
handledConnections = new ArrayList<IConnection>(); |
|
95 |
serviceStatusListener = new ServiceStatusListener(); |
|
96 |
} |
|
97 |
||
98 |
public static ConnectionStatusReconciler getInstance() { |
|
99 |
if (instance == null) |
|
100 |
instance = new ConnectionStatusReconciler(); |
|
101 |
||
102 |
return instance; |
|
103 |
} |
|
104 |
||
105 |
public void dispose() { |
|
106 |
manager.removeConnectionListener(connectionListener); |
|
107 |
for (IConnection connection : new ArrayList<IConnection>(handledConnections)) { |
|
108 |
removeConnection(connection); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
private boolean isDynamic(IConnection connection) { |
|
113 |
return connection instanceof IConnection2 && ((IConnection2) connection).isDynamic(); |
|
114 |
} |
|
115 |
||
116 |
private boolean isSysTRK(TRKConnectedService service) { |
|
117 |
String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK); |
|
118 |
return Boolean.parseBoolean(value); |
|
119 |
} |
|
120 |
||
121 |
private void addConnection(IConnection connection) { |
|
122 |
handledConnections.add(connection); |
|
123 |
for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
124 |
if (service instanceof TRKConnectedService || |
|
125 |
service instanceof TracingConnectedService) { |
|
126 |
service.addStatusChangedListener(serviceStatusListener); |
|
127 |
} |
|
128 |
} |
|
129 |
showConnectionsView(); |
|
130 |
} |
|
131 |
||
132 |
private void showConnectionsView() { |
|
932
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
133 |
// avoid deadlock if this called as a result of a launch sequence issuing a "select connection" dialog |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
134 |
Shell shell = WorkbenchUtils.getActiveShell(); |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
135 |
if (shell == null || !shell.isVisible()) { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
136 |
RunRunnableWhenWorkbenchVisibleJob.start(new Runnable() { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
137 |
public void run() { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
138 |
// try to show the connections view to start service testers |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
139 |
try { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
140 |
WorkbenchUtils.getView(CONNECTIONS_VIEW_ID); |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
141 |
} catch (PartInitException e) { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
142 |
} |
860 | 143 |
} |
932
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
144 |
}); |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
145 |
} else { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
146 |
Display.getDefault().asyncExec(new Runnable() { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
147 |
public void run() { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
148 |
try { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
149 |
WorkbenchUtils.getView(CONNECTIONS_VIEW_ID); |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
150 |
} catch (PartInitException e) { |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
151 |
} |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
152 |
} |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
153 |
}); |
9eff68f52ec6
Fix deadlock when the Select Connection dialog is up, user plugs in a device, and the Remote Connections view is shown
dadubrow
parents:
860
diff
changeset
|
154 |
} |
860 | 155 |
} |
156 |
||
157 |
private void reconcileAsCurrent(IConnection connection) { |
|
158 |
if (canBeSetToCurrent(connection)) { |
|
159 |
if (isReady(connection)) { // set as current |
|
160 |
reconcilerSetCurrentConnection = connection; |
|
161 |
manager.setCurrentConnection(connection); |
|
162 |
} else if (isNotReady(connection) && connection.equals(manager.getCurrentConnection())) { |
|
163 |
// unset current or set something else current |
|
164 |
if (isDynamic(connection) && userSetCurrentConnection != null) { |
|
165 |
manager.setCurrentConnection(userSetCurrentConnection); |
|
166 |
} |
|
167 |
else { |
|
168 |
// look for some other existing connection that is ready |
|
169 |
for (IConnection c : manager.getConnections()) { |
|
170 |
if (canBeSetToCurrent(c) && isReady(c)) { |
|
171 |
reconcilerSetCurrentConnection = connection; |
|
172 |
manager.setCurrentConnection(connection); |
|
173 |
return; |
|
174 |
} |
|
175 |
} |
|
176 |
// set to no current connection |
|
177 |
manager.setCurrentConnection(null); |
|
178 |
} |
|
179 |
} |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
private boolean isReady(IConnection connection) { |
|
184 |
return equalsConnectionStatus(connection, EConnectionStatus.READY); |
|
185 |
} |
|
186 |
||
187 |
private boolean isNotReady(IConnection connection) { |
|
188 |
return equalsConnectionStatus(connection, EConnectionStatus.NOT_READY); |
|
189 |
} |
|
190 |
||
191 |
private boolean equalsConnectionStatus(IConnection connection, EConnectionStatus status) { |
|
192 |
if (connection instanceof IConnection2) { |
|
193 |
IConnectionStatus connectionStatus = ((IConnection2) connection).getStatus(); |
|
194 |
if (connectionStatus != null) |
|
195 |
return connectionStatus.getEConnectionStatus().equals(status); |
|
196 |
} |
|
197 |
return false; |
|
198 |
} |
|
199 |
||
200 |
private boolean canBeSetToCurrent(IConnection connection) { |
|
201 |
// USB connections for now |
|
202 |
return USBConnectionType.ID.equals(connection.getConnectionType().getIdentifier()); |
|
203 |
} |
|
204 |
||
205 |
private void reconcileStatus(IConnection connection) { |
|
206 |
if (!isDynamic(connection)) // don't set status for user generated connections |
|
207 |
return; |
|
208 |
||
209 |
boolean isSysTRK = false; |
|
210 |
EStatus trkStatus = EStatus.UNKNOWN; |
|
211 |
EStatus traceStatus = EStatus.UNKNOWN; |
|
212 |
for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
213 |
if (service instanceof TRKConnectedService) { |
|
214 |
isSysTRK = isSysTRK((TRKConnectedService) service); |
|
215 |
trkStatus = service.getStatus().getEStatus(); |
|
216 |
} |
|
217 |
if (service instanceof TracingConnectedService) { |
|
218 |
traceStatus = service.getStatus().getEStatus(); |
|
219 |
} |
|
220 |
} |
|
221 |
setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus); |
|
222 |
} |
|
223 |
||
224 |
private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) { |
|
225 |
// use trk status |
|
226 |
EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus); |
|
965 | 227 |
// NOTE: removing trace status logic for now |
228 |
// // if sys trk, tracing also used |
|
229 |
// if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) { |
|
230 |
// connectionStatus = service2ConnectionStatus(traceStatus); |
|
231 |
// } |
|
860 | 232 |
|
233 |
String shortDesc = getShortDescriptionForStatus(connectionStatus); |
|
234 |
StringBuilder longDesc = new StringBuilder(Messages.getString("ConnectionStatusReconciler_TRKServicePrefix")); //$NON-NLS-1$ |
|
235 |
longDesc.append(getServiceStatusString(trkStatus)); |
|
965 | 236 |
// if (isSysTRK) { |
237 |
// longDesc.append(Messages.getString("ConnectionStatusReconciler_TracingServicePrefix")); //$NON-NLS-1$ |
|
238 |
// longDesc.append(getServiceStatusString(traceStatus)); |
|
239 |
// } |
|
860 | 240 |
|
241 |
connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString())); |
|
242 |
} |
|
243 |
||
244 |
private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) { |
|
245 |
switch (connectionStatus) { |
|
246 |
case READY: |
|
247 |
return Messages.getString("ConnectionStatusReconciler_ReadyLabel"); //$NON-NLS-1$ |
|
248 |
case NOT_READY: |
|
249 |
return Messages.getString("ConnectionStatusReconciler_NotReadyLabel"); //$NON-NLS-1$ |
|
250 |
case IN_USE: |
|
251 |
return Messages.getString("ConnectionStatusReconciler_InUseLabel"); //$NON-NLS-1$ |
|
252 |
case IN_USE_DISCONNECTED: |
|
253 |
return Messages.getString("ConnectionStatusReconciler_DisconnectedLabel"); //$NON-NLS-1$ |
|
254 |
} |
|
255 |
return ""; //$NON-NLS-1$ |
|
256 |
} |
|
257 |
||
258 |
private String getServiceStatusString(EStatus status) { |
|
259 |
switch (status) { |
|
260 |
case UP: |
|
261 |
return Messages.getString("ConnectionStatusReconciler_availableLabel"); //$NON-NLS-1$ |
|
262 |
case DOWN: |
|
263 |
return Messages.getString("ConnectionStatusReconciler_unavailableLabel"); //$NON-NLS-1$ |
|
264 |
case IN_USE: |
|
265 |
return Messages.getString("ConnectionStatusReconciler_inUseLabel_lower"); //$NON-NLS-1$ |
|
266 |
} |
|
267 |
return ""; //$NON-NLS-1$ |
|
268 |
} |
|
269 |
||
270 |
private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) { |
|
271 |
switch (serviceStatus) { |
|
272 |
case UP: |
|
273 |
return EConnectionStatus.READY; |
|
274 |
case DOWN: |
|
275 |
return EConnectionStatus.NOT_READY; |
|
276 |
case IN_USE: |
|
277 |
return EConnectionStatus.IN_USE; |
|
278 |
} |
|
279 |
return EConnectionStatus.NONE; |
|
280 |
} |
|
281 |
||
282 |
private void removeConnection(IConnection connection) { |
|
283 |
handledConnections.remove(connection); |
|
284 |
} |
|
285 |
||
286 |
private IConnection findConnection(IConnectedService cs) { |
|
287 |
for (IConnection connection : handledConnections) { |
|
288 |
for (IConnectedService connectedService : manager.getConnectedServices(connection)) { |
|
289 |
if (cs.equals(connectedService)) |
|
290 |
return connection; |
|
291 |
} |
|
292 |
} |
|
293 |
return null; |
|
294 |
} |
|
295 |
||
296 |
public void handleServiceStatusChange(IStatus status) { |
|
297 |
IConnectedService service = status.getConnectedService(); |
|
298 |
IConnection connection = findConnection(service); |
|
299 |
if (connection instanceof IConnection2) { |
|
300 |
reconcileStatus((IConnection2) connection); |
|
301 |
} |
|
302 |
if (connection != null) |
|
303 |
reconcileAsCurrent(connection); |
|
304 |
} |
|
305 |
||
306 |
} |