workspace
Class NetworkManager

java.lang.Object
  extended by workspace.NetworkManager

public class NetworkManager
extends java.lang.Object

A NetworkManager maintains connections and host information. The network is peer-to-peer over TCP.


Constructor Summary
NetworkManager()
           
 
Method Summary
(package private)  void connect(java.net.Socket s)
           
protected  void disconnect(java.lang.Long id)
          Disconnect from the given host, if connected.
protected  void disconnected(java.lang.Long id)
          Receive notification that we've disconnected from a host.
static void error(java.lang.String msg)
          Displays an error message to the user.
protected  boolean establishConnection(java.lang.String host, int port)
          Open a client connection.
protected  boolean eventReceived(NetworkEvent obj)
          Process objects as they are read.
protected  NetworkConnection getHost(java.lang.Long host)
          Allow subclasses to access the host information.
static long getHostIdentifier(java.net.InetAddress addr, int port)
          Returns the long (8-byte) identifier for the given host.
 java.lang.String getServerAddress()
           
 long getServerHash()
           
 int getServerPort()
           
 boolean openServerConnection()
          Open a server connection.
 boolean sendEvent(java.lang.Long host, NetworkEvent event)
          Send the event to the host.
 void sendEvent(NetworkEvent event)
          Broadcast the event to all connected parties.
protected  void setServerHash(long hash)
          Be careful when calling this method.
 void stop()
          Close all connections and reset the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NetworkManager

public NetworkManager()
Method Detail

getServerHash

public long getServerHash()

getServerPort

public int getServerPort()

setServerHash

protected void setServerHash(long hash)
Be careful when calling this method. If hash is 0, will reset any existing connections.


getServerAddress

public java.lang.String getServerAddress()

getHost

protected NetworkConnection getHost(java.lang.Long host)
Allow subclasses to access the host information.


sendEvent

public void sendEvent(NetworkEvent event)
Broadcast the event to all connected parties.


sendEvent

public boolean sendEvent(java.lang.Long host,
                         NetworkEvent event)
Send the event to the host. If we don't know how to reach the host, returns false.


establishConnection

protected boolean establishConnection(java.lang.String host,
                                      int port)
Open a client connection.


openServerConnection

public boolean openServerConnection()
Open a server connection.


disconnect

protected void disconnect(java.lang.Long id)
Disconnect from the given host, if connected.


stop

public void stop()
Close all connections and reset the server.


connect

void connect(java.net.Socket s)
       throws java.io.IOException
Throws:
java.io.IOException

eventReceived

protected boolean eventReceived(NetworkEvent obj)
Process objects as they are read. Subclasses should override this method, as the default implementation simply prints out the event.


disconnected

protected void disconnected(java.lang.Long id)
Receive notification that we've disconnected from a host. The NetworkConnection calls this method once close() executes. Subclasses should override this method to do any cleanups. Make sure to call the super-implementation.


error

public static void error(java.lang.String msg)
Displays an error message to the user.


getHostIdentifier

public static long getHostIdentifier(java.net.InetAddress addr,
                                     int port)
Returns the long (8-byte) identifier for the given host. Assumes IPv4. Future versions that use IPv6 should return 10 bytes (or use a different algorithm).