author | Matti Laitinen <matti.t.laitinen@nokia.com> |
Thu, 04 Mar 2010 13:38:40 +0200 | |
changeset 7 | 018264c44c89 |
parent 0 | 61163b28edca |
child 9 | e67492608de0 |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* Copyright (c) 2009 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 "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.s60tools.hticonnection.core; |
|
19 |
||
20 |
import java.util.ArrayList; |
|
21 |
import java.util.Collection; |
|
22 |
import java.util.List; |
|
23 |
import java.util.concurrent.ExecutionException; |
|
24 |
||
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
25 |
import org.eclipse.core.runtime.CoreException; |
0 | 26 |
import org.eclipse.swt.SWT; |
27 |
import org.eclipse.swt.widgets.Display; |
|
28 |
import org.eclipse.ui.PartInitException; |
|
29 |
||
30 |
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
|
31 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService; |
|
32 |
import com.nokia.carbide.remoteconnections.interfaces.IConnection; |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
33 |
import com.nokia.carbide.remoteconnections.interfaces.IService; |
0 | 34 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus; |
35 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus; |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
36 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
37 |
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.ISelectedConnectionInfo; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
38 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
39 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
40 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
41 |
import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus; |
0 | 42 |
import com.nokia.s60tools.hticonnection.HtiApiActivator; |
43 |
import com.nokia.s60tools.hticonnection.actions.OpenPreferencePageAction; |
|
44 |
import com.nokia.s60tools.hticonnection.connection.HTIConnectedService; |
|
45 |
import com.nokia.s60tools.hticonnection.connection.HTIConnectionStatus; |
|
46 |
import com.nokia.s60tools.hticonnection.connection.HTIService; |
|
47 |
import com.nokia.s60tools.hticonnection.gateway.DataGatewayManager; |
|
48 |
import com.nokia.s60tools.hticonnection.listener.HtiConnectionManager; |
|
49 |
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferenceConstants; |
|
50 |
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferencePage; |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
51 |
import com.nokia.s60tools.hticonnection.preferences.HtiApiPreferences; |
0 | 52 |
import com.nokia.s60tools.hticonnection.resources.Messages; |
53 |
import com.nokia.s60tools.hticonnection.services.HTIVersion; |
|
54 |
import com.nokia.s60tools.hticonnection.ui.dialogs.ErrorDialogWithHelp; |
|
55 |
import com.nokia.s60tools.hticonnection.ui.dialogs.HtiApiMessageBox; |
|
56 |
import com.nokia.s60tools.hticonnection.ui.views.main.MainView; |
|
57 |
import com.nokia.s60tools.util.cmdline.UnsupportedOSException; |
|
58 |
||
59 |
/** |
|
60 |
* This class manages current connection and holds it status information. |
|
61 |
*/ |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
62 |
public class HtiConnection { |
0 | 63 |
|
64 |
/** |
|
65 |
* Status of current connection. Used to device if new requests should |
|
66 |
* be accepted. Connection is in shutdown status when plug-in is started. |
|
67 |
*/ |
|
68 |
private ConnectionStatus currentStatus = ConnectionStatus.SHUTDOWN; |
|
69 |
||
70 |
/** |
|
71 |
* Thread is used to test connection when connection has been started. |
|
72 |
*/ |
|
73 |
private ConnectionCheckerThread connectionChecker = null; |
|
74 |
||
75 |
/** |
|
76 |
* Instance of this singleton class. |
|
77 |
*/ |
|
78 |
private static HtiConnection instance = null; |
|
79 |
||
80 |
/** |
|
81 |
* HTI version to which HTI Connection is currently connected. |
|
82 |
*/ |
|
83 |
private HTIVersion version = null; |
|
84 |
||
85 |
/** |
|
86 |
* Currently used connection. |
|
87 |
*/ |
|
88 |
private IConnection currentConnection = null; |
|
89 |
||
90 |
/** |
|
91 |
* Stores instance of data gateway, that is used for communication between HTI API and device. |
|
92 |
*/ |
|
93 |
private DataGatewayManager gatewayManager = null; |
|
94 |
||
95 |
/** |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
96 |
* Listener for listening changes in connections. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
97 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
98 |
private IConnectionListener connectionListener = null; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
99 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
100 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
101 |
* Listener for listening changes in current connection status. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
102 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
103 |
private IConnectionStatusChangedListener currentConnectionStatusListener = null; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
104 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
105 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
106 |
* To point right connection when IConnectionStatusChangedListener is added and removed. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
107 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
108 |
private IConnection currentlyOpenConnection = null; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
109 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
110 |
/** |
0 | 111 |
* Enumeration for status of the current connection. |
112 |
*/ |
|
113 |
public enum ConnectionStatus { |
|
114 |
SHUTDOWN, // Connection has been shut down. |
|
115 |
TESTING, // Testing connection. Used when testing connection from preferences. |
|
116 |
CONNECTING, // Connection is just started or there have been problems in connection. |
|
117 |
CONNECTED // Connection is working. |
|
118 |
}; |
|
119 |
||
120 |
/** |
|
121 |
* Private constructor to prevent creating new instances. |
|
122 |
*/ |
|
123 |
private HtiConnection(){ |
|
124 |
gatewayManager = new DataGatewayManager(); |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Only one instance can exist at one time. |
|
129 |
* @return Current instance. |
|
130 |
*/ |
|
131 |
public static synchronized HtiConnection getInstance(){ |
|
132 |
if( instance == null ){ |
|
133 |
instance = new HtiConnection(); |
|
134 |
} |
|
135 |
return instance; |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Initializes listeners and settings. |
|
140 |
*/ |
|
141 |
public void init() { |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
142 |
// Add listeners |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
143 |
// Create "current connection" listener |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
144 |
connectionListener = new ConnectionChangedListener(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
145 |
RemoteConnectionsActivator.getConnectionsManager().addConnectionListener(connectionListener); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
146 |
|
0 | 147 |
currentConnection = HtiApiActivator.getPreferences().getCurrentConnection(); |
148 |
} |
|
149 |
||
150 |
/** |
|
151 |
* Stops thread that checks if connection is up. |
|
152 |
*/ |
|
153 |
public void stop() { |
|
154 |
setConnectionStatus(ConnectionStatus.SHUTDOWN); |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
155 |
// Remove listeners |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
156 |
RemoteConnectionsActivator.getConnectionsManager().removeConnectionListener(connectionListener); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
157 |
|
0 | 158 |
connectionChecker = null; |
159 |
||
160 |
if(gatewayManager != null){ |
|
161 |
stopConnection(); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Starts connection with current settings. |
|
167 |
* @param isTesting True if connection is started only to test connection settings. |
|
168 |
* @return True if connection was started successfully. False otherwise. |
|
169 |
*/ |
|
170 |
public boolean startConnection(boolean isTesting) { |
|
171 |
IConnection connection = getCurrentConnection(); |
|
172 |
if(gatewayManager.isReady()) { |
|
173 |
// Old gateway needs to be stopped before starting new. |
|
174 |
stopConnection(); |
|
175 |
} |
|
176 |
||
177 |
if(connection == null) { |
|
178 |
String msg; |
|
179 |
List<IConnection> htiConns = getHTIConnections(); |
|
180 |
if(htiConns.size() > 0) { |
|
181 |
msg = new String(Messages.getString("HtiConnection.ConnectionNotSelected_ErrMsg")); //$NON-NLS-1$ |
|
182 |
} else { |
|
183 |
msg = new String(Messages.getString("HtiConnection.NoConnections_ErrMsg")); //$NON-NLS-1$ |
|
184 |
} |
|
185 |
||
186 |
ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg); |
|
187 |
Display.getDefault().asyncExec(runnable); |
|
188 |
return false; |
|
189 |
} |
|
190 |
||
191 |
// Connection status needs to be set before starting gateway, so that connection is set to use. |
|
192 |
ConnectionStatus status = (isTesting) ? ConnectionStatus.TESTING : ConnectionStatus.CONNECTING; |
|
193 |
setConnectionStatus(status); |
|
194 |
||
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
195 |
boolean result = startGateway(connection, isTesting); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
196 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
197 |
if (result) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
198 |
currentlyOpenConnection = connection; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
199 |
// Add status listener to the connection |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
200 |
if (currentlyOpenConnection instanceof IConnection2) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
201 |
currentConnectionStatusListener = new ConnectionStatusListener(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
202 |
((IConnection2) currentlyOpenConnection) |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
203 |
.addStatusChangedListener(currentConnectionStatusListener); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
204 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
205 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
206 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
207 |
return result; |
0 | 208 |
} |
209 |
||
210 |
/** |
|
211 |
* Starts datagateway with current settings. |
|
212 |
* @param isTesting True if datagateway is started only to test connection settings. |
|
213 |
* @return True if datagateway was started successfully. False otherwise. |
|
214 |
*/ |
|
215 |
private boolean startGateway(IConnection connection, boolean isTesting) { |
|
216 |
try { |
|
217 |
boolean started = false; |
|
218 |
// Connection status needs to be checked here, because setting connection status can take a while, |
|
219 |
// when connection is set in to use. User could have stopped the connection during that time. |
|
220 |
if(getConnectionStatus() != ConnectionStatus.SHUTDOWN) { |
|
221 |
// Connection string needs to be updated, before starting connection. |
|
222 |
// Otherwise old information might be shown. |
|
223 |
started = gatewayManager.startGateway(connection, isTesting); |
|
224 |
} |
|
225 |
||
226 |
if(started && !isTesting) { |
|
227 |
// Connection needs to be tested after datagateway has been started. |
|
228 |
connectionChecker = new ConnectionCheckerThread(); |
|
229 |
connectionChecker.start(); |
|
230 |
} |
|
231 |
else if(!started) { |
|
232 |
setConnectionStatus(ConnectionStatus.SHUTDOWN); |
|
233 |
} |
|
234 |
||
235 |
return started; |
|
236 |
||
237 |
} catch (UnsupportedOSException e) { |
|
238 |
Runnable runnable = new Runnable() { |
|
239 |
public void run() { |
|
240 |
// Only Windows operating system is supported. |
|
241 |
String errMsg = Messages.getString("HtiConnection.FailedToStartGateway_ErrMsg"); //$NON-NLS-1$ |
|
242 |
HtiApiMessageBox msgBox = new HtiApiMessageBox(errMsg, SWT.ICON_ERROR | SWT.OK); |
|
243 |
msgBox.open(); |
|
244 |
} |
|
245 |
||
246 |
}; |
|
247 |
// Messagebox needs to be opened from UI thread. |
|
248 |
Display.getDefault().asyncExec(runnable); |
|
249 |
||
250 |
return false; |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Stops datagateway. |
|
256 |
*/ |
|
257 |
public void stopConnection() { |
|
258 |
setConnectionStatus(ConnectionStatus.SHUTDOWN); |
|
259 |
gatewayManager.stopGateway(); |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
260 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
261 |
// Remove status listener from the connection |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
262 |
if ((currentlyOpenConnection instanceof IConnection2) && |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
263 |
(currentConnectionStatusListener != null)) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
264 |
((IConnection2) currentlyOpenConnection) |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
265 |
.removeStatusChangedListener(currentConnectionStatusListener); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
266 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
267 |
currentlyOpenConnection = null; |
0 | 268 |
} |
269 |
||
270 |
/** |
|
271 |
* Tests given connection. Shuts down existing connection. |
|
272 |
* This method is synchronized, because there can come multiple test |
|
273 |
* request at the same time from Remote Connections. |
|
274 |
*/ |
|
275 |
public synchronized IStatus testConnection(IConnection testConnection) { |
|
276 |
||
277 |
IConnection origConn = getCurrentConnection(); |
|
278 |
||
279 |
EStatus testStatus = EStatus.UNKNOWN; |
|
280 |
String shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$ |
|
281 |
String longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$ |
|
282 |
||
283 |
try { |
|
284 |
// Existing gateway needs to be stopped. |
|
285 |
stopConnection(); |
|
286 |
||
287 |
// New connection should be defined. |
|
288 |
setCurrentConnection(testConnection, true); |
|
289 |
||
290 |
// Gateway can be started now. |
|
291 |
if(startConnection(true)) { |
|
292 |
if(RequestQueueManager.getInstance().testConnection()) { |
|
293 |
HTIVersion version = getHTIVersion(); |
|
294 |
testStatus = EStatus.UP; |
|
295 |
shortDesc = Messages.getString("HtiConnection.Connected_StatusMsg"); //$NON-NLS-1$ |
|
296 |
longDesc = Messages.getString("HtiConnection.Connected_LongStatusMsg") + version.toString(); //$NON-NLS-1$ |
|
297 |
} else { |
|
298 |
testStatus = EStatus.DOWN; |
|
299 |
shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$ |
|
300 |
longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$ |
|
301 |
} |
|
302 |
} else { |
|
303 |
testStatus = EStatus.DOWN; |
|
304 |
shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$ |
|
305 |
longDesc = Messages.getString("HtiConnection.FailedToStartGateway_StatusMsg"); //$NON-NLS-1$ |
|
306 |
} |
|
307 |
||
308 |
} catch (Exception e) { |
|
309 |
if (e.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$ |
|
310 |
// HTI agent is not responding |
|
311 |
testStatus = EStatus.DOWN; |
|
312 |
shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$ |
|
313 |
longDesc = Messages.getString("HtiConnection.HtiNotResponding_StatusMsg"); //$NON-NLS-1$ |
|
314 |
} else { |
|
315 |
// Connection could not be created |
|
316 |
testStatus = EStatus.DOWN; |
|
317 |
shortDesc = Messages.getString("HtiConnection.NotConnected_StatusMsg"); //$NON-NLS-1$ |
|
318 |
longDesc = Messages.getString("HtiConnection.ConnectionFailed_StatusMsg"); //$NON-NLS-1$ |
|
319 |
} |
|
320 |
} finally { |
|
321 |
stopConnection(); |
|
322 |
setCurrentConnection(origConn, false); |
|
323 |
} |
|
324 |
||
325 |
return new HTIConnectionStatus(null, testStatus, shortDesc, longDesc); |
|
326 |
} |
|
327 |
||
328 |
/** |
|
329 |
* Set HTI version to which HTI Connection is currently connected. |
|
330 |
* @param version HTI version to which HTI Connection is currently connected. |
|
331 |
*/ |
|
332 |
public void setHTIVersion(HTIVersion version) { |
|
333 |
this.version = version; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Get HTI version to which HTI Connection is currently connected. |
|
338 |
* @return HTI version to which HTI Connection is currently connected, |
|
339 |
* or null if connection is down. |
|
340 |
*/ |
|
341 |
public HTIVersion getHTIVersion() { |
|
342 |
return version; |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Returns all connections which support connection types that HTI Connection can use. |
|
347 |
* @return List of connections that support connection types that HTI Connection can use. |
|
348 |
*/ |
|
349 |
public List<IConnection> getHTIConnections() { |
|
350 |
||
351 |
// Getting needed variables. |
|
352 |
Collection<IConnection> connections = RemoteConnectionsActivator.getConnectionsManager().getConnections(); |
|
353 |
Collection<String> connectionTypes = HTIService.getCompatibleConnectionTypeIds(); |
|
354 |
List<IConnection> htiConnections = new ArrayList<IConnection>(); |
|
355 |
||
356 |
// Going through all connection. |
|
357 |
for(IConnection conn : connections) { |
|
358 |
if(connectionTypes.contains(conn.getConnectionType().getIdentifier())) { |
|
359 |
htiConnections.add(conn); |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
return htiConnections; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Returns current status of connection. |
|
368 |
* @return Current status of connection. |
|
369 |
*/ |
|
370 |
public ConnectionStatus getConnectionStatus() { |
|
371 |
synchronized(currentStatus) { |
|
372 |
return currentStatus; |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Updates connection status in views description. |
|
378 |
*/ |
|
379 |
public void updateConnectionStatus() { |
|
380 |
Runnable updater = new ConnectionStatusUpdater(getCurrentConnection()); |
|
381 |
// Updating connection status asynchronously in default UI thread. |
|
382 |
Display.getDefault().asyncExec(updater); |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Sets current status of connection. |
|
387 |
* @param currentStatus The currentStatus to set. |
|
388 |
*/ |
|
389 |
public void setConnectionStatus(ConnectionStatus newStatus) { |
|
390 |
// Flagging status change information. |
|
391 |
boolean connectionStarted = false; |
|
392 |
boolean connectionTerminated = false; |
|
393 |
boolean connectionUp = false; |
|
394 |
boolean connectionDown = false; |
|
395 |
boolean needsUpdate = false; |
|
396 |
||
397 |
// Checking new status and updating current status. |
|
398 |
synchronized(currentStatus) { |
|
399 |
if (newStatus == ConnectionStatus.SHUTDOWN |
|
400 |
&& currentStatus != ConnectionStatus.SHUTDOWN) { |
|
401 |
// Shutting down the connection and connection tester. |
|
402 |
connectionChecker = null; |
|
403 |
connectionDown = true; |
|
404 |
} |
|
405 |
else if (newStatus == ConnectionStatus.CONNECTED |
|
406 |
&& currentStatus != ConnectionStatus.CONNECTED) { |
|
407 |
connectionUp = true; |
|
408 |
} |
|
409 |
else if (newStatus != ConnectionStatus.CONNECTED |
|
410 |
&& currentStatus == ConnectionStatus.CONNECTED) { |
|
411 |
connectionDown = true; |
|
412 |
} |
|
413 |
||
414 |
// Checking if it is needed to start/stop using current connection. |
|
415 |
if (newStatus != ConnectionStatus.SHUTDOWN |
|
416 |
&& currentStatus == ConnectionStatus.SHUTDOWN) { |
|
417 |
connectionStarted = true; |
|
418 |
} |
|
419 |
else if (newStatus == ConnectionStatus.SHUTDOWN |
|
420 |
&& currentStatus != ConnectionStatus.SHUTDOWN) { |
|
421 |
connectionTerminated = true; |
|
422 |
} |
|
423 |
||
424 |
needsUpdate = (currentStatus != newStatus); |
|
425 |
currentStatus = newStatus; |
|
426 |
} // End synchronized |
|
427 |
||
428 |
// Updating and informing outside of the synchronized block to prevent |
|
429 |
// synchronization slowing down connection status queries. |
|
430 |
||
431 |
if(needsUpdate) { |
|
432 |
updateConnectionStatus(); |
|
433 |
} |
|
434 |
||
435 |
if(connectionUp) { |
|
436 |
// Inform listeners of starting the connection |
|
437 |
HtiConnectionManager.getInstance().informConnectionStarted(); |
|
438 |
} |
|
439 |
else if(connectionDown) { |
|
440 |
// Inform listeners of connection termination |
|
441 |
HtiConnectionManager.getInstance().informConnectionTerminated(); |
|
442 |
setHTIVersion(null); |
|
443 |
RequestQueueManager.getInstance().cancelRequestsInQueue(); |
|
444 |
} |
|
445 |
||
446 |
if(connectionStarted) { |
|
447 |
// Connection needs to be set as used, before starting datagateway. |
|
448 |
// This must be done as last operation, because useConnection can take some time and |
|
449 |
// it is good to update other components first to prevent information lagging behind. |
|
450 |
IConnection conn = getCurrentConnection(); |
|
451 |
if(conn != null) { |
|
452 |
conn.useConnection(true); |
|
453 |
} |
|
454 |
} |
|
455 |
else if(connectionTerminated) { |
|
456 |
// Connection is not needed anymore. |
|
457 |
IConnection conn = getCurrentConnection(); |
|
458 |
if(conn != null) { |
|
459 |
conn.useConnection(false); |
|
460 |
} |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
/** |
|
465 |
* Shows message to user if exception is caused by communication problems. |
|
466 |
* This method also changes connection state to CONNECTNG, so service won't accept further requests. |
|
467 |
* @param exception Exception to be checked and reported. |
|
468 |
* @return Returns true if error was reported and connection is set to CONNECTING state, false otherwise. |
|
469 |
*/ |
|
470 |
public boolean reportConnectionError(ExecutionException exception) { |
|
471 |
||
472 |
String msg; |
|
473 |
||
474 |
// Checking if there are problems with connection and handling problems. |
|
475 |
if (exception.getMessage().equals("com.nokia.HTI.HTIException: HTI NOT INITIALIZED")) { //$NON-NLS-1$ |
|
476 |
msg = Messages.getString("HtiConnection.HtiNotResponding_MsgBoxMsg"); //$NON-NLS-1$ |
|
477 |
} |
|
478 |
else if( exception.getMessage().equals("java.net.ConnectException: Connection refused: connect")) { //$NON-NLS-1$ |
|
479 |
msg = Messages.getString("HtiConnection.ConnectionFailed_MsgBoxMsg"); //$NON-NLS-1$ |
|
480 |
} |
|
481 |
else { |
|
482 |
// HTI was initialized correctly and connection was established. |
|
483 |
// So this shouldn't be connection issue and no need to change to CONNECTING state. |
|
484 |
return false; |
|
485 |
} |
|
486 |
||
487 |
if(HtiConnection.getInstance().getConnectionStatus() != ConnectionStatus.SHUTDOWN) { |
|
488 |
// Setting connection status to testing state here to prevent extra error messages. |
|
489 |
HtiConnection.getInstance().setConnectionStatus(ConnectionStatus.CONNECTING); |
|
490 |
} |
|
491 |
||
492 |
ErrorDialogRunnable runnable = new ErrorDialogRunnable(msg); |
|
493 |
Display.getDefault().asyncExec(runnable); |
|
494 |
return true; |
|
495 |
} |
|
496 |
||
497 |
/** |
|
498 |
* Sets currently used connection. Saves connection to preferences, if it isn't a test connection. |
|
499 |
* @param currentConnection Connection to be set. |
|
500 |
* @param isTestConnection True if connection is set for test purposes. False otherwise. |
|
501 |
*/ |
|
502 |
public synchronized void setCurrentConnection(IConnection connection, boolean isTestConnection) { |
|
503 |
if(currentConnection != null && currentConnection != connection) { |
|
504 |
// Connection can be set as not used, because HTI Connection uses only currentConnection. |
|
505 |
currentConnection.useConnection(false); |
|
506 |
} |
|
507 |
this.currentConnection = connection; |
|
508 |
||
509 |
if(!isTestConnection) { |
|
510 |
// ID of normal connection need to be saved to preferences so that it will be used later as default. |
|
511 |
String connectionID = (currentConnection == null) ? HtiApiPreferenceConstants.DEFAULT_CONNECTION_ID |
|
512 |
: currentConnection.getIdentifier(); |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
513 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
514 |
// Set only if NOT already set as same or NOT set as "current connection". |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
515 |
if ((!connectionID.equals(HtiApiActivator.getPreferences().getConnectionID())) && |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
516 |
(!HtiApiPreferences.SELECTION_ID_CURRENT.equals(HtiApiActivator.getPreferences().getConnectionID()))) { |
0 | 517 |
HtiApiActivator.getPreferences().setConnectionID(connectionID); |
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Gets currently used connection. |
|
524 |
* @return Currently used connection, or <code>null</code> if connection has not been set. |
|
525 |
*/ |
|
526 |
public synchronized IConnection getCurrentConnection() { |
|
527 |
return currentConnection; |
|
528 |
} |
|
529 |
||
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
530 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
531 |
* Call when connection changes. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
532 |
* @param newConnection new connection |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
533 |
* @throws CoreException |
0 | 534 |
*/ |
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
535 |
public void changeConnection(String newConnection) throws CoreException { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
536 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
537 |
String currentConnectionId = null; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
538 |
if (getCurrentConnection() != null) currentConnectionId = getCurrentConnection().getIdentifier(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
539 |
boolean isConnected = getConnectionStatus() != ConnectionStatus.SHUTDOWN; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
540 |
boolean isNewConnection = (newConnection != null) && !(newConnection.equals(currentConnectionId)); |
0 | 541 |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
542 |
// Ensure that connection is ok. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
543 |
IService service = RemoteConnectionsActivator.getConnectionTypeProvider().findServiceByID(HTIService.ID); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
544 |
ISelectedConnectionInfo connectionInfo; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
545 |
connectionInfo = RemoteConnectionsActivator.getConnectionsManager().ensureConnection(newConnection, service); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
546 |
IConnection connection = connectionInfo.getConnection(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
547 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
548 |
if (isConnected && isNewConnection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
549 |
// Connection has been changed when connection is started. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
550 |
restartDataGateway(connection, false); |
0 | 551 |
} |
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
552 |
else if(!isConnected || currentConnection == null) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
553 |
// Connection isn't active or connections have been deleted. New connection can be set. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
554 |
setCurrentConnection(connection, false); |
0 | 555 |
} |
556 |
} |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
557 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
558 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
559 |
* Call when connection changes. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
560 |
* @param newConnection new connection |
0 | 561 |
*/ |
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
562 |
public void changeConnection(IConnection newConnection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
563 |
boolean isConnected = getConnectionStatus() != ConnectionStatus.SHUTDOWN; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
564 |
boolean isNewConnection = |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
565 |
(newConnection != null) && |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
566 |
!(newConnection.equals(getCurrentConnection())); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
567 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
568 |
if (isConnected && isNewConnection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
569 |
// Connection has been changed when connection is started. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
570 |
restartDataGateway(newConnection, false); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
571 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
572 |
else if(!isConnected || currentConnection == null) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
573 |
// Connection isn't active or connections have been deleted. New connection can be set. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
574 |
setCurrentConnection(newConnection, false); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
575 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
576 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
577 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
578 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
579 |
* Restarts Datagateway with settings from preference store. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
580 |
* @param isTesting True if testing connection and not trying to establish permanent connection. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
581 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
582 |
public boolean restartDataGateway(IConnection conn, boolean isTesting) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
583 |
stopConnection(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
584 |
setCurrentConnection(conn, false); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
585 |
return startConnection(isTesting); |
0 | 586 |
} |
587 |
||
588 |
// |
|
589 |
// Private classes. |
|
590 |
// |
|
591 |
||
592 |
/** |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
593 |
* Listener for listening changes in current connection status. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
594 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
595 |
private class ConnectionStatusListener implements IConnectionStatusChangedListener { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
596 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
597 |
public void statusChanged(IConnectionStatus status) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
598 |
// Stop connection when current connection device is removed. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
599 |
if (status.getEConnectionStatus().equals(EConnectionStatus.IN_USE_DISCONNECTED)) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
600 |
stopConnection(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
601 |
setCurrentConnection(null, false); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
602 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
603 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
604 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
605 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
606 |
/** |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
607 |
* Listener for listening changes in connections. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
608 |
*/ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
609 |
private class ConnectionChangedListener implements IConnectionListener { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
610 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
611 |
public void connectionAdded(IConnection connection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
612 |
// Do nothing. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
613 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
614 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
615 |
public void connectionRemoved(IConnection connection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
616 |
// Initializing needed variables. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
617 |
IConnection currConn = getCurrentConnection(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
618 |
if(currConn == null) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
619 |
// Nothing to do, if no connection is selected. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
620 |
return; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
621 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
622 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
623 |
// Checking if current connection was the removed one. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
624 |
if (currConn.getIdentifier().equals(connection.getIdentifier())) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
625 |
//If so, remove it and stop connection. |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
626 |
stopConnection(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
627 |
setCurrentConnection(null, false); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
628 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
629 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
630 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
631 |
public void currentConnectionSet(IConnection connection) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
632 |
if (HtiApiActivator.getPreferences().getConnectionID().equals(HtiApiPreferences.SELECTION_ID_CURRENT)) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
633 |
changeConnection(connection); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
634 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
635 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
636 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
637 |
}; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
638 |
|
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
639 |
/** |
0 | 640 |
* Thread that tests connection. |
641 |
* Connection needs to be tested when new connection has been started |
|
642 |
* or there has been problems with current connection. |
|
643 |
*/ |
|
644 |
private class ConnectionCheckerThread extends Thread { |
|
645 |
||
646 |
/** |
|
647 |
* Time in ms that is waited between testing the connection. |
|
648 |
*/ |
|
649 |
private static final long TEST_DELAY = 1000; |
|
650 |
||
651 |
/** |
|
652 |
* Keeps track for first connection try. This is needed if there |
|
653 |
* happens connection error when testing connection first time. |
|
654 |
* On other times connection failure information comes from outside |
|
655 |
* of this class. |
|
656 |
*/ |
|
657 |
private boolean firstConnection = true; |
|
658 |
||
659 |
/* (non-Javadoc) |
|
660 |
* @see java.lang.Thread#run() |
|
661 |
*/ |
|
662 |
public void run() { |
|
663 |
Thread thisThread = Thread.currentThread(); |
|
664 |
while(thisThread == connectionChecker) { |
|
665 |
||
666 |
if(getConnectionStatus() == ConnectionStatus.CONNECTING) { |
|
667 |
testConnection(); |
|
668 |
} |
|
669 |
||
670 |
try { |
|
671 |
Thread.sleep(TEST_DELAY); |
|
672 |
} catch (InterruptedException e) { |
|
673 |
// Not needed to handle. Just continuing polling in this case. |
|
674 |
} |
|
675 |
} |
|
676 |
} |
|
677 |
||
678 |
/** |
|
679 |
* Tests if current connection works and modifies connection status |
|
680 |
* in HtiApiActivator class if needed. |
|
681 |
*/ |
|
682 |
private void testConnection() { |
|
683 |
try { |
|
684 |
boolean connectionUp = RequestQueueManager.getInstance().testConnection(); |
|
685 |
||
686 |
if(connectionUp) { |
|
687 |
setConnectionStatus(ConnectionStatus.CONNECTED); |
|
688 |
} |
|
689 |
||
690 |
} catch (ExecutionException e) { |
|
691 |
if(firstConnection && getConnectionStatus() != ConnectionStatus.SHUTDOWN) { |
|
692 |
// Other than first connection errors are reported from RequestQueueManager. |
|
693 |
// This handles only first connection problem when trying to connect to the device. |
|
694 |
reportConnectionError(e); |
|
695 |
} |
|
696 |
} catch (Exception e) { |
|
697 |
// Catching other errors that doesn't need any specific actions. |
|
698 |
} |
|
699 |
||
700 |
firstConnection = false; |
|
701 |
} |
|
702 |
} |
|
703 |
||
704 |
/** |
|
705 |
* Updates connection status and buttons in UI. |
|
706 |
*/ |
|
707 |
private class ConnectionStatusUpdater implements Runnable { |
|
708 |
||
709 |
/** |
|
710 |
* Connection that needs status update. |
|
711 |
*/ |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
712 |
private final IConnection connectionToUpdate; |
0 | 713 |
|
714 |
/** |
|
715 |
* Constructor. |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
716 |
* @param connectionToUpdate Connection that needs status update. |
0 | 717 |
*/ |
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
718 |
public ConnectionStatusUpdater(IConnection connectionToUpdate) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
719 |
this.connectionToUpdate = connectionToUpdate; |
0 | 720 |
} |
721 |
||
722 |
/* (non-Javadoc) |
|
723 |
* @see java.lang.Runnable#run() |
|
724 |
*/ |
|
725 |
public void run() { |
|
726 |
||
727 |
// Connection status needs to be updated in connected service. |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
728 |
if(connectionToUpdate != null) { |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
729 |
Collection<IConnectedService> services = RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connectionToUpdate); |
0 | 730 |
// Collection can be null if getting services for test connection. |
731 |
if(services != null) { |
|
732 |
for(IConnectedService service : services) { |
|
733 |
if(service instanceof HTIConnectedService) { |
|
734 |
((HTIConnectedService)service).refreshStatus(); |
|
735 |
} |
|
736 |
} |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
try { |
|
741 |
MainView view = MainView.getViewInstance(false); |
|
742 |
view.updateActionButtonStates(); |
|
743 |
||
744 |
// Updating connection description. |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
745 |
String displayName = ""; |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
746 |
if(connectionToUpdate != null){ |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
747 |
displayName = connectionToUpdate.getDisplayName(); |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
748 |
} |
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
749 |
|
0 | 750 |
switch (getConnectionStatus()) { |
751 |
case CONNECTED: |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
752 |
view.updateDescription(Messages.getString("HtiConnection.Connected_ToolBar_Msg") + displayName); //$NON-NLS-1$ |
0 | 753 |
break; |
754 |
case CONNECTING: |
|
7
018264c44c89
HTI Connection v1.3.1 - Supporting Carbide 2.5
Matti Laitinen <matti.t.laitinen@nokia.com>
parents:
0
diff
changeset
|
755 |
view.updateDescription(Messages.getString("HtiConnection.Connecting_ToolBar_Msg") + displayName); //$NON-NLS-1$ |
0 | 756 |
break; |
757 |
case SHUTDOWN: |
|
758 |
view.updateDescription(Messages.getString("HtiConnection.NotConnected_ToolBar_Msg")); //$NON-NLS-1$ |
|
759 |
break; |
|
760 |
case TESTING: |
|
761 |
view.updateDescription(Messages.getString("HtiConnection.Testing_ToolBar_Msg")); //$NON-NLS-1$ |
|
762 |
break; |
|
763 |
} |
|
764 |
} catch (PartInitException e) { |
|
765 |
// No need to handle. Description doesn't need update if part is not visible. |
|
766 |
} |
|
767 |
} |
|
768 |
} |
|
769 |
||
770 |
/** |
|
771 |
* Runnable that opens error message box. |
|
772 |
*/ |
|
773 |
private class ErrorDialogRunnable implements Runnable { |
|
774 |
||
775 |
/** |
|
776 |
* Error message. |
|
777 |
*/ |
|
778 |
private final String errorMessage; |
|
779 |
||
780 |
/** |
|
781 |
* Constructor. |
|
782 |
* @param errorMessage Message to be shown in dialog. |
|
783 |
*/ |
|
784 |
public ErrorDialogRunnable(String errorMessage) { |
|
785 |
this.errorMessage = errorMessage; |
|
786 |
} |
|
787 |
||
788 |
/* (non-Javadoc) |
|
789 |
* @see java.lang.Runnable#run() |
|
790 |
*/ |
|
791 |
public void run() { |
|
792 |
||
793 |
// No need to show error message if preferences page is already created. |
|
794 |
if(HtiApiPreferencePage.isCreated()) { |
|
795 |
// Showing plain error message if preferences page is open. |
|
796 |
ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(errorMessage, SWT.OK); |
|
797 |
msgBox.open(); |
|
798 |
} else { |
|
799 |
// Asking if user wants to manage preferences. |
|
800 |
String msgWithQuestionStr = errorMessage + Messages.getString("HtiConnection.ManageConnections_ErrMsg"); //$NON-NLS-1$ |
|
801 |
||
802 |
ErrorDialogWithHelp msgBox = new ErrorDialogWithHelp(msgWithQuestionStr, SWT.YES | SWT.NO); |
|
803 |
int result = msgBox.open(); |
|
804 |
if(result == SWT.YES && !HtiApiPreferencePage.isCreated()){ |
|
805 |
// Opening preferences if Yes is selected. |
|
806 |
OpenPreferencePageAction openPreferencesAction = new OpenPreferencePageAction(); |
|
807 |
openPreferencesAction.run(); |
|
808 |
} |
|
809 |
} |
|
810 |
} |
|
811 |
} |
|
812 |
} |