anthill.nestor
Class LocalTrail

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--anthill.nestor.LocalTrail
All Implemented Interfaces:
java.lang.Runnable, Trail

public class LocalTrail
extends java.lang.Thread
implements Trail

Class LocalTrail is the implementation of Trail to be used during off-line simulations. Programmers are enabled to specify the maximum number of associations (ant identifier, previous nest identifier) which can be stored in a LocalTrail object, or the maximum duration of such an associations in a LocalTrail object, or both. Assocations which exceed the maximum capacity or are older than the maximum duration are discarded from this LocalTrail.


Inner Class Summary
protected  class LocalTrail.Pair
          The Pair class is an inner class used to link objects in the hash table in a list.
 
Field Summary
protected  LocalTrail.Pair head
          Head of the list connecting the pairs; pairs will be removed here
protected  java.util.HashMap map
          Hash table mapping ant identifiers to the nest from which the ant has arrived.
protected  LocalTrail.Pair tail
          Tail of the list connecting the pairs; pairs will be added here
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
LocalTrail(int maxsize, int maxtime, int interval)
          Instantiate a new Trail object by setting the maximum number of pair (ant id, source) which may be contained in it.
 
Method Summary
 boolean add(AntId id, NestId source)
          Add a pair (id, source) to the trail object containing the host.
 void end()
          Close the thread associated to this trail.
 NestId lookup(AntId id)
          Lookup this LocalTrail object in order to obtain the identifier of the nest from which the specified ant arrived.
 void run()
          Periodically scans the linked list of pairs in order to remove staled pairs (ant id, nest id).
 
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
 

Field Detail

map

protected java.util.HashMap map
Hash table mapping ant identifiers to the nest from which the ant has arrived.

head

protected LocalTrail.Pair head
Head of the list connecting the pairs; pairs will be removed here

tail

protected LocalTrail.Pair tail
Tail of the list connecting the pairs; pairs will be added here
Constructor Detail

LocalTrail

public LocalTrail(int maxsize,
                  int maxtime,
                  int interval)
Instantiate a new Trail object by setting the maximum number of pair (ant id, source) which may be contained in it.
Parameters:
maxsize - maximum number of pairs (ant id, source) which may be contained in this Trail object. If zero, the number of pairs is not limited.
maxtime - maximum time (in milliseconds) of permanence of a pair in this Trail object. If zero, the maximum time of permanence is not limited.
interval - polling time for obsolete pair removals
Method Detail

add

public boolean add(AntId id,
                   NestId source)
Add a pair (id, source) to the trail object containing the host. Returns true if the ant identifier is not already included in this Trail object; return false otherwise. The "false" notification can be used to detect duplicated visit to a nest.
Specified by:
add in interface Trail

lookup

public NestId lookup(AntId id)
Lookup this LocalTrail object in order to obtain the identifier of the nest from which the specified ant arrived. The pair associating the ant identifier to the source nest is removed from both the hash table and the linked list.
Specified by:
lookup in interface Trail

run

public void run()
Periodically scans the linked list of pairs in order to remove staled pairs (ant id, nest id).
Overrides:
run in class java.lang.Thread

end

public void end()
Close the thread associated to this trail.