network
Class Neighbor

java.lang.Object
  extended bynetwork.Neighbor
All Implemented Interfaces:
java.lang.Runnable

public class Neighbor
extends java.lang.Object
implements java.lang.Runnable

This class sends and receives packets from a neighbor.

Author:
hoele, cooperse

Constructor Summary
Neighbor(java.net.Socket socket, Network network, boolean transferKey)
          Creates a new Neighbor.
 
Method Summary
 void connect(java.net.Socket socket, boolean sendKey)
          Connects to a neighbor via passed socket
 void finalize()
          Called by the garbage collector.
 java.lang.String getAddress()
          IP Address.
 int getLocalPort()
           
 java.lang.String getName()
          Return the name of the neighbor.
 boolean isConnected()
          Returns whether or not there is a connection open to this Neighbor.
 boolean isRemoved()
          Indicates whether or not this Neighbor has started yet or has been stopped.
 void run()
          While the thread is running, deal with the packets coming in from the associated neighbor.
 void sendPacket(Packet packet)
          Sends the given packet through socket.
 void start()
          Start the Neighbor Thread.
 void stop()
          Stop the Neighbor Thread.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Neighbor

public Neighbor(java.net.Socket socket,
                Network network,
                boolean transferKey)
         throws java.security.InvalidKeyException,
                java.security.NoSuchAlgorithmException,
                javax.crypto.NoSuchPaddingException,
                java.lang.ClassNotFoundException,
                java.io.IOException
Creates a new Neighbor.

Parameters:
socket - The Socket used to read from the neighbor and write to the neighbor.
network - The Network class that handles this Neighbor.
transferKey - Whether the encryption key should be sent
Throws:
java.lang.ClassNotFoundException
java.io.IOException
javax.crypto.NoSuchPaddingException
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
Method Detail

getName

public java.lang.String getName()
Return the name of the neighbor.

Returns:
The name of the neighbor.

toString

public java.lang.String toString()

sendPacket

public void sendPacket(Packet packet)
                throws java.security.NoSuchAlgorithmException,
                       javax.crypto.NoSuchPaddingException,
                       java.security.InvalidKeyException,
                       javax.crypto.IllegalBlockSizeException,
                       java.io.IOException
Sends the given packet through socket.

Parameters:
packet - The packet to be sent.
Throws:
javax.crypto.NoSuchPaddingException
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
java.io.IOException
javax.crypto.IllegalBlockSizeException

connect

public void connect(java.net.Socket socket,
                    boolean sendKey)
             throws java.lang.ClassNotFoundException,
                    java.io.IOException,
                    java.security.NoSuchAlgorithmException,
                    javax.crypto.NoSuchPaddingException,
                    java.security.InvalidKeyException
Connects to a neighbor via passed socket

Parameters:
socket - The neighbor's Socket object
sendKey - Whether the encryption key should be sent
Throws:
java.lang.ClassNotFoundException
java.io.IOException
javax.crypto.NoSuchPaddingException
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException

start

public void start()
Start the Neighbor Thread.


stop

public void stop()
Stop the Neighbor Thread.


run

public void run()
While the thread is running, deal with the packets coming in from the associated neighbor.

Specified by:
run in interface java.lang.Runnable

getAddress

public java.lang.String getAddress()
IP Address.

Returns:
The IP address of the Neighbor.

isConnected

public boolean isConnected()
Returns whether or not there is a connection open to this Neighbor.

Returns:
True if connected, false otherwise.

getLocalPort

public int getLocalPort()

isRemoved

public boolean isRemoved()
Indicates whether or not this Neighbor has started yet or has been stopped.

Returns:
True if the neighbor is inactive. False otherwise.

finalize

public void finalize()
              throws java.lang.Throwable
Called by the garbage collector. Just calls stop().

Throws:
java.lang.Throwable