trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/api/ConnectionHandler.java
changeset 11 5b9d4d8641ce
equal deleted inserted replaced
10:ed1c9f64298a 11:5b9d4d8641ce
       
     1 /*
       
     2  * Copyright (c) 2007-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 "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  * Connection handler handles the connecting and disconnecting
       
    17  *
       
    18  */
       
    19 package com.nokia.traceviewer.api;
       
    20 
       
    21 import java.util.List;
       
    22 
       
    23 import org.eclipse.jface.preference.IPreferenceStore;
       
    24 import org.eclipse.swt.widgets.Display;
       
    25 
       
    26 import com.nokia.traceviewer.TraceViewerPlugin;
       
    27 import com.nokia.traceviewer.action.ConnectAction;
       
    28 import com.nokia.traceviewer.api.TraceViewerAPI.TVAPIError;
       
    29 import com.nokia.traceviewer.engine.TraceProvider;
       
    30 import com.nokia.traceviewer.engine.TraceViewerGlobals;
       
    31 import com.nokia.traceviewer.engine.preferences.PreferenceConstants;
       
    32 import com.nokia.traceviewer.internal.api.TraceViewerAPI2Impl;
       
    33 
       
    34 /**
       
    35  * Connection handler handles the connecting and disconnecting
       
    36  * 
       
    37  */
       
    38 final class ConnectionHandler {
       
    39 
       
    40 	/**
       
    41 	 * Old connection method
       
    42 	 */
       
    43 	private String oldConnectionMethod;
       
    44 
       
    45 	/**
       
    46 	 * Old connection parameters
       
    47 	 */
       
    48 	private String[] oldParameters;
       
    49 
       
    50 	/**
       
    51 	 * Old connection ID that was selected.
       
    52 	 */
       
    53 	private String oldSelectedConnectionID;
       
    54 
       
    55 	/**
       
    56 	 * Connect using current settings defined in TraceViewer's preferences
       
    57 	 * 
       
    58 	 * @return error code from TraceViewerAPI
       
    59 	 */
       
    60 	public TVAPIError connect() {
       
    61 		TVAPIError errorcode = TVAPIError.NONE;
       
    62 
       
    63 		// Check if already connected
       
    64 		if (isAlreadyConnected()) {
       
    65 			errorcode = TVAPIError.ALREADY_CONNECTED;
       
    66 
       
    67 		} else {
       
    68 
       
    69 			// If view exists and we are in UI thread, do the connecting through
       
    70 			// the view action
       
    71 			if (TraceViewerGlobals.getTraceViewer().getView() != null
       
    72 					&& Display.getCurrent() != null) {
       
    73 				ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
    74 						.getTraceViewer().getView().getActionFactory()
       
    75 						.getConnectAction()));
       
    76 
       
    77 				errorcode = action.connect(false, true);
       
    78 
       
    79 				// Connect through engine
       
    80 			} else {
       
    81 				changeDataFormat();
       
    82 				boolean success = TraceViewerGlobals.getTraceViewer().connect();
       
    83 
       
    84 				if (!success) {
       
    85 					errorcode = TVAPIError.INVALID_CONNECTION_SETTINGS;
       
    86 				}
       
    87 
       
    88 				// If succeeded and view exists, change button
       
    89 				if (success
       
    90 						&& TraceViewerGlobals.getTraceViewer().getView() != null) {
       
    91 					ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
    92 							.getTraceViewer().getView().getActionFactory()
       
    93 							.getConnectAction()));
       
    94 					action.changeToDisconnectAction();
       
    95 				}
       
    96 			}
       
    97 		}
       
    98 		return errorcode;
       
    99 	}
       
   100 
       
   101 	/**
       
   102 	 * Connect using given parameters. Possible connection methods can be found
       
   103 	 * from TraceViewerAPI constants. Parameters should be given in this order:
       
   104 	 * When using TCP connection, parameters are IP address port number and
       
   105 	 * channel number in this order. When using USB serial connection, the only
       
   106 	 * parameter is the COM port number. This function will also set given
       
   107 	 * parameters to TraceViewer's connection preferences if the connecting
       
   108 	 * succeeds.
       
   109 	 * 
       
   110 	 * @param connectionMethod
       
   111 	 *            the connection method to use
       
   112 	 * @param parameters
       
   113 	 *            array of parameters
       
   114 	 * @return error code from TraceViewerAPI
       
   115 	 */
       
   116 	public TVAPIError connect(int connectionMethod, String[] parameters) {
       
   117 		TVAPIError errorcode = TVAPIError.NONE;
       
   118 
       
   119 		// Check if already connected
       
   120 		if (isAlreadyConnected()) {
       
   121 			errorcode = TVAPIError.ALREADY_CONNECTED;
       
   122 
       
   123 		} else {
       
   124 			// Save old connection preferences
       
   125 			saveConnectionPreferences();
       
   126 
       
   127 			// Set new connection preferences
       
   128 			boolean parametersOk = setNewConnectionPreferences(
       
   129 					connectionMethod, parameters);
       
   130 
       
   131 			if (parametersOk) {
       
   132 
       
   133 				// If view exists and we are in UI thread, do the connecting
       
   134 				// through the view action
       
   135 				if (TraceViewerGlobals.getTraceViewer().getView() != null
       
   136 						&& Display.getCurrent() != null) {
       
   137 					ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
   138 							.getTraceViewer().getView().getActionFactory()
       
   139 							.getConnectAction()));
       
   140 					errorcode = action.connect(false, false);
       
   141 
       
   142 					// Connect through engine
       
   143 				} else {
       
   144 					changeDataFormat();
       
   145 					boolean success = TraceViewerGlobals.getTraceViewer()
       
   146 							.connect();
       
   147 
       
   148 					if (success) {
       
   149 						errorcode = TVAPIError.NONE;
       
   150 					} else {
       
   151 						errorcode = TVAPIError.INVALID_CONNECTION_SETTINGS;
       
   152 					}
       
   153 				}
       
   154 
       
   155 				// Connecting failed
       
   156 				if (errorcode != TVAPIError.NONE) {
       
   157 
       
   158 					// Set old preferences back
       
   159 					setOldPreferencesBack();
       
   160 
       
   161 					// If succeeded and view exists, change button
       
   162 				} else if (TraceViewerGlobals.getTraceViewer().getView() != null) {
       
   163 					ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
   164 							.getTraceViewer().getView().getActionFactory()
       
   165 							.getConnectAction()));
       
   166 					action.changeToDisconnectAction();
       
   167 				}
       
   168 
       
   169 			} else {
       
   170 				// Set old preferences back
       
   171 				setOldPreferencesBack();
       
   172 
       
   173 				errorcode = TVAPIError.INVALID_CONNECTION_PARAMETERS;
       
   174 			}
       
   175 		}
       
   176 		return errorcode;
       
   177 	}
       
   178 
       
   179 	/**
       
   180 	 * Changes Data Format
       
   181 	 */
       
   182 	private void changeDataFormat() {
       
   183 
       
   184 		// Get the TraceViewer preferenceStore
       
   185 		IPreferenceStore store = TraceViewerPlugin.getDefault()
       
   186 				.getPreferenceStore();
       
   187 
       
   188 		// If there are more than one TraceProvider
       
   189 		if (TraceViewerGlobals.getListOfTraceProviders().size() > 1) {
       
   190 
       
   191 			String selectedConnectionType = store
       
   192 					.getString(PreferenceConstants.CONNECTION_TYPE);
       
   193 
       
   194 			// If the currently selected TraceProvider doesn't has selected
       
   195 			// connection as preferred, try to find one that does
       
   196 			if (!TraceViewerGlobals.getTraceProvider()
       
   197 					.getPreferredConnectionType()
       
   198 					.equals(selectedConnectionType)) {
       
   199 
       
   200 				List<TraceProvider> traceProviders = TraceViewerGlobals
       
   201 						.getListOfTraceProviders();
       
   202 
       
   203 				// Go through list of TraceProviders
       
   204 				for (int i = 0; i < traceProviders.size(); i++) {
       
   205 					TraceProvider newProvider = traceProviders.get(i);
       
   206 					if (newProvider.getPreferredConnectionType().equals(
       
   207 							selectedConnectionType)) {
       
   208 						store.setValue(PreferenceConstants.DATA_FORMAT,
       
   209 								newProvider.getName());
       
   210 
       
   211 						// Set new Trace provider
       
   212 						TraceViewerGlobals.setTraceProvider(newProvider, true);
       
   213 						break;
       
   214 					}
       
   215 				}
       
   216 			}
       
   217 		}
       
   218 	}
       
   219 
       
   220 	/**
       
   221 	 * Sets new connection preferences
       
   222 	 * 
       
   223 	 * @param connectionMethod
       
   224 	 *            connection method
       
   225 	 * @param parameters
       
   226 	 *            connection parameters
       
   227 	 * @return true if succeeded, false if failed
       
   228 	 */
       
   229 	private boolean setNewConnectionPreferences(int connectionMethod,
       
   230 			String[] parameters) {
       
   231 
       
   232 		boolean succeeded = true;
       
   233 
       
   234 		// Get the TraceViewer preferenceStore
       
   235 		IPreferenceStore store = TraceViewerPlugin.getDefault()
       
   236 				.getPreferenceStore();
       
   237 
       
   238 		try {
       
   239 			String connectionMethodStr = null;
       
   240 
       
   241 			// TCP / IP
       
   242 			if (connectionMethod == TraceViewerAPI.TVAPI_CONNECTION_TCP) {
       
   243 				connectionMethodStr = PreferenceConstants.CONNECTION_TYPE_TCPIP;
       
   244 
       
   245 				// There must be at least 2 parameters
       
   246 				if (parameters != null && parameters.length > 1) {
       
   247 
       
   248 					// Set IP address
       
   249 					store.setValue(PreferenceConstants.IP_ADDRESS,
       
   250 							parameters[0]);
       
   251 
       
   252 					// Set port number
       
   253 					store.setValue(PreferenceConstants.TCPIP_PORT,
       
   254 							parameters[1]);
       
   255 
       
   256 					// Check if there is channel specified
       
   257 					if (parameters.length > 2) {
       
   258 
       
   259 						// Set channel number
       
   260 						store.setValue(PreferenceConstants.TCPIP_CHANNEL,
       
   261 								parameters[2]);
       
   262 					}
       
   263 
       
   264 				} else {
       
   265 					succeeded = false;
       
   266 				}
       
   267 
       
   268 				// USB Serial
       
   269 			} else if (connectionMethod == TraceViewerAPI.TVAPI_CONNECTION_USB_SERIAL) {
       
   270 				connectionMethodStr = PreferenceConstants.CONNECTION_TYPE_USB_SERIAL;
       
   271 
       
   272 				// There must be at least 1 parameters
       
   273 				if (parameters != null && parameters.length > 0) {
       
   274 
       
   275 					// Set com port
       
   276 					store.setValue(PreferenceConstants.USB_SERIAL_COM_PORT,
       
   277 							parameters[0]);
       
   278 
       
   279 				} else {
       
   280 					succeeded = false;
       
   281 				}
       
   282 			}
       
   283 
       
   284 			// Set the connection method
       
   285 			if (succeeded) {
       
   286 				store.setValue(PreferenceConstants.CONNECTION_TYPE,
       
   287 						connectionMethodStr);
       
   288 
       
   289 				// Old connection ID is not valid, since connection settings
       
   290 				// have been changed.
       
   291 				store.setValue(PreferenceConstants.SELECTED_CONNECTION_ID, ""); //$NON-NLS-1$
       
   292 			}
       
   293 		} catch (Exception e) {
       
   294 			succeeded = false;
       
   295 		}
       
   296 
       
   297 		return succeeded;
       
   298 	}
       
   299 
       
   300 	/**
       
   301 	 * Saves old connection preferences
       
   302 	 */
       
   303 	private void saveConnectionPreferences() {
       
   304 
       
   305 		// Get the TraceViewer preferenceStore
       
   306 		IPreferenceStore store = TraceViewerPlugin.getDefault()
       
   307 				.getPreferenceStore();
       
   308 
       
   309 		oldConnectionMethod = store
       
   310 				.getString(PreferenceConstants.CONNECTION_TYPE);
       
   311 
       
   312 		oldSelectedConnectionID = store
       
   313 				.getString(PreferenceConstants.SELECTED_CONNECTION_ID);
       
   314 
       
   315 		// TCP / IP connection
       
   316 		if (oldConnectionMethod
       
   317 				.equals(PreferenceConstants.CONNECTION_TYPE_TCPIP)) {
       
   318 			oldParameters = new String[2];
       
   319 			oldParameters[0] = store.getString(PreferenceConstants.IP_ADDRESS);
       
   320 			oldParameters[1] = store.getString(PreferenceConstants.TCPIP_PORT);
       
   321 
       
   322 			// USB Serial connection
       
   323 		} else if (oldConnectionMethod
       
   324 				.equals(PreferenceConstants.CONNECTION_TYPE_USB_SERIAL)) {
       
   325 			oldParameters = new String[1];
       
   326 			oldParameters[0] = store
       
   327 					.getString(PreferenceConstants.USB_SERIAL_COM_PORT);
       
   328 		}
       
   329 	}
       
   330 
       
   331 	/**
       
   332 	 * Sets old connection preferences back to preference store
       
   333 	 */
       
   334 	private void setOldPreferencesBack() {
       
   335 
       
   336 		// Get the TraceViewer preferenceStore
       
   337 		IPreferenceStore store = TraceViewerPlugin.getDefault()
       
   338 				.getPreferenceStore();
       
   339 
       
   340 		// Set the connection method
       
   341 		store
       
   342 				.setValue(PreferenceConstants.CONNECTION_TYPE,
       
   343 						oldConnectionMethod);
       
   344 		// Set the selected connection ID
       
   345 		store.setValue(PreferenceConstants.SELECTED_CONNECTION_ID,
       
   346 				oldSelectedConnectionID);
       
   347 
       
   348 		// TCP / IP connection
       
   349 		if (oldConnectionMethod
       
   350 				.equals(PreferenceConstants.CONNECTION_TYPE_TCPIP)) {
       
   351 			store.setValue(PreferenceConstants.IP_ADDRESS, oldParameters[0]);
       
   352 			store.setValue(PreferenceConstants.TCPIP_PORT, oldParameters[1]);
       
   353 
       
   354 			// USB Serial connection
       
   355 		} else if (oldConnectionMethod
       
   356 				.equals(PreferenceConstants.CONNECTION_TYPE_USB_SERIAL)) {
       
   357 			store.setValue(PreferenceConstants.USB_SERIAL_COM_PORT,
       
   358 					oldParameters[0]);
       
   359 		}
       
   360 
       
   361 		// Resetting old connection settings
       
   362 		oldConnectionMethod = null;
       
   363 		oldParameters = null;
       
   364 	}
       
   365 
       
   366 	/**
       
   367 	 * Checks if there are old connection preferences to restore.
       
   368 	 * 
       
   369 	 * @return <code>true</code> if are settings to restore, otherwise
       
   370 	 *         <code>false</code>.
       
   371 	 */
       
   372 	public boolean hasRestorableConnectionPreferences() {
       
   373 		return (oldConnectionMethod != null && oldParameters != null);
       
   374 	}
       
   375 
       
   376 	/**
       
   377 	 * Disconnects from the target
       
   378 	 * 
       
   379 	 * @return error code from TraceViewerAPI
       
   380 	 */
       
   381 	public TVAPIError disconnect() {
       
   382 		TVAPIError errorcode = TVAPIError.NONE;
       
   383 
       
   384 		// Check if not connected
       
   385 		if (!isAlreadyConnected()) {
       
   386 			errorcode = TVAPIError.NOT_CONNECTED;
       
   387 
       
   388 		} else {
       
   389 			// Doing real disconnect only if client has been connected with
       
   390 			// custom connection preferences
       
   391 			// or there are no more registered clients left using the
       
   392 			// connection.
       
   393 			if (hasRestorableConnectionPreferences()
       
   394 					|| !TraceViewerAPI2Impl.hasRegisteredClients()) {
       
   395 
       
   396 				boolean disconnect;
       
   397 				// If view exists and we are in UI thread, do the disconnecting
       
   398 				// through the view action
       
   399 				if (TraceViewerGlobals.getTraceViewer().getView() != null
       
   400 						&& Display.getCurrent() != null) {
       
   401 					ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
   402 							.getTraceViewer().getView().getActionFactory()
       
   403 							.getConnectAction()));
       
   404 					disconnect = action.disconnect();
       
   405 
       
   406 					// Disconnect through engine
       
   407 				} else {
       
   408 					disconnect = TraceViewerGlobals.getTraceViewer()
       
   409 							.disconnect();
       
   410 				}
       
   411 
       
   412 				// Disconnection failed
       
   413 				if (!disconnect) {
       
   414 					errorcode = TVAPIError.DISCONNECTING_FAILED;
       
   415 
       
   416 					// Disconnecting succeeded and view exists, change button
       
   417 				} else if (TraceViewerGlobals.getTraceViewer().getView() != null) {
       
   418 					ConnectAction action = ((ConnectAction) (TraceViewerGlobals
       
   419 							.getTraceViewer().getView().getActionFactory()
       
   420 							.getConnectAction()));
       
   421 					action.changeToConnectAction();
       
   422 				}
       
   423 			}
       
   424 		}
       
   425 
       
   426 		// In case custom connection parameters were used in last connect...
       
   427 		if (hasRestorableConnectionPreferences()) {
       
   428 			// ... we are restoring the old settings back
       
   429 			setOldPreferencesBack();
       
   430 		}
       
   431 
       
   432 		return errorcode;
       
   433 	}
       
   434 
       
   435 	/**
       
   436 	 * Checks if the connection is already connected
       
   437 	 * 
       
   438 	 * @return true if already connected
       
   439 	 */
       
   440 	private boolean isAlreadyConnected() {
       
   441 		boolean alreadyConnected = false;
       
   442 
       
   443 		// Check if already connected
       
   444 		if (TraceViewerGlobals.getTraceViewer().getConnection() != null) {
       
   445 			if (TraceViewerGlobals.getTraceViewer().getConnection()
       
   446 					.isConnected()) {
       
   447 				alreadyConnected = true;
       
   448 			}
       
   449 		}
       
   450 		return alreadyConnected;
       
   451 	}
       
   452 }