org.kaariboga.io
Class DeliveryService

java.lang.Object
  |
  +--org.kaariboga.io.DeliveryService
All Implemented Interfaces:
KbMessageHandler

public class DeliveryService
extends java.lang.Object
implements KbMessageHandler

The DeliveryService receives messages and dispatches them to other bases. At the moment messages are delivered using TCP/IP socket connections, but it future other connections or protocols may be supported. For example a DeliveryService might use HTTP to tunnel through a firewall.


Field Summary
(package private)  ListenThread listenThread
          Thread listening for incoming messages
protected  int port
          Port on which this service receives messages
protected  java.util.LinkedList receivingThreads
          List of all threads currently receiving messages
protected  java.util.LinkedList sendingThreads
          List of all threads currently sending messages
protected  KbMessageHandler user
          This user is contacted in case something goes wrong with a posted message.
 
Fields inherited from interface org.kaariboga.io.KbMessageHandler
AGENT_NOT_REACHABLE, BASE_NOT_REACHABLE, BASE_OFFLINE
 
Constructor Summary
DeliveryService(KbMessageHandler user, int port)
          Creates a new delivery service.
 
Method Summary
 void goOffline()
          Removes connection to the internet.
 void goOnline()
          Connects to the internet.
 void handleMessage(KaaribogaMessage message)
          Called by a receiving thread when a message has arrived.
 void messageDeliveryError(int errorCode, KaaribogaMessage message)
          Called in case a message could not be delivered.
 void postMessage(KaaribogaMessage message)
          Delivers this message to the destination.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

user

protected KbMessageHandler user
This user is contacted in case something goes wrong with a posted message.

port

protected int port
Port on which this service receives messages

sendingThreads

protected java.util.LinkedList sendingThreads
List of all threads currently sending messages

receivingThreads

protected java.util.LinkedList receivingThreads
List of all threads currently receiving messages

listenThread

ListenThread listenThread
Thread listening for incoming messages
Constructor Detail

DeliveryService

public DeliveryService(KbMessageHandler user,
                       int port)
Creates a new delivery service. To go online call goOnline() first.
Parameters:
user - A user of this service, who receives error messages.
port - The network port, this receiver should listen on.
Method Detail

postMessage

public void postMessage(KaaribogaMessage message)
Delivers this message to the destination. Note, that there is no method parameter for a DeliveryServiceUser. On errors allways the global DeliveryServiceUser is notified. This is becaus agents should never post messages directy. They should use the underlying base. This method has no return parameter for success of the operation, because messages are handled asynchronously and it can't be determined at call time, if the deliveery succeeds. On error the DeliveryServiceUser is notified.
Parameters:
message - The message, which should be transfered. The message allready contains sender and recipient.

handleMessage

public void handleMessage(KaaribogaMessage message)
Called by a receiving thread when a message has arrived.
Specified by:
handleMessage in interface KbMessageHandler
Parameters:
message - The message, that has arrived.

messageDeliveryError

public void messageDeliveryError(int errorCode,
                                 KaaribogaMessage message)
Called in case a message could not be delivered. It is not guaranteed, that this method is called in every case, for example if a DeliveryService is offline, it may decide to store incoming messages until it is online again.
Specified by:
messageDeliveryError in interface KbMessageHandler
Parameters:
errorCode - Delivery error code. At the moment the following codes exit: BASE_NOT_REACHABLE, AGENT_NOT_REACHABLE, BASE_OFFLINE
message - The message, that could not be delivered. Note, that this may be null if the message no longer exists on the sending host. Future versions of KaaribogaMessage may include a global unique identifier.

goOnline

public void goOnline()
Connects to the internet.

goOffline

public void goOffline()
Removes connection to the internet.