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.gateway;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.io.IOException;
|
|
22 |
|
|
23 |
import com.freescale.cdt.debug.cw.core.SerialConnectionSettings;
|
|
24 |
import com.nokia.carbide.remoteconnections.interfaces.IConnection;
|
|
25 |
import com.nokia.s60tools.hticonnection.HtiApiActivator;
|
|
26 |
import com.nokia.s60tools.hticonnection.common.ProductInfoRegistry;
|
|
27 |
import com.nokia.s60tools.hticonnection.connection.HTIService;
|
|
28 |
import com.nokia.s60tools.hticonnection.core.HtiConnection;
|
|
29 |
import com.nokia.s60tools.hticonnection.core.HtiConnection.ConnectionStatus;
|
|
30 |
import com.nokia.s60tools.hticonnection.resources.Messages;
|
|
31 |
import com.nokia.s60tools.hticonnection.util.HtiApiConsole;
|
|
32 |
import com.nokia.s60tools.util.cmdline.UnsupportedOSException;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* This class controls gateway.
|
|
36 |
*/
|
|
37 |
public class DataGatewayManager {
|
|
38 |
|
|
39 |
// Datagateway parameters.
|
|
40 |
private static final String COM_PORT_PARAMETER_NAME = "-COMPORT="; //$NON-NLS-1$
|
|
41 |
private static final String REMOTE_HOST_PARAMETER_NAME = "-REMOTE_HOST="; //$NON-NLS-1$
|
|
42 |
private static final String REMOTE_PORT_PARAMETER_NAME = "-REMOTE_PORT="; //$NON-NLS-1$
|
|
43 |
private static final String PORT_PARAMETER_NAME = "-port="; //$NON-NLS-1$
|
|
44 |
private static final String COMM_CHANNEL_PARAMETER = "-commchannel="; //$NON-NLS-1$
|
|
45 |
private static final String COMM_CHANNEL_SERIAL = "SERIAL"; //$NON-NLS-1$
|
|
46 |
private static final String COMM_CHANNEL_IP = "IPCOMM"; //$NON-NLS-1$
|
|
47 |
|
|
48 |
/**
|
|
49 |
* Class that holdes Datagateway.exe process that is running.
|
|
50 |
*/
|
|
51 |
private ProcessHolder holder = null;
|
|
52 |
/**
|
|
53 |
* This object is used to synchronize process holder.
|
|
54 |
* Separate synchronizer object is needed because holder object changes and can be null.
|
|
55 |
*/
|
|
56 |
private Object holderSynchronizer = new Object();
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Constructor.
|
|
60 |
*/
|
|
61 |
public DataGatewayManager() {
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Starts gateway with specified connection.
|
|
66 |
* @param connection Connection that is used to start gateway.
|
|
67 |
* @param isTesting True if datagateway is started only to test connection settings.
|
|
68 |
* @return True if succeeded to start datagateway. False otherwise.
|
|
69 |
* @throws UnsupportedOSException
|
|
70 |
*/
|
|
71 |
public boolean startGateway(IConnection connection, boolean isTesting) throws UnsupportedOSException {
|
|
72 |
try {
|
|
73 |
// Getting general parameters.
|
|
74 |
String path = HtiApiActivator.getDefault().getPluginInstallPath() +
|
|
75 |
File.separator + ProductInfoRegistry.getWin32BinariesRelativePath();
|
|
76 |
|
|
77 |
String[] cmd = getDatagatewayCommand(connection, path);
|
|
78 |
|
|
79 |
// Making sure that old gateway is stopped.
|
|
80 |
stopGateway();
|
|
81 |
|
|
82 |
// Starting datagateway. Each gateway has own class that keeps track of the process.
|
|
83 |
|
|
84 |
boolean started = false;
|
|
85 |
synchronized (holderSynchronizer) {
|
|
86 |
holder = new ProcessHolder();
|
|
87 |
started = holder.runAsyncCommand(cmd, path);
|
|
88 |
}
|
|
89 |
|
|
90 |
if(!started){
|
|
91 |
// Running command failed. Possible errors are reported in processCreated callback.
|
|
92 |
HtiConnection.getInstance().setConnectionStatus(ConnectionStatus.SHUTDOWN);
|
|
93 |
return false;
|
|
94 |
}
|
|
95 |
|
|
96 |
// Started successfully.
|
|
97 |
return true;
|
|
98 |
} catch (IOException e) {
|
|
99 |
HtiApiConsole.getInstance().println(
|
|
100 |
Messages.getString("DataGatewayManager.Start_Datagateway_Failed_ConsoleMsg"), //$NON-NLS-1$
|
|
101 |
HtiApiConsole.MSG_ERROR);
|
|
102 |
} catch (Exception e) {
|
|
103 |
HtiApiConsole.getInstance().println(Messages.getString("DataGatewayManager.Start_Datagateway_Failed_Reason_ConsoleMsg") //$NON-NLS-1$
|
|
104 |
+ e.getMessage(), HtiApiConsole.MSG_ERROR);
|
|
105 |
}
|
|
106 |
|
|
107 |
// Exception was thrown. Failed to start gateway.
|
|
108 |
return false;
|
|
109 |
}
|
|
110 |
|
|
111 |
/**
|
|
112 |
* Returns command that is used to run datagateway.
|
|
113 |
* @param connection Connection that is used to start gateway.
|
|
114 |
* @param path Path where datagateway is located.
|
|
115 |
* @return Command that is used to run datagateway.
|
|
116 |
*/
|
|
117 |
private String[] getDatagatewayCommand(IConnection connection, String path) {
|
|
118 |
|
|
119 |
// Getting general parameters.
|
|
120 |
String type = connection.getConnectionType().getIdentifier();
|
|
121 |
|
|
122 |
if(type.equals(HTIService.SERIAL_TYPE) ||
|
|
123 |
type.equals(HTIService.SERIAL_BT_TYPE) ||
|
|
124 |
type.equals(HTIService.USB_TYPE)) {
|
|
125 |
// Handling serial connections
|
|
126 |
|
|
127 |
String comPort = "COM" + connection.getSettings().get(SerialConnectionSettings.PORT); //$NON-NLS-1$
|
|
128 |
|
|
129 |
String[] cmd = new String[] {
|
|
130 |
path + File.separator + ProductInfoRegistry.getDatagatewayExeName(),
|
|
131 |
PORT_PARAMETER_NAME + DataGatewayConstants.PORT,
|
|
132 |
COMM_CHANNEL_PARAMETER + COMM_CHANNEL_SERIAL,
|
|
133 |
COM_PORT_PARAMETER_NAME+comPort};
|
|
134 |
|
|
135 |
return cmd;
|
|
136 |
|
|
137 |
}
|
|
138 |
else if(type.equals(HTIService.TCPIP_TYPE)) {
|
|
139 |
// Handling IP connections.
|
|
140 |
String remoteHost = connection.getSettings().get("ipAddress"); //$NON-NLS-1$
|
|
141 |
String remotePort = connection.getSettings().get(HTIService.ID);
|
|
142 |
|
|
143 |
String[] cmd = new String[] {
|
|
144 |
path + File.separator + ProductInfoRegistry.getDatagatewayExeName(),
|
|
145 |
PORT_PARAMETER_NAME + DataGatewayConstants.PORT,
|
|
146 |
COMM_CHANNEL_PARAMETER + COMM_CHANNEL_IP,
|
|
147 |
REMOTE_HOST_PARAMETER_NAME + remoteHost,
|
|
148 |
REMOTE_PORT_PARAMETER_NAME + remotePort };
|
|
149 |
|
|
150 |
return cmd;
|
|
151 |
|
|
152 |
}
|
|
153 |
else {
|
|
154 |
throw new RuntimeException(Messages.getString("DataGatewayManager.IncomptibleConnection_ExceptionMsg")); //$NON-NLS-1$
|
|
155 |
}
|
|
156 |
}
|
|
157 |
|
|
158 |
/**
|
|
159 |
* Stops currently running gateway, or does nothing if gateway is not running.
|
|
160 |
*/
|
|
161 |
public void stopGateway(){
|
|
162 |
synchronized (holderSynchronizer) {
|
|
163 |
if(holder != null) {
|
|
164 |
holder.stopProcess();
|
|
165 |
// This process holder is not needed anymore.
|
|
166 |
holder = null;
|
|
167 |
}
|
|
168 |
}
|
|
169 |
}
|
|
170 |
|
|
171 |
/**
|
|
172 |
* Checks if gateway is ready for use.
|
|
173 |
* @return Returns true if gateway is ready for use.
|
|
174 |
*/
|
|
175 |
public boolean isReady() {
|
|
176 |
synchronized (holderSynchronizer) {
|
|
177 |
if(holder == null) {
|
|
178 |
return false;
|
|
179 |
}
|
|
180 |
return holder.isReady();
|
|
181 |
}
|
|
182 |
}
|
|
183 |
}
|