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