Android Open Source - webots-remote-control Connection Manager






From Project

Back to project page webots-remote-control.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project webots-remote-control listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.black_mesa.webots_remote_control.client;
/*from  w w  w .  ja  va 2  s.co m*/
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.black_mesa.webots_remote_control.communication_structures.CommunicationStructure;
import org.black_mesa.webots_remote_control.database.Server;
import org.black_mesa.webots_remote_control.listeners.ClientListener;
import org.black_mesa.webots_remote_control.listeners.ConnectionManagerListener;

/**
 * Manages a collection of connections represented by Client objects.
 * 
 * @author Ilja Kroonen
 * 
 */
public class ConnectionManager {
  private final List<ConnectionManagerListener> mListeners = new ArrayList<ConnectionManagerListener>();
  private final Map<Server, Client> mConnections = new Hashtable<Server, Client>();
  private final ClientListener mClientListener;

  /**
   * Instantiates the ConnectionManager.
   */
  public ConnectionManager() {
    mClientListener = new ClientListener() {

      @Override
      public void onStateChange(final Server server, final ConnectionState state) {
        Client source = mConnections.get(server);
        if (source == null) {
          // The client could have queued a stateChange event in the
          // UI thread queue between the call to dispose and the
          // actual disposing. We just ignore the event.
          return;
        }
        if (state != ConnectionState.CONNECTED) {
          mConnections.remove(server);
        }

        for (ConnectionManagerListener l : mListeners) {
          l.onStateChange(server, state);
        }
      }

      @Override
      public void onReception(final Server server, final CommunicationStructure data) {
        // This will be needed to implement further features
      }
    };
  }

  /**
   * Adds a listener to this ConnectionManager. It will be notified on any
   * state change in the connections.
   * 
   * @param listener
   *            Listener that will be added.
   */
  public final void addListener(final ConnectionManagerListener listener) {
    mListeners.add(listener);
  }

  /**
   * Removes a listener.
   * 
   * @param listener
   *            Listener that will be removed.
   */
  public final void removeListener(final ConnectionManagerListener listener) {
    mListeners.remove(listener);
  }

  /**
   * Connects to a server.
   * 
   * @param server
   *            Server to connect.
   */
  public final void addServer(final Server server) {
    if (mConnections.containsKey(server)) {
      throw new IllegalArgumentException(server + " was already present in the manager");
    }
    mConnections.put(server, new Client(server, mClientListener));
  }

  /**
   * Removes a server from the ConnectionManager and disconnects its client.
   * 
   * @param server
   *            Server
   */
  public final void removeServer(final Server server) {
    Client client = mConnections.get(server);
    if (client == null) {
      return;
    }
    client.dispose();
    mConnections.remove(server);
  }

  /**
   * Returns the client corresponding to a server.
   * 
   * @param server
   *            Server corresponding to the client we need.
   * @return Client corresponding to the server.
   */
  public final Client getClient(final Server server) {
    return mConnections.get(server);
  }

  /**
   * Closes all connections.
   */
  public final void dispose() {
    for (Client c : mConnections.values()) {
      c.dispose();
    }
    mConnections.clear();
  }
}




Java Source Code List

org.black_mesa.webots_remote_control.activities.AboutFragment.java
org.black_mesa.webots_remote_control.activities.AddServerActivity.java
org.black_mesa.webots_remote_control.activities.CameraFragment.java
org.black_mesa.webots_remote_control.activities.ConnectionFragment.java
org.black_mesa.webots_remote_control.activities.MainActivity.java
org.black_mesa.webots_remote_control.client.CamerasManager.java
org.black_mesa.webots_remote_control.client.Client.java
org.black_mesa.webots_remote_control.client.ConnectionManager.java
org.black_mesa.webots_remote_control.client.ConnectionState.java
org.black_mesa.webots_remote_control.client.package-info.java
org.black_mesa.webots_remote_control.communication_structures.CameraInstructionQueue.java
org.black_mesa.webots_remote_control.communication_structures.CameraInstruction.java
org.black_mesa.webots_remote_control.communication_structures.CommunicationStructure.java
org.black_mesa.webots_remote_control.communication_structures.package-info.java
org.black_mesa.webots_remote_control.database.DataBaseContract.java
org.black_mesa.webots_remote_control.database.DataBaseHelper.java
org.black_mesa.webots_remote_control.database.DataSource.java
org.black_mesa.webots_remote_control.database.ServerListAdapter.java
org.black_mesa.webots_remote_control.database.Server.java
org.black_mesa.webots_remote_control.listeners.CameraJoysticksViewListener.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV1.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV2.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV3.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV4.java
org.black_mesa.webots_remote_control.listeners.CameraTouchListenerV5.java
org.black_mesa.webots_remote_control.listeners.ClientListener.java
org.black_mesa.webots_remote_control.listeners.ConnectionManagerListener.java
org.black_mesa.webots_remote_control.listeners.OnListEventsListener.java
org.black_mesa.webots_remote_control.listeners.package-info.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV1.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV2.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV3.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV4.java
org.black_mesa.webots_remote_control.utils.CameraTouchHandlerV5.java
org.black_mesa.webots_remote_control.utils.package-info.java
org.black_mesa.webots_remote_control.views.CameraJoysticksView.java
org.black_mesa.webots_remote_control.views.CameraView.java