routing
Class MessageRouter

java.lang.Object
  extended by routing.MessageRouter
Direct Known Subclasses:
ActiveRouter, PassiveRouter

public abstract class MessageRouter
extends java.lang.Object

Superclass for message routers.


Field Summary
static java.lang.String B_SIZE_S
          Message buffer size -setting id ("bufferSize").
static int DENIED_NO_SPACE
          Receive return value for not enough space in the buffer for the msg
static int DENIED_OLD
          Receive return value for an old (already received) message
static int DENIED_TTL
          Receive return value for messages whose TTL has expired
static int DENIED_UNSPECIFIED
          Receive return value for unspecified reason
static java.lang.String MSG_TTL_S
          Message TTL -setting id ("msgTtl").
protected  int msgTtl
          TTL for all messages
static int Q_MODE_FIFO
          Setting value for FIFO queue mode
static int Q_MODE_RANDOM
          Setting value for random queue mode
static int RCV_OK
          Receive return value for OK
static java.lang.String SEND_QUEUE_MODE_S
          Message/fragment sending queue type -setting id ("sendQueue").
static int TRY_LATER_BUSY
          Receive return value for busy receiver
 
Constructor Summary
protected MessageRouter(MessageRouter r)
          Copy-constructor.
  MessageRouter(Settings s)
          Constructor.
 
Method Summary
 void addApplication(Application app)
          Adds an application to the attached applications list.
protected  void addToMessages(Message m, boolean newMessage)
          Adds a message to the message buffer and informs message listeners about new message (if requested).
abstract  void changedConnection(Connection con)
          Informs the router about change in connections state.
protected  int compareByQueueMode(Message m1, Message m2)
          Gives the order of the two given messages as defined by the current queue mode
 boolean createNewMessage(Message m)
          Creates a new message to the router.
 void deleteMessage(java.lang.String id, boolean drop)
          Deletes a message from the buffer and informs message listeners about the event
 java.util.Collection<Application> getApplications(java.lang.String ID)
          Returns all the applications that want to receive messages for the given application ID.
 int getBufferSize()
          Returns the size of the message buffer.
 int getFreeBufferSize()
          Returns the amount of free space in the buffer.
protected  DTNHost getHost()
          Returns the host this router is in
protected  Message getMessage(java.lang.String id)
          Returns a message by ID.
 java.util.Collection<Message> getMessageCollection()
          Returns a reference to the messages of this router in collection.
 int getNrofMessages()
          Returns the number of messages this router has
 RoutingInfo getRoutingInfo()
          Returns routing information about this router.
protected  boolean hasMessage(java.lang.String id)
          Checks if this router has a message with certain id buffered.
 void init(DTNHost host, java.util.List<MessageListener> mListeners)
          Initializes the router; i.e.
protected  boolean isDeliveredMessage(Message m)
          Returns true if a full message with same ID as the given message has been received by this host as the final recipient (at least once).
protected  boolean isIncomingMessage(java.lang.String id)
          Returns true if a message with the given ID is one of the currently incoming messages, false if not
 void messageAborted(java.lang.String id, DTNHost from, int bytesRemaining)
          This method should be called (on the receiving host) when a message transfer was aborted.
 Message messageTransferred(java.lang.String id, DTNHost from)
          This method should be called (on the receiving host) after a message was successfully transferred.
protected  void putToIncomingBuffer(Message m, DTNHost from)
          Puts a message to incoming messages buffer.
 int receiveMessage(Message m, DTNHost from)
          Try to start receiving a message from another host.
protected  Message removeFromIncomingBuffer(java.lang.String id, DTNHost from)
          Removes and returns a message with a certain ID from the incoming messages buffer or null if such message wasn't found.
protected  Message removeFromMessages(java.lang.String id)
          Removes and returns a message from the message buffer.
abstract  MessageRouter replicate()
          Creates a replicate of this router.
 boolean requestDeliverableMessages(Connection con)
          Requests for deliverable message from this router to be sent trough a connection.
 void sendMessage(java.lang.String id, DTNHost to)
          Start sending a message to another host.
protected  java.util.List sortByQueueMode(java.util.List list)
          Sorts/shuffles the given list according to the current sending queue mode.
 java.lang.String toString()
          Returns a String presentation of this router
 void update()
          Updates router.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

B_SIZE_S

public static final java.lang.String B_SIZE_S
Message buffer size -setting id ("bufferSize"). Integer value in bytes.

See Also:
Constant Field Values

MSG_TTL_S

public static final java.lang.String MSG_TTL_S
Message TTL -setting id ("msgTtl"). Value is in minutes and must be an integer.

See Also:
Constant Field Values

SEND_QUEUE_MODE_S

public static final java.lang.String SEND_QUEUE_MODE_S
Message/fragment sending queue type -setting id ("sendQueue"). This setting affects the order the messages and fragments are sent if the routing protocol doesn't define any particular order (e.g, if more than one message can be sent directly to the final recipient). Valid values are

See Also:
Constant Field Values

Q_MODE_RANDOM

public static final int Q_MODE_RANDOM
Setting value for random queue mode

See Also:
Constant Field Values

Q_MODE_FIFO

public static final int Q_MODE_FIFO
Setting value for FIFO queue mode

See Also:
Constant Field Values

RCV_OK

public static final int RCV_OK
Receive return value for OK

See Also:
Constant Field Values

TRY_LATER_BUSY

public static final int TRY_LATER_BUSY
Receive return value for busy receiver

See Also:
Constant Field Values

DENIED_OLD

public static final int DENIED_OLD
Receive return value for an old (already received) message

See Also:
Constant Field Values

DENIED_NO_SPACE

public static final int DENIED_NO_SPACE
Receive return value for not enough space in the buffer for the msg

See Also:
Constant Field Values

DENIED_TTL

public static final int DENIED_TTL
Receive return value for messages whose TTL has expired

See Also:
Constant Field Values

DENIED_UNSPECIFIED

public static final int DENIED_UNSPECIFIED
Receive return value for unspecified reason

See Also:
Constant Field Values

msgTtl

protected int msgTtl
TTL for all messages

Constructor Detail

MessageRouter

public MessageRouter(Settings s)
Constructor. Creates a new message router based on the settings in the given Settings object. Size of the message buffer is read from B_SIZE_S setting. Default value is Integer.MAX_VALUE.

Parameters:
s - The settings object

MessageRouter

protected MessageRouter(MessageRouter r)
Copy-constructor.

Parameters:
r - Router to copy the settings from.
Method Detail

init

public void init(DTNHost host,
                 java.util.List<MessageListener> mListeners)
Initializes the router; i.e. sets the host this router is in and message listeners that need to be informed about message related events etc.

Parameters:
host - The host this router is in
mListeners - The message listeners

update

public void update()
Updates router. This method should be called (at least once) on every simulation interval to update the status of transfer(s).


changedConnection

public abstract void changedConnection(Connection con)
Informs the router about change in connections state.

Parameters:
con - The connection that changed

getMessage

protected Message getMessage(java.lang.String id)
Returns a message by ID.

Parameters:
id - ID of the message
Returns:
The message

hasMessage

protected boolean hasMessage(java.lang.String id)
Checks if this router has a message with certain id buffered.

Parameters:
id - Identifier of the message
Returns:
True if the router has message with this id, false if not

isDeliveredMessage

protected boolean isDeliveredMessage(Message m)
Returns true if a full message with same ID as the given message has been received by this host as the final recipient (at least once).

Parameters:
m - message we're interested of
Returns:
true if a message with the same ID has been received by this host as the final recipient.

getMessageCollection

public java.util.Collection<Message> getMessageCollection()
Returns a reference to the messages of this router in collection. Note: If there's a chance that some message(s) from the collection could be deleted (or added) while iterating through the collection, a copy of the collection should be made to avoid concurrent modification exceptions.

Returns:
a reference to the messages of this router in collection

getNrofMessages

public int getNrofMessages()
Returns the number of messages this router has

Returns:
How many messages this router has

getBufferSize

public int getBufferSize()
Returns the size of the message buffer.

Returns:
The size or Integer.MAX_VALUE if the size isn't defined.

getFreeBufferSize

public int getFreeBufferSize()
Returns the amount of free space in the buffer. May return a negative value if there are more messages in the buffer than should fit there (because of creating new messages).

Returns:
The amount of free space (Integer.MAX_VALUE if the buffer size isn't defined)

getHost

protected DTNHost getHost()
Returns the host this router is in

Returns:
The host object

sendMessage

public void sendMessage(java.lang.String id,
                        DTNHost to)
Start sending a message to another host.

Parameters:
id - Id of the message to send
to - The host to send the message to

requestDeliverableMessages

public boolean requestDeliverableMessages(Connection con)
Requests for deliverable message from this router to be sent trough a connection.

Parameters:
con - The connection to send the messages trough
Returns:
True if this router started a transfer, false if not

receiveMessage

public int receiveMessage(Message m,
                          DTNHost from)
Try to start receiving a message from another host.

Parameters:
m - Message to put in the receiving buffer
from - Who the message is from
Returns:
Value zero if the node accepted the message (RCV_OK), value less than zero if node rejected the message (e.g. DENIED_OLD), value bigger than zero if the other node should try later (e.g. TRY_LATER_BUSY).

messageTransferred

public Message messageTransferred(java.lang.String id,
                                  DTNHost from)
This method should be called (on the receiving host) after a message was successfully transferred. The transferred message is put to the message buffer unless this host is the final recipient of the message.

Parameters:
id - Id of the transferred message
from - Host the message was from (previous hop)
Returns:
The message that this host received

putToIncomingBuffer

protected void putToIncomingBuffer(Message m,
                                   DTNHost from)
Puts a message to incoming messages buffer. Two messages with the same ID are distinguished by the from host.

Parameters:
m - The message to put
from - Who the message was from (previous hop).

removeFromIncomingBuffer

protected Message removeFromIncomingBuffer(java.lang.String id,
                                           DTNHost from)
Removes and returns a message with a certain ID from the incoming messages buffer or null if such message wasn't found.

Parameters:
id - ID of the message
from - The host that sent this message (previous hop)
Returns:
The found message or null if such message wasn't found

isIncomingMessage

protected boolean isIncomingMessage(java.lang.String id)
Returns true if a message with the given ID is one of the currently incoming messages, false if not

Parameters:
id - ID of the message
Returns:
True if such message is incoming right now

addToMessages

protected void addToMessages(Message m,
                             boolean newMessage)
Adds a message to the message buffer and informs message listeners about new message (if requested).

Parameters:
m - The message to add
newMessage - If true, message listeners are informed about a new message, if false, nothing is informed.

removeFromMessages

protected Message removeFromMessages(java.lang.String id)
Removes and returns a message from the message buffer.

Parameters:
id - Identifier of the message to remove
Returns:
The removed message or null if message for the ID wasn't found

messageAborted

public void messageAborted(java.lang.String id,
                           DTNHost from,
                           int bytesRemaining)
This method should be called (on the receiving host) when a message transfer was aborted.

Parameters:
id - Id of the message that was being transferred
from - Host the message was from (previous hop)
bytesRemaining - Nrof bytes that were left before the transfer would have been ready; or -1 if the number of bytes is not known

createNewMessage

public boolean createNewMessage(Message m)
Creates a new message to the router.

Parameters:
m - The message to create
Returns:
True if the creation succeeded, false if not (e.g. the message was too big for the buffer)

deleteMessage

public void deleteMessage(java.lang.String id,
                          boolean drop)
Deletes a message from the buffer and informs message listeners about the event

Parameters:
id - Identifier of the message to delete
drop - If the message is dropped (e.g. because of full buffer) this should be set to true. False value indicates e.g. remove of message because it was delivered to final destination.

sortByQueueMode

protected java.util.List sortByQueueMode(java.util.List list)
Sorts/shuffles the given list according to the current sending queue mode. The list can contain either Message or Tuple objects. Other objects cause error.

Parameters:
list - The list to sort or shuffle
Returns:
The sorted/shuffled list

compareByQueueMode

protected int compareByQueueMode(Message m1,
                                 Message m2)
Gives the order of the two given messages as defined by the current queue mode

Parameters:
m1 - The first message
m2 - The second message
Returns:
-1 if the first message should come first, 1 if the second message should come first, or 0 if the ordering isn't defined

getRoutingInfo

public RoutingInfo getRoutingInfo()
Returns routing information about this router.

Returns:
The routing information.

addApplication

public void addApplication(Application app)
Adds an application to the attached applications list.

Parameters:
app - The application to attach to this router.

getApplications

public java.util.Collection<Application> getApplications(java.lang.String ID)
Returns all the applications that want to receive messages for the given application ID.

Parameters:
ID - The application ID or null for all apps.
Returns:
A list of all applications that want to receive the message.

replicate

public abstract MessageRouter replicate()
Creates a replicate of this router. The replicate has the same settings as this router but empty buffers and routing tables.

Returns:
The replicate

toString

public java.lang.String toString()
Returns a String presentation of this router

Overrides:
toString in class java.lang.Object
Returns:
A String presentation of this router