uk.org.arlott.simon.hw.f24.lbsta
Class Host

java.lang.Object
  extended by uk.org.arlott.simon.hw.f24.lbsta.Host
All Implemented Interfaces:
PacketDestination, PacketTransport

public class Host
extends java.lang.Object
implements PacketTransport, PacketDestination

Represents a host on the network.


Field Summary
private  int address
          The address of this host.
private static boolean[] allAddresses
          The addresses currently in use.
private  boolean inNetwork
          Whether or not this host is currently in the network or not.
 LAN lan
          The LAN this host is connected to.
private static int lastAssigned
          The last assigned address.
private  Log log
          The event log.
static int MAX_ADDRESSES
          The maximum number of host addresses.
private  Network network
          The network this host is on.
private  java.util.HashSet<Packet> packets
          The packets attached to this host.
private  boolean randomPackets
          Whether or not this host should send packets at random.
 int x
          The X co-ordinate position of this host.
 int y
          The X co-ordinate position of this host.
 
Constructor Summary
Host(Host host, LAN lan, Network network)
          Creates a new Host object.
Host(Host host, Network network)
          Creates a new Host object.
Host(int x, int y, LAN lan, Network network)
          Creates a new Host object.
 
Method Summary
 void addPacket(Packet p)
          Adds a packet to this host.
 void addToNetwork()
          Adds this host to the network.
 void enableRandomPackets(boolean randomPackets)
          Sets the status of the random packets mode.
private static int findFreeAddress()
          Finds an unused host address.
 int getAddress()
          Gets the address.
 java.lang.String[] getLog()
          Gets the log entries for this host.
 java.util.Vector<Packet> getPackets()
          Gets all the packets on this host.
 boolean isRandomPacketsEnabled()
          Gets the status of the random packets mode.
 void networkChanged()
          Indicates that the network has been modified.
 java.util.Vector<Packet> processPacket(Packet p)
          Processes a packet received by this host.
 void removeAllPackets()
          Removes all the packets from this host.
 void removeFromNetwork()
          Removes this host from the network.
 void removePacket(Packet p)
          Removes a packet from this host.
 void reset()
          Resets this host's log.
private  boolean setAddress(Host h, int newAddress)
          Sets the address of a host.
 boolean setAddress(int newAddress)
          Sets the address.
private static boolean setInNetwork(Host h, boolean inNetwork)
          Updates the in network state of a host.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

private Log log
The event log.


packets

private java.util.HashSet<Packet> packets
The packets attached to this host.


network

private Network network
The network this host is on.


lan

public LAN lan
The LAN this host is connected to.


x

public int x
The X co-ordinate position of this host.


y

public int y
The X co-ordinate position of this host.


MAX_ADDRESSES

public static final int MAX_ADDRESSES
The maximum number of host addresses.

See Also:
Constant Field Values

allAddresses

private static boolean[] allAddresses
The addresses currently in use.


lastAssigned

private static int lastAssigned
The last assigned address.


address

private int address
The address of this host.


inNetwork

private boolean inNetwork
Whether or not this host is currently in the network or not.


randomPackets

private boolean randomPackets
Whether or not this host should send packets at random.

Constructor Detail

Host

public Host(int x,
            int y,
            LAN lan,
            Network network)
     throws InvalidPositionException
Creates a new Host object.

Parameters:
x - X position.
y - Y position.
lan - The LAN this host is attached to.
network - The Network this host is on.
Throws:
InvalidPositionException - If the position is invalid.

Host

public Host(Host host,
            LAN lan,
            Network network)
     throws InvalidPositionException
Creates a new Host object.

Parameters:
host - An existing Host object to copy.
lan - The LAN this host is attached to.
network - The Network this host is on.
Throws:
InvalidPositionException - If the position is invalid.

Host

public Host(Host host,
            Network network)
     throws InvalidPositionException
Creates a new Host object.

Parameters:
host - An existing Host object to copy.
network - The Network this host is on.
Throws:
InvalidPositionException - If the position is invalid.
Method Detail

networkChanged

public void networkChanged()
Indicates that the network has been modified.


addToNetwork

public void addToNetwork()
                  throws TooManyHostsException
Adds this host to the network.

Throws:
TooManyHostsException - If there are too many hosts on the network already.

removeFromNetwork

public void removeFromNetwork()
Removes this host from the network.


reset

public void reset()
Resets this host's log.


getLog

public java.lang.String[] getLog()
Gets the log entries for this host.

Returns:
The log as an array of strings.

setInNetwork

private static boolean setInNetwork(Host h,
                                    boolean inNetwork)
Updates the in network state of a host.

Parameters:
h - The Host to modify.
inNetwork - Whether or not the host is in the network or not.
Returns:
true if the action was successful.

findFreeAddress

private static int findFreeAddress()
Finds an unused host address.

Returns:
The next free host address.

getAddress

public int getAddress()
Gets the address.

Returns:
This host's address.

setAddress

public boolean setAddress(int newAddress)
Sets the address.

Parameters:
newAddress - The new address for the host.
Returns:
true if the address is not already in use.

setAddress

private boolean setAddress(Host h,
                           int newAddress)
Sets the address of a host.

Parameters:
h - The Host to modify.
newAddress - The new address of the host.
Returns:
true if the address is not already in use.

isRandomPacketsEnabled

public boolean isRandomPacketsEnabled()
Gets the status of the random packets mode.

Returns:
true if enabled.

enableRandomPackets

public void enableRandomPackets(boolean randomPackets)
Sets the status of the random packets mode.

Parameters:
randomPackets - true to enable or false to disable.

addPacket

public void addPacket(Packet p)
Adds a packet to this host.

Specified by:
addPacket in interface PacketTransport
Parameters:
p - The Packet to add.

removePacket

public void removePacket(Packet p)
Removes a packet from this host.

Specified by:
removePacket in interface PacketTransport
Parameters:
p - The Packet to remove.

getPackets

public java.util.Vector<Packet> getPackets()
Gets all the packets on this host.

Returns:
The packets on this host.

removeAllPackets

public void removeAllPackets()
Removes all the packets from this host.


processPacket

public java.util.Vector<Packet> processPacket(Packet p)
Processes a packet received by this host.

Specified by:
processPacket in interface PacketDestination
Parameters:
p - The Packet to process.
Returns:
Any new packets that the processing caused.