network
Class Network

java.lang.Object
  extended bycore.EventNotifier
      extended bynetwork.NetworkNotifier
          extended bynetwork.Network

public class Network
extends NetworkNotifier

This is the the only class that any other layer of our project should care about. To use it, have a class implement the NetworkListener interface and pass an instance of that class to the constructor (probably this). We do this so there is a NetworkListener listening before we start the Server.

Author:
hoele, cooperse

Field Summary
static java.lang.String CACHE_MAX_SIZE
           
static java.lang.String LOCALHOST
           
static java.lang.String MAX_NEIGHBORS
           
static java.lang.String MIN_NEIGHBORS
           
static int NUM_PORTS
          The number of ports to listen on
static int PORT
          The first port for this app, avail range: 32636-32765
static int PORT_INC
          The next port to check
 
Constructor Summary
Network(java.lang.String ownVirtualAddress, Prefs prefs, java.util.Vector channelListeners)
          Create a new Network with no neighbors and a server that listens for connections.
 
Method Summary
 void addIPs(java.lang.String addresses)
          Add the given string of IPs to the IPCache
 boolean addNeighbor(java.net.Socket socket, boolean transferKey)
          Add a neighbor.
 boolean addNeighbor(java.lang.String address)
          Add a neighbor.
 void addNeighbors()
          Try to fill the neighbor list.
 void addRoute(java.lang.String fromVirtualAddress, Neighbor neighbor)
           
 void broadcast(Packet packet, Neighbor fromNeighbor)
          Broadcasts packet to all neighbors.
 void cancelTimer()
          Stop the maintainence daemon.
 boolean contains(java.lang.String ip)
           
 java.lang.String getLocalIP()
           
 int getMinNeighbors()
           
 java.util.Collection getNeighbors()
          Returns a Collection of neighbors connected to.
 int getNumNeighbors()
          Get the number of connected neighbors.
 java.lang.String getOwnVirtualAddress()
           
 Prefs getPrefs()
           
 java.util.Hashtable getVirtualAddressToPacketID()
           
 void printChannelList(java.lang.String from)
          Print channel list associated with from address
 void reloadServers()
          loads server ips into cache again.
 void removeNeighbor(Neighbor neighbor, java.lang.String caller)
          Remove the neighbor with the given address from the list of neighbors.
 void sendIPs(Neighbor neighbor, int numRequested)
          Send the IPs of our neighbors to the requesting neighbor
 void sendMessage(Message message)
          Send the TextMessage message.
 void sendPacket(XMLPacket packet, Neighbor fromNeighbor)
          Send the Packet.
 void sendPacketToNeighbor(Packet packet, Neighbor neighbor)
           
 void stop()
          Stop the network.
 
Methods inherited from class network.NetworkNotifier
fireMessageReceived, fireNeighborConnected
 
Methods inherited from class core.EventNotifier
addListener, removeListener
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALHOST

public static final java.lang.String LOCALHOST
See Also:
Constant Field Values

MIN_NEIGHBORS

public static final java.lang.String MIN_NEIGHBORS
See Also:
Constant Field Values

CACHE_MAX_SIZE

public static final java.lang.String CACHE_MAX_SIZE
See Also:
Constant Field Values

MAX_NEIGHBORS

public static final java.lang.String MAX_NEIGHBORS
See Also:
Constant Field Values

PORT

public static final int PORT
The first port for this app, avail range: 32636-32765

See Also:
Constant Field Values

PORT_INC

public static final int PORT_INC
The next port to check

See Also:
Constant Field Values

NUM_PORTS

public static final int NUM_PORTS
The number of ports to listen on

See Also:
Constant Field Values
Constructor Detail

Network

public Network(java.lang.String ownVirtualAddress,
               Prefs prefs,
               java.util.Vector channelListeners)
Create a new Network with no neighbors and a server that listens for connections. State prefs.

Parameters:
ownVirtualAddress - Our virtual address.
prefs - Our preferences.
channelListeners -
Method Detail

addNeighbors

public void addNeighbors()
Try to fill the neighbor list.


addNeighbor

public boolean addNeighbor(java.lang.String address)
Add a neighbor.

Parameters:
address - The IP address or hostname of the neighbor.

addNeighbor

public boolean addNeighbor(java.net.Socket socket,
                           boolean transferKey)
Add a neighbor.

Parameters:
socket - The Socket for communicating with the neighbor.
transferKey - Whether we need to send the Cipher
Returns:
False if neighbor could not be added.

addRoute

public void addRoute(java.lang.String fromVirtualAddress,
                     Neighbor neighbor)

removeNeighbor

public void removeNeighbor(Neighbor neighbor,
                           java.lang.String caller)
Remove the neighbor with the given address from the list of neighbors.

Parameters:
neighbor - The address of the neighbor to be removed.

sendMessage

public void sendMessage(Message message)
Send the TextMessage message.

Parameters:
message - The TextMessage to be sent.

sendPacket

public void sendPacket(XMLPacket packet,
                       Neighbor fromNeighbor)
Send the Packet. This is where routing will occur.

Parameters:
packet - The Packet to be sent.
fromNeighbor - The neighbor the packet is from.

broadcast

public void broadcast(Packet packet,
                      Neighbor fromNeighbor)
Broadcasts packet to all neighbors.

Parameters:
packet - The packet to be broadcasted.
fromNeighbor - The neighbor the packet is from.

sendPacketToNeighbor

public void sendPacketToNeighbor(Packet packet,
                                 Neighbor neighbor)

getNeighbors

public java.util.Collection getNeighbors()
Returns a Collection of neighbors connected to.

Returns:
a Collection of neighbors.

stop

public void stop()
Stop the network.


getOwnVirtualAddress

public java.lang.String getOwnVirtualAddress()
Returns:
Returns Own virtual address.

printChannelList

public void printChannelList(java.lang.String from)
Print channel list associated with from address

Parameters:
from -

getNumNeighbors

public int getNumNeighbors()
Get the number of connected neighbors.

Returns:
The number of neighbors.

sendIPs

public void sendIPs(Neighbor neighbor,
                    int numRequested)
Send the IPs of our neighbors to the requesting neighbor

Parameters:
neighbor - The neighbor requesting the IPs.
numRequested - The number of IPs requested.

addIPs

public void addIPs(java.lang.String addresses)
Add the given string of IPs to the IPCache

Parameters:
addresses - The string of IPs separated by white space

contains

public boolean contains(java.lang.String ip)
Parameters:
ip - the ip address of the neighbor.
Returns:
true if the neighbor's ip is in our cache; false otherwise.

reloadServers

public void reloadServers()
loads server ips into cache again.


getMinNeighbors

public int getMinNeighbors()
Returns:
The minimum number of neighbors.

cancelTimer

public void cancelTimer()
Stop the maintainence daemon.


getLocalIP

public java.lang.String getLocalIP()
Returns:
Returns the local IP.

getPrefs

public Prefs getPrefs()
Returns:
Returns the prefs.

getVirtualAddressToPacketID

public java.util.Hashtable getVirtualAddressToPacketID()
Returns:
Returns the virtualAddressToPacketID.