add logic for disconnect and reconnect
authordadubrow
Fri, 18 Dec 2009 10:44:27 -0600
changeset 711 d97b8b6bce37
parent 710 47bb893ba749
child 712 776322e7fc9a
add logic for disconnect and reconnect
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/registry/Registry.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionUIUtils.java
connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/registry/Registry.java	Fri Dec 18 10:19:18 2009 -0600
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/registry/Registry.java	Fri Dec 18 10:44:27 2009 -0600
@@ -44,6 +44,7 @@
 
 import com.nokia.carbide.remoteconnections.Messages;
 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
+import com.nokia.carbide.remoteconnections.interfaces.AbstractConnection.ConnectionStatus;
 import com.nokia.carbide.remoteconnections.interfaces.IClientServiceSiteUI;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectedServiceFactory;
@@ -54,6 +55,10 @@
 import com.nokia.carbide.remoteconnections.interfaces.IExtensionFilter;
 import com.nokia.carbide.remoteconnections.interfaces.IService;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
+import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
+import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
+import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
+import com.nokia.carbide.remoteconnections.internal.ui.ConnectionUIUtils;
 import com.nokia.carbide.remoteconnections.ui.ClientServiceSiteUI;
 import com.nokia.cpp.internal.api.utils.core.Check;
 import com.nokia.cpp.internal.api.utils.core.ListenerList;
@@ -83,6 +88,7 @@
 	private List<IConnectedServiceFactory> connectedServiceFactories;
 	private ListenerList<IConnectionListener> connectionListeners;
 	private IConnection defaultConnection;
+	private Map<IConnection, IConnectionStatusChangedListener> connectionListenerMap;
 
 	public static Registry instance() {
 		return instance;
@@ -91,6 +97,7 @@
 	private Registry() {
 		// private because is singleton
 		connectionToConnectedServices = new HashMap<IConnection, List<IConnectedService>>();
+		connectionListenerMap = new HashMap<IConnection, IConnectionStatusChangedListener>();
 	}
 
 	public void loadExtensions() {
@@ -530,17 +537,59 @@
 		return defaultConnection;
 	}
 
-	public void disconnect(IConnection2 connection) {
+	public void disconnect(final IConnection2 connection) {
+		if (!connection.isDynamic())
+			return;
 		
-		// TODO transition to disconnected state and wait:
-		// if not in-use, remove and stop waiting
-		// if reconnected, stop waiting
-		
+		// transition to disconnected state and wait:
+		// 	if not in-use, remove and stop waiting
+		// 	if reconnected, stop waiting
+		if (connection.getStatus().getEConnectionStatus().equals(EConnectionStatus.IN_USE)) {
+			IConnectionStatus status = new ConnectionStatus(EConnectionStatus.IN_USE_DISCONNECTED, 
+						"connection has been disconnected while in use, please reconnect");
+			connection.setStatus(status);
+			IConnectionStatusChangedListener listener = new IConnectionStatusChangedListener() {
+				public void statusChanged(IConnectionStatus status) {
+					if (notInUse(status)) {
+						IConnectionStatusChangedListener listener = 
+							connectionListenerMap.remove(connection);
+						connection.removeStatusChangedListener(listener);
+						removeConnection(connection);
+					}
+				}
+
+				private boolean notInUse(IConnectionStatus status) {
+					EConnectionStatus eStatus = status.getEConnectionStatus();
+					return !eStatus.equals(EConnectionStatus.IN_USE) &&
+						!eStatus.equals(EConnectionStatus.IN_USE_DISCONNECTED);
+				}
+			};
+			connectionListenerMap.put(connection, listener);
+			connection.addStatusChangedListener(listener);
+		}
 	}
 
 	public boolean reconnect(IConnection2 connection) {
-		// TODO if not removed, transition out of disconnected state
+		if (!connection.isDynamic())
+			return false;
+		
+		// if not removed, transition out of disconnected state 
 		// return not removed
+		IConnectionStatusChangedListener listener = connectionListenerMap.remove(connection);
+		if (listener != null)
+			connection.removeStatusChangedListener(listener);
+		if (connectionToConnectedServices.containsKey(connection)) {
+			IConnectionStatus status;
+			if (ConnectionUIUtils.isSomeServiceInUse(connection)) {
+				status = new ConnectionStatus(EConnectionStatus.IN_USE, 
+						Messages.getString("ConnectionsView.InUseDesc")); //$NON-NLS-1$
+			}
+			else {
+				status = new ConnectionStatus(EConnectionStatus.NOT_READY, ""); //$NON-NLS-1$
+			}
+			connection.setStatus(status);
+			return true;
+		}
 		return false;
 	}
 }
\ No newline at end of file
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionUIUtils.java	Fri Dec 18 10:19:18 2009 -0600
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionUIUtils.java	Fri Dec 18 10:44:27 2009 -0600
@@ -27,6 +27,8 @@
 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
+import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus.EConnectionStatus;
@@ -37,23 +39,6 @@
  */
 public class ConnectionUIUtils {
 
-	public static IConnectedService.IStatus getFirstInUseStatus(IConnection connection) {
-		Collection<IConnectedService> connectedServices = 
-			Registry.instance().getConnectedServices(connection);
-		// if any service is in-use, then connection is in-use
-		for (IConnectedService connectedService : connectedServices) {
-			IConnectedService.IStatus status = connectedService.getStatus();
-			if (status.getEStatus().equals(IConnectedService.IStatus.EStatus.IN_USE))
-				return status;
-		}
-		
-		return null;
-	}
-
-	public static boolean isConnectionInUse(IConnection connection) {
-		return getFirstInUseStatus(connection) != null;
-	}
-
 	private static final ImageDescriptor STATUS_AVAIL_IMGDESC = 
 	RemoteConnectionsActivator.getImageDescriptor("icons/statusAvailable.png"); //$NON-NLS-1$
 	private static final ImageDescriptor STATUS_UNAVAIL_IMGDESC = 
@@ -125,7 +110,7 @@
 			IConnectionStatus status = ((IConnection2) connection).getStatus();
 			return getConnectionStatusImage(status);
 		} else {
-			if (isConnectionInUse(connection)) {
+			if (isSomeServiceInUse(connection)) {
 				return ConnectionUIUtils.STATUS_INUSE_IMG;
 			}
 			return ConnectionUIUtils.CONNECTION_IMG;
@@ -150,4 +135,20 @@
 		return null;
 	}
 
+	public static IStatus getFirstInUseServiceStatus(IConnection connection) {
+		Collection<IConnectedService> connectedServices = 
+			Registry.instance().getConnectedServices(connection);
+		// if any service is in-use, then connection is in-use
+		for (IConnectedService connectedService : connectedServices) {
+			IStatus status = connectedService.getStatus();
+			if (status.getEStatus().equals(EStatus.IN_USE))
+				return status;
+		}
+		
+		return null;
+	}
+
+	public static boolean isSomeServiceInUse(IConnection connection) {
+		return getFirstInUseServiceStatus(connection) != null;
+	}
 }
--- a/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java	Fri Dec 18 10:19:18 2009 -0600
+++ b/connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java	Fri Dec 18 10:44:27 2009 -0600
@@ -211,7 +211,7 @@
 			else if (value instanceof IConnectedService) {
 				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
 				IConnection connection = findConnection((IConnectedService) value);
-				if (connection != null && isConnectionInUse(connection))
+				if (connection != null && ConnectionUIUtils.isSomeServiceInUse(connection))
 					status = EStatus.IN_USE;
 				return ConnectionUIUtils.getConnectedServiceStatusImage(status);
 			}
@@ -227,14 +227,14 @@
 				IStatus status = null;
 				IConnection connection = findConnection((IConnectedService) value);
 				if (connection != null)
-					status = getFirstInUseStatus(connection);
+					status = ConnectionUIUtils.getFirstInUseServiceStatus(connection);
 				if (status == null) {
 					status = ((IConnectedService) value).getStatus();
 					return status.getShortDescription();
 				}
 			}
 			else if (value instanceof IConnection) {
-				IStatus status = getFirstInUseStatus((IConnection) value);
+				IStatus status = ConnectionUIUtils.getFirstInUseServiceStatus((IConnection) value);
 				if (status != null)
 					return status.getShortDescription();
 			}
@@ -274,7 +274,7 @@
 				IStatus status = ((IConnectedService) value).getStatus();
 				IConnection connection = findConnection((IConnectedService) value);
 				if (!status.getEStatus().equals(EStatus.IN_USE) ||
-						!(connection != null && isConnectionInUse(connection))) { // if in-use, we show it in the connection row
+						!(connection != null && ConnectionUIUtils.isSomeServiceInUse(connection))) { // if in-use, we show it in the connection row
 					String longDescription = status.getLongDescription();
 					if (longDescription != null)
 						longDescription = TextUtils.canonicalizeNewlines(longDescription, " "); //$NON-NLS-1$
@@ -282,7 +282,7 @@
 				}
 			}
 			else if (value instanceof IConnection) {
-				if (isConnectionInUse((IConnection) value)) {
+				if (ConnectionUIUtils.isSomeServiceInUse((IConnection) value)) {
 					return Messages.getString("ConnectionsView.InUseDesc");
 				}
 			}
@@ -758,23 +758,6 @@
 		return null;
 	}
 
-	private static IStatus getFirstInUseStatus(IConnection connection) {
-		Collection<IConnectedService> connectedServices = 
-			Registry.instance().getConnectedServices(connection);
-		// if any service is in-use, then connection is in-use
-		for (IConnectedService connectedService : connectedServices) {
-			IStatus status = connectedService.getStatus();
-			if (status.getEStatus().equals(EStatus.IN_USE))
-				return status;
-		}
-		
-		return null;
-	}
-
-	private boolean isConnectionInUse(IConnection connection) {
-		return getFirstInUseStatus(connection) != null;
-	}
-
 	private boolean isDynamicConnection(Object object) {
 		return object instanceof IConnection2 && ((IConnection2) object).isDynamic();
 	}