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

java.lang.Object
  extended by uk.org.arlott.simon.hw.f24.lbsta.Network

public class Network
extends java.lang.Object

Network object containing all the LANs, hosts and bridges on the network. Handles adding/removing components and one simulation iteration.


Field Summary
private  java.util.HashMap<java.awt.Point,java.util.HashSet<BridgePort>> bridgePortsIdx
          Index of bridge ports on the network.
private  java.util.HashSet<Bridge> bridges
          Bridges on the network.
private  java.util.HashMap<java.awt.Point,java.util.HashSet<Bridge>> bridgesIdx
          Index of bridges on the network.
private  java.util.Vector<NetworkEvent> events
          Events that were recoreded on the network.
private  java.util.HashSet<Host> hosts
          Hosts on the network.
private  java.util.HashMap<java.awt.Point,Host> hostsHIdx
          Index of horizontal hosts on the network.
private  java.util.HashMap<java.awt.Point,Host> hostsVIdx
          Index of vertical hosts on the network.
private  java.util.HashSet<LAN> lans
          LANs on the network.
private  java.util.HashMap<java.awt.Point,LAN> lansHIdx
          Index of horizontal LANs on the network.
private  java.util.HashMap<java.awt.Point,LAN> lansVIdx
          Index of vertical LANs on the network.
private  java.util.HashSet<NetworkChangeListener> listeners
          Listeners of network objects being changed.
private  java.util.HashSet<Packet> newPackets
          New packets to be added to the network.
private  java.lang.Long now
          Current network simulation time.
private  java.util.HashSet<Packet> packets
          Packets on the network.
private  java.lang.Boolean saved
          Saved status of the network and script.
 
Constructor Summary
Network()
           
 
Method Summary
 boolean add(Bridge obj)
          Adds a bridge to the network.
 boolean add(Bridge obj, boolean test)
          Adds a bridge to the network.
 boolean add(Host obj)
          Adds a host to the network.
 boolean add(Host obj, boolean test)
          Adds a host to the network.
 boolean add(LAN obj)
          Adds a LAN to the network.
 boolean add(LAN obj, boolean test)
          Adds a LAN to the network.
 void addChangeListener(NetworkChangeListener ncl)
          Adds a change listener to those requiring updates of changes to network components.
 void addPacket(Packet packet)
          Adds a packet to the network.
private static boolean detectLoop(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet)
          Detects loops of LANs.
private static boolean detectLoop(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet, java.util.HashSet<LAN> seenSet, LAN parent, LAN current)
          Detects loops of LANs.
 void event(NetworkEvent event)
          Adds a new event to the list of events that have occured.
 java.util.Vector<Bridge> getBridges()
          Gets the bridges on the network.
 LAN getHLAN(java.awt.Point p)
          Gets the horizontal LAN at a given point.
 java.util.Vector<Host> getHosts()
          Gets the hosts on the network.
 java.util.Vector<LAN> getLANs()
          Gets the LANs on the network.
 java.lang.Object getObjectAt(java.awt.Point p)
          Tries to find a component at a given point.
 long getTime()
          Gets the current network simulation time.
 LAN getVLAN(java.awt.Point p)
          Gets the vertical LAN at a given point.
 boolean isSaved()
          Gets the save state of the network.
private  void linkBridgePorts(BridgePort b)
          Links any bridge ports at the same location to a BridgePort
private  void linkBridgePorts(LAN l, int x, int y)
          Links any bridge ports at the given position to a LAN
 void markSaved()
          Marks the network as being saved.
 void networkChanged()
          Marks the network as being changed (not saved).
 void notifyListeners(java.lang.Object o)
          Notifies the current change listeners of a change to a network component.
private static void putCheckSet(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet, LAN l)
          Put l into checkSet for loop detection
private  void queuePacket(java.lang.Object source, PacketDestination destination, PacketTransport location, int direction, PacketData data)
          Queues a packet to be added to the network.
private  void queuePacket(java.util.Vector<Packet> packets, java.lang.Object source)
          Queues packets to be added to the network.
private  void reconnectLANs(LAN a, LAN b)
          Reconnect two LANs together when a dividing LAN has been removed.
 boolean remove(Bridge obj)
          Removes a bridge from the network.
 boolean remove(Host obj, boolean disconnect)
          Removes a Host from the network.
 boolean remove(LAN obj, boolean disconnect)
          Removes a LAN from the network.
 void removeAll(Script script)
          Removes all the network components and resets the script.
 void removeAllPackets()
          Removes all the packets from the network components.
 void removeChangeListener(NetworkChangeListener ncl)
          Removes a change listener from those requiring updates of changes to network components.
 java.util.Vector<NetworkEvent> run()
          Performs one network simulation iteration.
 void setTime(long time)
          Sets the current network simulation time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

listeners

private final java.util.HashSet<NetworkChangeListener> listeners
Listeners of network objects being changed.


lans

private java.util.HashSet<LAN> lans
LANs on the network.


lansHIdx

private java.util.HashMap<java.awt.Point,LAN> lansHIdx
Index of horizontal LANs on the network.


lansVIdx

private java.util.HashMap<java.awt.Point,LAN> lansVIdx
Index of vertical LANs on the network.


hosts

private java.util.HashSet<Host> hosts
Hosts on the network.


hostsHIdx

private java.util.HashMap<java.awt.Point,Host> hostsHIdx
Index of horizontal hosts on the network.


hostsVIdx

private java.util.HashMap<java.awt.Point,Host> hostsVIdx
Index of vertical hosts on the network.


bridges

private java.util.HashSet<Bridge> bridges
Bridges on the network.


bridgesIdx

private java.util.HashMap<java.awt.Point,java.util.HashSet<Bridge>> bridgesIdx
Index of bridges on the network.


bridgePortsIdx

private java.util.HashMap<java.awt.Point,java.util.HashSet<BridgePort>> bridgePortsIdx
Index of bridge ports on the network.


packets

private java.util.HashSet<Packet> packets
Packets on the network.


newPackets

private java.util.HashSet<Packet> newPackets
New packets to be added to the network.


events

private final java.util.Vector<NetworkEvent> events
Events that were recoreded on the network.


saved

private java.lang.Boolean saved
Saved status of the network and script.


now

private java.lang.Long now
Current network simulation time.

Constructor Detail

Network

public Network()
Method Detail

getLANs

public java.util.Vector<LAN> getLANs()
Gets the LANs on the network.

Returns:
All the LANs that are in this network.

getHLAN

public LAN getHLAN(java.awt.Point p)
Gets the horizontal LAN at a given point.

Parameters:
p - The position to check for a LAN.
Returns:
The LAN if found.

getVLAN

public LAN getVLAN(java.awt.Point p)
Gets the vertical LAN at a given point.

Parameters:
p - The position to check for a LAN.
Returns:
The LAN if found.

getHosts

public java.util.Vector<Host> getHosts()
Gets the hosts on the network.

Returns:
All the hosts that are in this network.

getBridges

public java.util.Vector<Bridge> getBridges()
Gets the bridges on the network.

Returns:
All the bridges that are in this network.

removeAll

public void removeAll(Script script)
Removes all the network components and resets the script.

Parameters:
script - The Script to reset.

removeAllPackets

public void removeAllPackets()
Removes all the packets from the network components.


isSaved

public boolean isSaved()
Gets the save state of the network.

Returns:
true if the network has not been modified since it was saved.

markSaved

public void markSaved()
Marks the network as being saved.


networkChanged

public void networkChanged()
Marks the network as being changed (not saved).


getTime

public long getTime()
Gets the current network simulation time.

Returns:
The time in milliseconds.

setTime

public void setTime(long time)
Sets the current network simulation time.

Parameters:
time - The time in milliseconds.

getObjectAt

public java.lang.Object getObjectAt(java.awt.Point p)
Tries to find a component at a given point.

Parameters:
p - The position to check for component.
Returns:
The found network component.

addChangeListener

public void addChangeListener(NetworkChangeListener ncl)
Adds a change listener to those requiring updates of changes to network components.

Parameters:
ncl - A NetworkChangeListener.

removeChangeListener

public void removeChangeListener(NetworkChangeListener ncl)
Removes a change listener from those requiring updates of changes to network components.

Parameters:
ncl - A NetworkChangeListener.

notifyListeners

public void notifyListeners(java.lang.Object o)
Notifies the current change listeners of a change to a network component.

Parameters:
o - The component that has been changed.

add

public boolean add(LAN obj)
            throws LoopException,
                   InvalidPositionException
Adds a LAN to the network.

Parameters:
obj - The LAN to add.
Returns:
true if successful.
Throws:
LoopException - If the new LAN will cause a loop to be formed.
InvalidPositionException - If the LAN is in an invalid position.

add

public boolean add(LAN obj,
                   boolean test)
            throws LoopException,
                   InvalidPositionException
Adds a LAN to the network.

Parameters:
obj - The LAN to add.
test - true if the addition should be checked but not completed.
Returns:
true if successful.
Throws:
LoopException - If the new LAN will cause a loop to be formed.
InvalidPositionException - If the LAN is in an invalid position.

putCheckSet

private static void putCheckSet(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet,
                                LAN l)
Put l into checkSet for loop detection

Parameters:
l - LAN to add
checkSet - HashMap to add LAN to

detectLoop

private static boolean detectLoop(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet)
Detects loops of LANs.

Parameters:
checkSet - The set of LANs to be checked.
Returns:
true if there is a loop.

detectLoop

private static boolean detectLoop(java.util.HashMap<java.awt.Point,java.util.HashSet<LAN>> checkSet,
                                  java.util.HashSet<LAN> seenSet,
                                  LAN parent,
                                  LAN current)
Detects loops of LANs.

Parameters:
checkSet - The set of LANs to be checked.
seenSet - The set of LANs that have been seen.
parent - The previous LAN that was/is being checked.
current - The current LAN being checked.
Returns:
true if there is a loop.

add

public boolean add(Host obj)
            throws InvalidPositionException,
                   TooManyHostsException
Adds a host to the network.

Parameters:
obj - The Host to add.
Returns:
true if successful.
Throws:
InvalidPositionException - If the host is in an invalid position.
TooManyHostsException - If the maximum number of hosts has been reached.

add

public boolean add(Host obj,
                   boolean test)
            throws InvalidPositionException,
                   TooManyHostsException
Adds a host to the network.

Parameters:
obj - The Host to add.
test - true if the addition should be checked but not completed.
Returns:
true if successful.
Throws:
InvalidPositionException - If the host is in an invalid position.
TooManyHostsException - If the maximum number of hosts has been reached.

add

public boolean add(Bridge obj)
            throws InvalidPositionException,
                   TooManyBridgesException
Adds a bridge to the network.

Parameters:
obj - The Bridge to add.
Returns:
true if successful.
Throws:
InvalidPositionException - If the host is in an invalid position.
TooManyBridgesException - If the maximum number of bridges has been reached.

add

public boolean add(Bridge obj,
                   boolean test)
            throws InvalidPositionException,
                   TooManyBridgesException
Adds a bridge to the network.

Parameters:
obj - The Bridge to add.
test - true if the addition should be checked but not completed.
Returns:
true if successful.
Throws:
InvalidPositionException - If the host is in an invalid position.
TooManyBridgesException - If the maximum number of bridges has been reached.

linkBridgePorts

private void linkBridgePorts(LAN l,
                             int x,
                             int y)
Links any bridge ports at the given position to a LAN

Parameters:
l - The LAN to link any found bridge ports to.
x - The X co-ordinate positon to check.
y - The Y co-ordinate positon to check.

linkBridgePorts

private void linkBridgePorts(BridgePort b)
Links any bridge ports at the same location to a BridgePort

Parameters:
b - The BridgePort to link any found bridge ports to.

remove

public boolean remove(LAN obj,
                      boolean disconnect)
Removes a LAN from the network.

Parameters:
obj - The LAN to remove.
disconnect - true if hosts should be disconnected.
Returns:
true if the LAN was in the network.

reconnectLANs

private void reconnectLANs(LAN a,
                           LAN b)
Reconnect two LANs together when a dividing LAN has been removed.

Parameters:
a - The left/top LAN.
b - The right/bottom LAN.

remove

public boolean remove(Host obj,
                      boolean disconnect)
Removes a Host from the network.

Parameters:
obj - The Host to remove.
disconnect - true if the host should be disconnected from its LAN.
Returns:
true if the host was in the network.

remove

public boolean remove(Bridge obj)
Removes a bridge from the network.

Parameters:
obj - The Bridge to remove.
Returns:
true if the bridge was in the network.

run

public java.util.Vector<NetworkEvent> run()
Performs one network simulation iteration.

Returns:
Any new events that occured.

queuePacket

private void queuePacket(java.lang.Object source,
                         PacketDestination destination,
                         PacketTransport location,
                         int direction,
                         PacketData data)
Queues a packet to be added to the network.

Parameters:
source - Source of the packet.
destination - Destination of the packet.
location - Intial location of the packet.
direction - Direction on the location of the packet.
data - The data contained in the packet.

queuePacket

private void queuePacket(java.util.Vector<Packet> packets,
                         java.lang.Object source)
Queues packets to be added to the network.

Parameters:
packets - Packets to be added.
source - Source of the packets.

addPacket

public void addPacket(Packet packet)
Adds a packet to the network.

Parameters:
packet - The Packet to add.

event

public void event(NetworkEvent event)
Adds a new event to the list of events that have occured.

Parameters:
event - The new NetworkEvent to add.