com.alien.enterpriseRFID.discovery
Class NetworkDiscoveryListenerService

java.lang.Object
  extended bycom.alien.enterpriseRFID.discovery.NetworkDiscoveryListenerService
All Implemented Interfaces:
java.lang.Runnable

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

NetworkDiscoveryListenerService is the service that deals with Alien Readers that are appearing and disappearing on the network. Each Alien reader is configured by default to broadcast heartbeat messages over its local subnet. These messages are UDP (User Datagram Protocol) packets containing small XML documents which detail the reader's type, name, and contact information. By listening for these heartbeat messages, the network discovery classes can identify and report back details of readers that exist on the network.

Once the NetworkDiscoveryListenerService class is instantiated and started, it will run in its own thread until it is stopped. While running, the object listens for reader heartbeats, calling either the readerAdded() or readerRenewed() methods of a registered DiscoveryListener when it detects a reader. Part of the heartbeat sent out by the reader indicates the time until the next heartbeat is expected. If this time expires before a new heartbeat is received, then the NetworkDiscoveryListenerService will assume the reader has gone offline and will call the readerRemoved() method.

At any time, the current list of online networked readers can be obtained by calling the getDiscoveryItems() method, which returns an array of DiscoveryItems.

Version:
1.3 Dec 2007
Author:
David Krull

Field Summary
static int DEFAULT_LISTENER_PORT
          The default port on which this service will listen for reader heartbeats, if no port is specified when the service is created.
static int DEFAULT_LISTENER_TIMEOUT_SECONDS
          The default timeout value for opening up the socket to receive reader heartbeat messages on.
 boolean isDebug
           
 
Constructor Summary
NetworkDiscoveryListenerService()
          Creates a new NetworkDiscoveryListenerService, using the default port number.
NetworkDiscoveryListenerService(int listenerPort)
          Creates a new NetworkDiscoveryListenerService, using the specified port number.
 
Method Summary
 void allDiscoveryItemsExpired()
           
 void discoveryItemExpired(DiscoveryItem discoveryItem)
          Removes a DiscoveryItem from the hashtable, and notifies listeners.
 DiscoveryItem[] getDiscoveryItems()
          Returns an array of DiscoveryItems representing all of the readers that this NetworkDiscoveryListenerService knows about.
 DiscoveryListener getDiscoveryListener()
          Returns the object that has been registered by this NetworkDiscoveryListenerService to receive messages when readers are discovered, renewed, or removed from the network.
 int getListenerPort()
          Returns the port number for this service to listen on.
 boolean isRunning()
          Returns true if this NetworkDiscoveryListenerService is running, and false if it is not.
 void run()
          Reads heartbeat messages off the UDP port, and handle the data received.
 void setDiscoveryListener(DiscoveryListener discoveryListener)
          Registers an object with this NetworkDiscoveryListenerService to receive messages when readers are discovered, renewed, or removed from the network.
 void setListenerPort(int listenerPort)
          Sets the port number for this service to listen on.
 void startService()
          Starts up this NetworkDiscoveryListenerService.
 void startService(boolean doPing)
          Starts up this NetworkDiscoveryListenerService.
 void stopService()
          Stops this NetworkDiscoveryListenerService.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LISTENER_PORT

public static final int DEFAULT_LISTENER_PORT
The default port on which this service will listen for reader heartbeats, if no port is specified when the service is created.

See Also:
Constant Field Values

DEFAULT_LISTENER_TIMEOUT_SECONDS

public static final int DEFAULT_LISTENER_TIMEOUT_SECONDS
The default timeout value for opening up the socket to receive reader heartbeat messages on.

See Also:
Constant Field Values

isDebug

public boolean isDebug
Constructor Detail

NetworkDiscoveryListenerService

public NetworkDiscoveryListenerService()
Creates a new NetworkDiscoveryListenerService, using the default port number.


NetworkDiscoveryListenerService

public NetworkDiscoveryListenerService(int listenerPort)
Creates a new NetworkDiscoveryListenerService, using the specified port number. Readers can be directed to send out their heartbeat messages over a different port by using the set HeartbeatPort command.

Parameters:
listenerPort - the port number to listen for heartbeat messages on
Method Detail

getDiscoveryListener

public DiscoveryListener getDiscoveryListener()
Returns the object that has been registered by this NetworkDiscoveryListenerService to receive messages when readers are discovered, renewed, or removed from the network. Registered listeners must implement the DiscoveryListener interface.

Returns:
the registered discovery listener object

setDiscoveryListener

public void setDiscoveryListener(DiscoveryListener discoveryListener)
Registers an object with this NetworkDiscoveryListenerService to receive messages when readers are discovered, renewed, or removed from the network. Registered listeners must implement the DiscoveryListener interface.

Parameters:
discoveryListener - the listening object

startService

public void startService()
                  throws AlienDiscoverySocketException
Starts up this NetworkDiscoveryListenerService. This service will continue to run on its own thread until its stopService() method is called.

This invocation automatically sends out a UDP "heartbeat ping" packet that newer readers can respond to with an immediate heartbeat, which improves discovery time. If you do not wish for this extra packet to be sent, use startService(boolean doPing) instead.

Throws:
AlienDiscoverySocketException - if a socket can not be opened

startService

public void startService(boolean doPing)
                  throws AlienDiscoverySocketException
Starts up this NetworkDiscoveryListenerService. This service will continue to run on its own thread until its stopService() method is called.

The service will optionally send out a UDP "heartbeat ping" packet, depending on the doPing argument. Newer readers can respond to this extra ping packet with an immediate heartbeat, which improves discovery time.

Throws:
AlienDiscoverySocketException - if a socket can not be opened

stopService

public void stopService()
Stops this NetworkDiscoveryListenerService.


run

public void run()
Reads heartbeat messages off the UDP port, and handle the data received. It also checks for expired leases.

Specified by:
run in interface java.lang.Runnable

isRunning

public boolean isRunning()
Returns true if this NetworkDiscoveryListenerService is running, and false if it is not.

Returns:
a boolean to indicate if this service is running

discoveryItemExpired

public void discoveryItemExpired(DiscoveryItem discoveryItem)
Removes a DiscoveryItem from the hashtable, and notifies listeners. This method is public so that external sources can cancel leases... for example if an external source tries to connect and fails, it can cancel the lease that way.

Parameters:
discoveryItem - the item that has expired

allDiscoveryItemsExpired

public void allDiscoveryItemsExpired()

getDiscoveryItems

public DiscoveryItem[] getDiscoveryItems()
Returns an array of DiscoveryItems representing all of the readers that this NetworkDiscoveryListenerService knows about. Items that were discovered by a SerialDiscoveryListenerService are not included in this list.

Returns:
an array of DiscoveryItems

getListenerPort

public int getListenerPort()
Returns the port number for this service to listen on.

Returns:
the current listener port

setListenerPort

public void setListenerPort(int listenerPort)
Sets the port number for this service to listen on. Only takes effect the next time the service is started.

Parameters:
listenerPort - the new port number to listen on.