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