routing
Class ActiveRouter

java.lang.Object
  extended by routing.MessageRouter
      extended by routing.ActiveRouter
Direct Known Subclasses:
DirectDeliveryRouter, EnergyAwareRouter, EpidemicOracleRouter, EpidemicRouter, FirstContactRouter, MaxPropRouter, MaxPropRouterWithEstimation, ProphetRouter, ProphetRouterWithEstimation, SprayAndWaitRouter

public abstract class ActiveRouter
extends MessageRouter

Superclass of active routers. Contains convenience methods (e.g. getOldestMessage(boolean)) and watching of sending connections (see update()).


Field Summary
static java.lang.String DELETE_DELIVERED_S
          Delete delivered messages -setting id ("deleteDelivered").
protected  boolean deleteDelivered
          should messages that final recipient marks as delivered be deleted from message buffer
static java.lang.String RESPONSE_PREFIX
          prefix of all response message IDs
protected  java.util.ArrayList<Connection> sendingConnections
          connection(s) that are currently used for sending
static int TTL_CHECK_INTERVAL
          how often TTL check (discarding old messages) is performed
 
Fields inherited from class routing.MessageRouter
B_SIZE_S, DENIED_NO_SPACE, DENIED_OLD, DENIED_TTL, DENIED_UNSPECIFIED, MSG_TTL_S, msgTtl, Q_MODE_FIFO, Q_MODE_RANDOM, RCV_OK, SEND_QUEUE_MODE_S, TRY_LATER_BUSY
 
Constructor Summary
protected ActiveRouter(ActiveRouter r)
          Copy constructor.
  ActiveRouter(Settings s)
          Constructor.
 
Method Summary
protected  void addToSendingConnections(Connection con)
          Adds a connections to sending connections which are monitored in the update.
protected  boolean canStartTransfer()
          Makes rudimentary checks (that we have at least one message and one connection) about can this router start transfer.
 void changedConnection(Connection con)
          Called when a connection's state changes.
protected  int checkReceiving(Message m)
          Checks if router "wants" to start receiving message (i.e.
 boolean createNewMessage(Message m)
          Creates a new message to the router.
protected  void dropExpiredMessages()
          Drops messages whose TTL is less than zero.
protected  Connection exchangeDeliverableMessages()
          Exchanges deliverable (to final recipient) messages between this host and all hosts this host is currently connected to.
protected  java.util.List<Connection> getConnections()
          Returns a list of connections this host currently has with other hosts.
protected  java.util.List<Tuple<Message,Connection>> getMessagesForConnected()
          Returns a list of message-connections tuples of the messages whose recipient is some host that we're connected to at the moment.
protected  Message getOldestMessage(boolean excludeMsgBeingSent)
          Returns the oldest (by receive time) message in the message buffer (that is not being sent if excludeMsgBeingSent is true).
 void init(DTNHost host, java.util.List<MessageListener> mListeners)
          Initializes the router; i.e.
 boolean isSending(java.lang.String msgId)
          Returns true if this router is currently sending a message with msgId.
 boolean isTransferring()
          Returns true if this router is transferring something at the moment or some transfer has not been finalized.
protected  boolean makeRoomForMessage(int size)
          Removes messages from the buffer (oldest first) until there's enough space for the new message.
protected  void makeRoomForNewMessage(int size)
          Tries to make room for a new message.
 Message messageTransferred(java.lang.String id, DTNHost from)
          This method should be called (on the receiving host) after a message was successfully transferred.
 int receiveMessage(Message m, DTNHost from)
          Try to start receiving a message from another host.
 boolean requestDeliverableMessages(Connection con)
          Requests for deliverable message from this router to be sent trough a connection.
protected  void shuffleMessages(java.util.List<Message> messages)
          Shuffles a messages list so the messages are in random order.
protected  int startTransfer(Message m, Connection con)
          Tries to start a transfer of message using a connection.
protected  void transferAborted(Connection con)
          Method is called just before a transfer is aborted at update() due connection going down.
protected  void transferDone(Connection con)
          Method is called just before a transfer is finalized at update().
protected  Message tryAllMessages(Connection con, java.util.List<Message> messages)
          Goes trough the messages until the other node accepts one for receiving (or doesn't accept any).
protected  Connection tryAllMessagesToAllConnections()
          Tries to send all messages that this router is carrying to all connections this node has.
protected  Tuple<Message,Connection> tryMessagesForConnected(java.util.List<Tuple<Message,Connection>> tuples)
          Tries to send messages for the connections that are mentioned in the Tuples in the order they are in the list until one of the connections starts transferring or all tuples have been tried.
protected  Connection tryMessagesToConnections(java.util.List<Message> messages, java.util.List<Connection> connections)
          Tries to send all given messages to all given connections.
 void update()
          Checks out all sending connections to finalize the ready ones and abort those whose connection went down.
 
Methods inherited from class routing.MessageRouter
addApplication, addToMessages, compareByQueueMode, deleteMessage, getApplications, getBufferSize, getFreeBufferSize, getHost, getMessage, getMessageCollection, getNrofMessages, getRoutingInfo, hasMessage, isDeliveredMessage, isIncomingMessage, messageAborted, putToIncomingBuffer, removeFromIncomingBuffer, removeFromMessages, replicate, sendMessage, sortByQueueMode, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DELETE_DELIVERED_S

public static final java.lang.String DELETE_DELIVERED_S
Delete delivered messages -setting id ("deleteDelivered"). Boolean valued. If set to true and final recipient of a message rejects it because it already has it, the message is deleted from buffer. Default=false.

See Also:
Constant Field Values

deleteDelivered

protected boolean deleteDelivered
should messages that final recipient marks as delivered be deleted from message buffer


RESPONSE_PREFIX

public static final java.lang.String RESPONSE_PREFIX
prefix of all response message IDs

See Also:
Constant Field Values

TTL_CHECK_INTERVAL

public static int TTL_CHECK_INTERVAL
how often TTL check (discarding old messages) is performed


sendingConnections

protected java.util.ArrayList<Connection> sendingConnections
connection(s) that are currently used for sending

Constructor Detail

ActiveRouter

public ActiveRouter(Settings s)
Constructor. Creates a new message router based on the settings in the given Settings object.

Parameters:
s - The settings object

ActiveRouter

protected ActiveRouter(ActiveRouter r)
Copy constructor.

Parameters:
r - The router prototype where setting values are copied from
Method Detail

init

public void init(DTNHost host,
                 java.util.List<MessageListener> mListeners)
Description copied from class: MessageRouter
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.

Overrides:
init in class MessageRouter
Parameters:
host - The host this router is in
mListeners - The message listeners

changedConnection

public void changedConnection(Connection con)
Called when a connection's state changes. This version doesn't do anything but subclasses may want to override this.

Specified by:
changedConnection in class MessageRouter
Parameters:
con - The connection that changed

requestDeliverableMessages

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

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

createNewMessage

public boolean createNewMessage(Message m)
Description copied from class: MessageRouter
Creates a new message to the router.

Overrides:
createNewMessage in class MessageRouter
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)

receiveMessage

public int receiveMessage(Message m,
                          DTNHost from)
Description copied from class: MessageRouter
Try to start receiving a message from another host.

Overrides:
receiveMessage in class MessageRouter
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)
Description copied from class: MessageRouter
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.

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

getConnections

protected java.util.List<Connection> getConnections()
Returns a list of connections this host currently has with other hosts.

Returns:
a list of connections this host currently has with other hosts

startTransfer

protected int startTransfer(Message m,
                            Connection con)
Tries to start a transfer of message using a connection. Is starting succeeds, the connection is added to the watch list of active connections

Parameters:
m - The message to transfer
con - The connection to use
Returns:
the value returned by Connection.startTransfer(DTNHost, Message)

canStartTransfer

protected boolean canStartTransfer()
Makes rudimentary checks (that we have at least one message and one connection) about can this router start transfer.

Returns:
True if router can start transfer, false if not

checkReceiving

protected int checkReceiving(Message m)
Checks if router "wants" to start receiving message (i.e. router isn't transferring, doesn't have the message and has room for it).

Parameters:
m - The message to check
Returns:
A return code similar to MessageRouter.receiveMessage(Message, DTNHost), i.e. MessageRouter.RCV_OK if receiving seems to be OK, TRY_LATER_BUSY if router is transferring, DENIED_OLD if the router is already carrying the message or it has been delivered to this router (as final recipient), or DENIED_NO_SPACE if the message does not fit into buffer

makeRoomForMessage

protected boolean makeRoomForMessage(int size)
Removes messages from the buffer (oldest first) until there's enough space for the new message.

Parameters:
size - Size of the new message transferred, the transfer is aborted before message is removed
Returns:
True if enough space could be freed, false if not

dropExpiredMessages

protected void dropExpiredMessages()
Drops messages whose TTL is less than zero.


makeRoomForNewMessage

protected void makeRoomForNewMessage(int size)
Tries to make room for a new message. Current implementation simply calls makeRoomForMessage(int) and ignores the return value. Therefore, if the message can't fit into buffer, the buffer is only cleared from messages that are not being sent.

Parameters:
size - Size of the new message

getOldestMessage

protected Message getOldestMessage(boolean excludeMsgBeingSent)
Returns the oldest (by receive time) message in the message buffer (that is not being sent if excludeMsgBeingSent is true).

Parameters:
excludeMsgBeingSent - If true, excludes message(s) that are being sent from the oldest message check (i.e. if oldest message is being sent, the second oldest message is returned)
Returns:
The oldest message or null if no message could be returned (no messages in buffer or all messages in buffer are being sent and exludeMsgBeingSent is true)

getMessagesForConnected

protected java.util.List<Tuple<Message,Connection>> getMessagesForConnected()
Returns a list of message-connections tuples of the messages whose recipient is some host that we're connected to at the moment.

Returns:
a list of message-connections tuples

tryMessagesForConnected

protected Tuple<Message,Connection> tryMessagesForConnected(java.util.List<Tuple<Message,Connection>> tuples)
Tries to send messages for the connections that are mentioned in the Tuples in the order they are in the list until one of the connections starts transferring or all tuples have been tried.

Parameters:
tuples - The tuples to try
Returns:
The tuple whose connection accepted the message or null if none of the connections accepted the message that was meant for them.

tryAllMessages

protected Message tryAllMessages(Connection con,
                                 java.util.List<Message> messages)
Goes trough the messages until the other node accepts one for receiving (or doesn't accept any). If a transfer is started, the connection is included in the list of sending connections.

Parameters:
con - Connection trough which the messages are sent
messages - A list of messages to try
Returns:
The message whose transfer was started or null if no transfer was started.

tryMessagesToConnections

protected Connection tryMessagesToConnections(java.util.List<Message> messages,
                                              java.util.List<Connection> connections)
Tries to send all given messages to all given connections. Connections are first iterated in the order they are in the list and for every connection, the messages are tried in the order they are in the list. Once an accepting connection is found, no other connections or messages are tried.

Parameters:
messages - The list of Messages to try
connections - The list of Connections to try
Returns:
The connections that started a transfer or null if no connection accepted a message.

tryAllMessagesToAllConnections

protected Connection tryAllMessagesToAllConnections()
Tries to send all messages that this router is carrying to all connections this node has. Messages are ordered using the MessageRouter.sortByQueueMode(List). See tryMessagesToConnections(List, List) for sending details.

Returns:
The connections that started a transfer or null if no connection accepted a message.

exchangeDeliverableMessages

protected Connection exchangeDeliverableMessages()
Exchanges deliverable (to final recipient) messages between this host and all hosts this host is currently connected to. First all messages from this host are checked and then all other hosts are asked for messages to this host. If a transfer is started, the search ends.

Returns:
A connection that started a transfer or null if no transfer was started

shuffleMessages

protected void shuffleMessages(java.util.List<Message> messages)
Shuffles a messages list so the messages are in random order.

Parameters:
messages - The list to sort and shuffle

addToSendingConnections

protected void addToSendingConnections(Connection con)
Adds a connections to sending connections which are monitored in the update.

Parameters:
con - The connection to add
See Also:
update()

isTransferring

public boolean isTransferring()
Returns true if this router is transferring something at the moment or some transfer has not been finalized.

Returns:
true if this router is transferring something

isSending

public boolean isSending(java.lang.String msgId)
Returns true if this router is currently sending a message with msgId.

Parameters:
msgId - The ID of the message
Returns:
True if the message is being sent false if not

update

public void update()
Checks out all sending connections to finalize the ready ones and abort those whose connection went down. Also drops messages whose TTL <= 0 (checking every one simulated minute).

Overrides:
update in class MessageRouter
See Also:
addToSendingConnections(Connection)

transferAborted

protected void transferAborted(Connection con)
Method is called just before a transfer is aborted at update() due connection going down. This happens on the sending host. Subclasses that are interested of the event may want to override this.

Parameters:
con - The connection whose transfer was aborted

transferDone

protected void transferDone(Connection con)
Method is called just before a transfer is finalized at update(). Subclasses that are interested of the event may want to override this.

Parameters:
con - The connection whose transfer was finalized