author | dadubrow |
Fri, 29 Jan 2010 15:43:40 -0600 | |
changeset 848 | 679300bad088 |
parent 770 | cb7e68ca1ef4 |
child 924 | 657e34ac671f |
permissions | -rw-r--r-- |
748 | 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 |
||
848
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
23 |
import org.eclipse.ui.PartInitException; |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
24 |
|
748 | 25 |
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
26 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService; |
|
27 |
import com.nokia.carbide.remoteconnections.interfaces.IConnection; |
|
28 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
|
29 |
import com.nokia.carbide.remoteconnections.interfaces.AbstractConnection.ConnectionStatus; |
|
30 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus; |
|
31 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener; |
|
32 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus; |
|
33 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener; |
|
34 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2; |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
35 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus; |
748 | 36 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus; |
749 | 37 |
import com.nokia.carbide.trk.support.Messages; |
748 | 38 |
import com.nokia.carbide.trk.support.connection.USBConnectionType; |
39 |
import com.nokia.carbide.trk.support.service.TRKConnectedService; |
|
40 |
import com.nokia.carbide.trk.support.service.TracingConnectedService; |
|
848
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
41 |
import com.nokia.cpp.internal.api.utils.ui.RunRunnableWhenWorkbenchVisibleJob; |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
42 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
748 | 43 |
|
44 |
/** |
|
45 |
* A singleton object that manages the device status of dynamic connections |
|
46 |
* based on the status of the TRK and Tracing services. |
|
47 |
*/ |
|
48 |
public class ConnectionStatusReconciler { |
|
49 |
||
848
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
50 |
private static final String CONNECTIONS_VIEW_ID = |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
51 |
"com.nokia.carbide.remoteconnections.view.ConnectionsView"; //$NON-NLS-1$ |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
52 |
|
748 | 53 |
private class ConnectionListener implements IConnectionListener { |
54 |
||
55 |
public void connectionAdded(IConnection connection) { |
|
56 |
addConnection(connection); |
|
57 |
} |
|
58 |
||
59 |
public void connectionRemoved(IConnection connection) { |
|
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
60 |
if (connection.equals(userSetCurrentConnection)) |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
61 |
userSetCurrentConnection = null; |
748 | 62 |
removeConnection(connection); |
63 |
} |
|
64 |
||
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
65 |
public void currentConnectionSet(IConnection connection) { |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
66 |
if (connection != null && !connection.equals(reconcilerSetCurrentConnection)) |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
67 |
userSetCurrentConnection = connection; |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
68 |
} |
748 | 69 |
|
70 |
} |
|
71 |
||
72 |
private class ServiceStatusListener implements IStatusChangedListener { |
|
73 |
||
74 |
public void statusChanged(IStatus status) { |
|
75 |
handleServiceStatusChange(status); |
|
76 |
} |
|
77 |
||
78 |
} |
|
79 |
||
80 |
private static ConnectionStatusReconciler instance; |
|
81 |
private IConnectionsManager manager; |
|
82 |
private IConnectionListener connectionListener; |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
83 |
private List<IConnection> handledConnections; |
748 | 84 |
private ServiceStatusListener serviceStatusListener; |
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
85 |
private IConnection reconcilerSetCurrentConnection; |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
86 |
private IConnection userSetCurrentConnection; |
748 | 87 |
|
88 |
private ConnectionStatusReconciler() { |
|
89 |
connectionListener = new ConnectionListener(); |
|
90 |
manager = RemoteConnectionsActivator.getConnectionsManager(); |
|
91 |
manager.addConnectionListener(connectionListener); |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
92 |
handledConnections = new ArrayList<IConnection>(); |
748 | 93 |
serviceStatusListener = new ServiceStatusListener(); |
94 |
} |
|
95 |
||
96 |
public static ConnectionStatusReconciler getInstance() { |
|
97 |
if (instance == null) |
|
98 |
instance = new ConnectionStatusReconciler(); |
|
99 |
||
100 |
return instance; |
|
101 |
} |
|
102 |
||
103 |
public void dispose() { |
|
104 |
manager.removeConnectionListener(connectionListener); |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
105 |
for (IConnection connection : new ArrayList<IConnection>(handledConnections)) { |
748 | 106 |
removeConnection(connection); |
107 |
} |
|
108 |
} |
|
109 |
||
752
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
110 |
private boolean isDynamic(IConnection connection) { |
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
111 |
return connection instanceof IConnection2 && ((IConnection2) connection).isDynamic(); |
748 | 112 |
} |
113 |
||
114 |
private boolean isSysTRK(TRKConnectedService service) { |
|
115 |
String value = service.getProperties().get(TRKConnectedService.PROP_SYS_TRK); |
|
752
a0609ea7401a
add sys trk checking for usb only and use with reconciler
dadubrow
parents:
750
diff
changeset
|
116 |
return Boolean.parseBoolean(value); |
748 | 117 |
} |
118 |
||
119 |
private void addConnection(IConnection connection) { |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
120 |
handledConnections.add(connection); |
748 | 121 |
for (IConnectedService service : manager.getConnectedServices(connection)) { |
122 |
if (service instanceof TRKConnectedService || |
|
123 |
service instanceof TracingConnectedService) { |
|
124 |
service.addStatusChangedListener(serviceStatusListener); |
|
125 |
} |
|
126 |
} |
|
848
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
127 |
showConnectionsView(); |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
128 |
} |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
129 |
|
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
130 |
private void showConnectionsView() { |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
131 |
RunRunnableWhenWorkbenchVisibleJob.start(new Runnable() { |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
132 |
public void run() { |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
133 |
// try to show the connections view to start service testers |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
134 |
try { |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
135 |
WorkbenchUtils.getView(CONNECTIONS_VIEW_ID); |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
136 |
} catch (PartInitException e) { |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
137 |
} |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
138 |
} |
679300bad088
Show connections view when new connection is added
dadubrow
parents:
770
diff
changeset
|
139 |
}); |
748 | 140 |
} |
141 |
||
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
142 |
private void reconcileAsCurrent(IConnection connection) { |
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
143 |
if (canBeSetToCurrent(connection)) { |
770 | 144 |
if (isReady(connection)) { // set as current |
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
145 |
reconcilerSetCurrentConnection = connection; |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
146 |
manager.setCurrentConnection(connection); |
770 | 147 |
} else if (isNotReady(connection) && connection.equals(manager.getCurrentConnection())) { |
148 |
// unset current or set something else current |
|
149 |
if (isDynamic(connection) && userSetCurrentConnection != null) { |
|
150 |
manager.setCurrentConnection(userSetCurrentConnection); |
|
151 |
} |
|
152 |
else { |
|
153 |
// look for some other existing connection that is ready |
|
154 |
for (IConnection c : manager.getConnections()) { |
|
155 |
if (canBeSetToCurrent(c) && isReady(c)) { |
|
156 |
reconcilerSetCurrentConnection = connection; |
|
157 |
manager.setCurrentConnection(connection); |
|
158 |
return; |
|
159 |
} |
|
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
160 |
} |
770 | 161 |
// set to no current connection |
162 |
manager.setCurrentConnection(null); |
|
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
163 |
} |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
164 |
} |
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
165 |
} |
758
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
166 |
} |
c3cf3704d340
Make setting default part of reconciler responsibility
dadubrow
parents:
752
diff
changeset
|
167 |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
168 |
private boolean isReady(IConnection connection) { |
770 | 169 |
return equalsConnectionStatus(connection, EConnectionStatus.READY); |
170 |
} |
|
171 |
||
172 |
private boolean isNotReady(IConnection connection) { |
|
173 |
return equalsConnectionStatus(connection, EConnectionStatus.NOT_READY); |
|
174 |
} |
|
175 |
||
176 |
private boolean equalsConnectionStatus(IConnection connection, EConnectionStatus status) { |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
177 |
if (connection instanceof IConnection2) { |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
178 |
IConnectionStatus connectionStatus = ((IConnection2) connection).getStatus(); |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
179 |
if (connectionStatus != null) |
770 | 180 |
return connectionStatus.getEConnectionStatus().equals(status); |
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
181 |
} |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
182 |
return false; |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
183 |
} |
770 | 184 |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
185 |
private boolean canBeSetToCurrent(IConnection connection) { |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
186 |
// USB connections for now |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
187 |
return USBConnectionType.ID.equals(connection.getConnectionType().getIdentifier()); |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
188 |
} |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
189 |
|
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
190 |
private void reconcileStatus(IConnection connection) { |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
191 |
if (!isDynamic(connection)) // don't set status for user generated connections |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
192 |
return; |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
193 |
|
748 | 194 |
boolean isSysTRK = false; |
195 |
EStatus trkStatus = EStatus.UNKNOWN; |
|
196 |
EStatus traceStatus = EStatus.UNKNOWN; |
|
197 |
for (IConnectedService service : manager.getConnectedServices(connection)) { |
|
198 |
if (service instanceof TRKConnectedService) { |
|
199 |
isSysTRK = isSysTRK((TRKConnectedService) service); |
|
200 |
trkStatus = service.getStatus().getEStatus(); |
|
201 |
} |
|
202 |
if (service instanceof TracingConnectedService) { |
|
203 |
traceStatus = service.getStatus().getEStatus(); |
|
204 |
} |
|
205 |
} |
|
206 |
setConnectionStatus((IConnection2) connection, isSysTRK, trkStatus, traceStatus); |
|
207 |
} |
|
208 |
||
209 |
private void setConnectionStatus(IConnection2 connection, boolean isSysTRK, EStatus trkStatus, EStatus traceStatus) { |
|
210 |
// use trk status |
|
211 |
EConnectionStatus connectionStatus = service2ConnectionStatus(trkStatus); |
|
212 |
// if sys trk, tracing also used |
|
213 |
if (isSysTRK && connectionStatus.equals(EConnectionStatus.READY)) { |
|
214 |
connectionStatus = service2ConnectionStatus(traceStatus); |
|
215 |
} |
|
216 |
||
217 |
String shortDesc = getShortDescriptionForStatus(connectionStatus); |
|
749 | 218 |
StringBuilder longDesc = new StringBuilder(Messages.getString("ConnectionStatusReconciler_TRKServicePrefix")); //$NON-NLS-1$ |
748 | 219 |
longDesc.append(getServiceStatusString(trkStatus)); |
220 |
if (isSysTRK) { |
|
749 | 221 |
longDesc.append(Messages.getString("ConnectionStatusReconciler_TracingServicePrefix")); //$NON-NLS-1$ |
748 | 222 |
longDesc.append(getServiceStatusString(traceStatus)); |
223 |
} |
|
224 |
||
225 |
connection.setStatus(new ConnectionStatus(connectionStatus, shortDesc, longDesc.toString())); |
|
226 |
} |
|
227 |
||
228 |
private String getShortDescriptionForStatus(EConnectionStatus connectionStatus) { |
|
229 |
switch (connectionStatus) { |
|
230 |
case READY: |
|
749 | 231 |
return Messages.getString("ConnectionStatusReconciler_ReadyLabel"); //$NON-NLS-1$ |
748 | 232 |
case NOT_READY: |
749 | 233 |
return Messages.getString("ConnectionStatusReconciler_NotReadyLabel"); //$NON-NLS-1$ |
748 | 234 |
case IN_USE: |
749 | 235 |
return Messages.getString("ConnectionStatusReconciler_InUseLabel"); //$NON-NLS-1$ |
748 | 236 |
case IN_USE_DISCONNECTED: |
749 | 237 |
return Messages.getString("ConnectionStatusReconciler_DisconnectedLabel"); //$NON-NLS-1$ |
748 | 238 |
} |
749 | 239 |
return ""; //$NON-NLS-1$ |
748 | 240 |
} |
241 |
||
242 |
private String getServiceStatusString(EStatus status) { |
|
243 |
switch (status) { |
|
244 |
case UP: |
|
749 | 245 |
return Messages.getString("ConnectionStatusReconciler_availableLabel"); //$NON-NLS-1$ |
748 | 246 |
case DOWN: |
749 | 247 |
return Messages.getString("ConnectionStatusReconciler_unavailableLabel"); //$NON-NLS-1$ |
748 | 248 |
case IN_USE: |
749 | 249 |
return Messages.getString("ConnectionStatusReconciler_inUseLabel_lower"); //$NON-NLS-1$ |
748 | 250 |
} |
749 | 251 |
return ""; //$NON-NLS-1$ |
748 | 252 |
} |
253 |
||
254 |
private EConnectionStatus service2ConnectionStatus(EStatus serviceStatus) { |
|
255 |
switch (serviceStatus) { |
|
256 |
case UP: |
|
257 |
return EConnectionStatus.READY; |
|
258 |
case DOWN: |
|
259 |
return EConnectionStatus.NOT_READY; |
|
260 |
case IN_USE: |
|
261 |
return EConnectionStatus.IN_USE; |
|
262 |
} |
|
263 |
return EConnectionStatus.NONE; |
|
264 |
} |
|
265 |
||
266 |
private void removeConnection(IConnection connection) { |
|
267 |
handledConnections.remove(connection); |
|
268 |
} |
|
269 |
||
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
270 |
private IConnection findConnection(IConnectedService cs) { |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
271 |
for (IConnection connection : handledConnections) { |
748 | 272 |
for (IConnectedService connectedService : manager.getConnectedServices(connection)) { |
273 |
if (cs.equals(connectedService)) |
|
274 |
return connection; |
|
275 |
} |
|
276 |
} |
|
277 |
return null; |
|
278 |
} |
|
279 |
||
280 |
public void handleServiceStatusChange(IStatus status) { |
|
281 |
IConnectedService service = status.getConnectedService(); |
|
763
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
282 |
IConnection connection = findConnection(service); |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
283 |
if (connection instanceof IConnection2) { |
751f5182b866
Refactor API and related from default connection to current + update heuristics
dadubrow
parents:
758
diff
changeset
|
284 |
reconcileStatus((IConnection2) connection); |
748 | 285 |
} |
764
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
286 |
if (connection != null) |
603d6ca035e8
updated set and reset current depending on ready state
dadubrow
parents:
763
diff
changeset
|
287 |
reconcileAsCurrent(connection); |
748 | 288 |
} |
289 |
||
290 |
} |