Package connection

Source Code of connection.ConnectionManager

package connection;


import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ConcurrentHashMap;

import connection.listener.Listener;
import connection.listener.SecureListener;

import iteration.ConnectionIterationClientInterface;
import iteration.context.IterationContextInterface;


public class ConnectionManager
{
  private ConcurrentHashMap<Integer, ClientConnection> array;

  public ConnectionManager()
  {
    array = new ConcurrentHashMap<Integer, ClientConnection>();
  }

  public void listen()
  {
    new Listener(this);
    new SecureListener(this);
  }

  public boolean accept(ServerSocket listener)
  {
    try
    {
      new ClientConnector(listener);
    }
    catch (IOException e)
    {
      System.out.println("Failed accepting connection. System error message: "
          + e.getMessage());
      return false;
    }

    return true;
  }

  public int add(ClientConnection connection)
  {
    int newId = connection.hashCode();
    array.put(newId, connection);
    printConnectionCount();
    return newId;
  }

  public void remove(int clientId)
  {
    array.remove(clientId);
    printConnectionCount();
  }

  public int getNumberOfConnections()
  {
    return array.size();
  }

  private void printConnectionCount()
  {
    System.out.println(new Integer(array.size()) + " clients connected");
  }

  public void iterate(ConnectionIterationClientInterface client,
      IterationContextInterface context)
  {
    for (ClientConnection current : array.values())
    {
      client.iterate(current, context);
    }
  }

  public ClientConnection getClientById(int id)
  {
    return array.get(id);
  }

}
TOP

Related Classes of connection.ConnectionManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.