trace/traceviewer/com.nokia.traceviewer/src/com/nokia/traceviewer/internal/api/TraceViewerAPI2Impl.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  */
       
    17 
       
    18 package com.nokia.traceviewer.internal.api;
       
    19 
       
    20 import java.util.Collections;
       
    21 import java.util.HashSet;
       
    22 import java.util.Iterator;
       
    23 import java.util.Set;
       
    24 
       
    25 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    26 import com.nokia.traceviewer.api.ITraceConnClient;
       
    27 import com.nokia.traceviewer.api.ITraceConnInfo;
       
    28 import com.nokia.traceviewer.api.TraceConnectivityException;
       
    29 import com.nokia.traceviewer.api.TraceViewerAPI;
       
    30 import com.nokia.traceviewer.api.TraceViewerAPI2;
       
    31 import com.nokia.traceviewer.api.TraceViewerAPI.TVAPIError;
       
    32 import com.nokia.traceviewer.engine.ConnectionHelper;
       
    33 
       
    34 /**
       
    35  * Implementation if TraceViewer API functions that offers - notification
       
    36  * services for client on connection status changes. - keeps count of active
       
    37  * clients, and disconnects data source only when there is no more active
       
    38  * clients.
       
    39  */
       
    40 public class TraceViewerAPI2Impl {
       
    41 
       
    42 	/**
       
    43 	 * Storage for currently registered clients.
       
    44 	 */
       
    45 	public static Set<ITraceConnClient> clients = Collections
       
    46 			.synchronizedSet(new HashSet<ITraceConnClient>());
       
    47 
       
    48 	/**
       
    49 	 * Connect using current settings defined in TraceViewer's preferences.
       
    50 	 * Client is registered successfully in case no exceptions are thrown.
       
    51 	 * 
       
    52 	 * @param client
       
    53 	 *            client requesting the connection.
       
    54 	 * @return connection info about the connected service.
       
    55 	 * @throws TraceConnectivityException
       
    56 	 */
       
    57 	public static ITraceConnInfo connect(ITraceConnClient client)
       
    58 			throws TraceConnectivityException {
       
    59 		TVAPIError error = TraceViewerAPI.connect();
       
    60 		if (error == TVAPIError.NONE || error == TVAPIError.ALREADY_CONNECTED) {
       
    61 			// Register must have after connect attempt because it triggers
       
    62 			// notifications to already registered clients
       
    63 			registerClient(client);
       
    64 		} else {
       
    65 			throw new TraceConnectivityException(error);
       
    66 		}
       
    67 		IConnection connectionWithCurrentID = ConnectionHelper
       
    68 				.getConnectionWithCurrentID();
       
    69 		return new TraceConnInfo(connectionWithCurrentID);
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * Connects using current settings defined in TraceViewer's preferences
       
    74 	 * Client is disconnected and unregistered successfully for notifications in
       
    75 	 * case no exceptions are thrown.
       
    76 	 * 
       
    77 	 * @param client
       
    78 	 *            client requesting the connection.
       
    79 	 * @throws TraceConnectivityException
       
    80 	 */
       
    81 	public static void disconnect(ITraceConnClient client)
       
    82 			throws TraceConnectivityException {
       
    83 		// Client is unregistered in any case and must be done before
       
    84 		// disconnect() is called
       
    85 		// because ínside disconnect() there is callback back to
       
    86 		// hasRegisteredClients() method.
       
    87 		boolean clientWasRegistered = unregisterClient(client);
       
    88 		// Real disconnect only, if there are no more other clients using the
       
    89 		// connection
       
    90 		if (!hasRegisteredClients()) {
       
    91 			TVAPIError error = TraceViewerAPI.disconnect();
       
    92 			if (error != TVAPIError.NONE) {
       
    93 				throw new TraceConnectivityException(error);
       
    94 			}
       
    95 		}
       
    96 		// clientWasRegistered is true if the client was registered before
       
    97 		// unregister attempt
       
    98 		if (!clientWasRegistered) {
       
    99 			// Reporting also possible failures that are possible errors in
       
   100 			// client code
       
   101 			throw new RuntimeException(
       
   102 					TraceViewerAPI2.class.getSimpleName()
       
   103 							+ ": " //$NON-NLS-1$ 
       
   104 							+ Messages.TraceViewerAPI2Impl_UnregisterNotRegisteredClient_ErrMsg
       
   105 							+ ": " + client); //$NON-NLS-1$ 
       
   106 		}
       
   107 	}
       
   108 
       
   109 	/**
       
   110 	 * Checks if there are any registered clients left.
       
   111 	 * 
       
   112 	 * @return <code>true</code> if there are registered clients, otherwise
       
   113 	 *         <code>false</code>.
       
   114 	 */
       
   115 	public static boolean hasRegisteredClients() {
       
   116 		synchronized (clients) {
       
   117 			return !clients.isEmpty();
       
   118 		}
       
   119 	}
       
   120 
       
   121 	/**
       
   122 	 * Registers client.
       
   123 	 * 
       
   124 	 * @param client
       
   125 	 *            client to register
       
   126 	 */
       
   127 	private static void registerClient(ITraceConnClient client) {
       
   128 		synchronized (clients) {
       
   129 			clients.add(client);
       
   130 		}
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Unregisters client.
       
   135 	 * 
       
   136 	 * @param client
       
   137 	 *            client to unregister
       
   138 	 * @return true if the previously registered client was unregistered
       
   139 	 *         successfully
       
   140 	 */
       
   141 	private static boolean unregisterClient(ITraceConnClient client) {
       
   142 		synchronized (clients) {
       
   143 			// remove returns true if the set contained the specified element.
       
   144 			return clients.remove(client);
       
   145 		}
       
   146 	}
       
   147 
       
   148 	/**
       
   149 	 * Notifies all client about disconnect event
       
   150 	 * 
       
   151 	 * @param currentConnection
       
   152 	 *            currently selected connection
       
   153 	 */
       
   154 	public static void notifyConnection(IConnection currentConnection) {
       
   155 		synchronized (clients) {
       
   156 			for (Iterator<ITraceConnClient> iterator = clients.iterator(); iterator
       
   157 					.hasNext();) {
       
   158 				ITraceConnClient client = iterator.next();
       
   159 				client.notifyConnection(new TraceConnInfo(currentConnection));
       
   160 			}
       
   161 		}
       
   162 	}
       
   163 
       
   164 	/**
       
   165 	 * Notifies all client about disconnect event
       
   166 	 */
       
   167 	public static void notifyDisconnection() {
       
   168 		synchronized (clients) {
       
   169 			for (Iterator<ITraceConnClient> iterator = clients.iterator(); iterator
       
   170 					.hasNext();) {
       
   171 				ITraceConnClient client = iterator.next();
       
   172 				client.notifyDisconnection();
       
   173 			}
       
   174 		}
       
   175 	}
       
   176 
       
   177 	/**
       
   178 	 * Notifies listeners that connection preferences has been changed.
       
   179 	 * 
       
   180 	 * @param connectionInfo
       
   181 	 *            Connection info about the new connection preferences.
       
   182 	 */
       
   183 	public static void notifyConnPrefsChanged(IConnection connectionInfo) {
       
   184 		synchronized (clients) {
       
   185 			for (Iterator<ITraceConnClient> iterator = clients.iterator(); iterator
       
   186 					.hasNext();) {
       
   187 				ITraceConnClient client = iterator.next();
       
   188 				client
       
   189 						.notifyConnPrefsChanged(new TraceConnInfo(
       
   190 								connectionInfo));
       
   191 			}
       
   192 		}
       
   193 	}
       
   194 
       
   195 	/**
       
   196 	 * Gets count of currently registered clients. This method is meant only for
       
   197 	 * unit testing purposes.
       
   198 	 * 
       
   199 	 * @return count of currently registered clients
       
   200 	 */
       
   201 	public static int getRegisteredClientsCount() {
       
   202 		synchronized (clients) {
       
   203 			return clients.size();
       
   204 		}
       
   205 	}
       
   206 
       
   207 }