anthill.antnet
Class UdpGate

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--anthill.antnet.UdpGate
All Implemented Interfaces:
Clearable, Gate, java.lang.Runnable

public class UdpGate
extends java.lang.Thread
implements Gate

A UdpGate is an implementation of the Gate interface based on UDP. UdpGate creates a thread listening to a datagram socket, and delivers received agents to the socket it receives.

Version:
$Revision$
Author:
Alberto Montresor

Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
UdpGate(int port)
          Constructs a new UdpGate by instantiating a new datagram socket listening on the port contained in the anthill identifier.
 
Method Summary
 void clear()
          Restores the object to its initial state.
 NestId getId()
          Returns the identifier of the Nest to which this gate is associated.
 void run()
          While-true loop waiting for a packet arriving from the datagram socket; the received packet is delivered to the agent listener object.
 void send(NestId id, Ant ant, int ttl)
          Send an agent to the specified anthill.
 void setAntListener(AntListener listener)
          Sets the ant listener, i.e. the object that will accept the ants coming from other nests.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

UdpGate

public UdpGate(int port)
        throws java.io.IOException
Constructs a new UdpGate by instantiating a new datagram socket listening on the port contained in the anthill identifier.
Method Detail

getId

public NestId getId()
Returns the identifier of the Nest to which this gate is associated.
Specified by:
getId in interface Gate

setAntListener

public void setAntListener(AntListener listener)
Description copied from interface: Gate
Sets the ant listener, i.e. the object that will accept the ants coming from other nests.
Specified by:
setAntListener in interface Gate
Following copied from interface: anthill.nest.Gate
Parameters:
listener - the listener object that will accepts the ants coming from other nests

send

public void send(NestId id,
                 Ant ant,
                 int ttl)
          throws java.io.IOException,
                 java.lang.ClassCastException
Send an agent to the specified anthill. Both the anthill identifier and the agent must be serializable or externalizable.
Specified by:
send in interface Gate
Throws:
java.lang.ClassCastException - raised if the anthill identifier does not belong to the expected class (in this case, UdpNid)
java.io.IOException - raised if something goes wrong during serialization and sending

run

public void run()
While-true loop waiting for a packet arriving from the datagram socket; the received packet is delivered to the agent listener object.
Overrides:
run in class java.lang.Thread

clear

public void clear()
Description copied from interface: Clearable
Restores the object to its initial state.
Specified by:
clear in interface Clearable