ca.mcgill.cs.postina.scribe
Class ScribeApplication.messageHandler

java.lang.Object
  extended by ca.mcgill.cs.postina.scribe.ScribeApplication.messageHandler
All Implemented Interfaces:
java.lang.Runnable
Enclosing class:
ScribeApplication

private class ScribeApplication.messageHandler
extends java.lang.Object
implements java.lang.Runnable

The inner class messageHandler is responsible for handling all incoming direct messages. Its main task is to provide reliability.

Version:
9-Jan-08
Author:
Dominik Zindel

Field Summary
private  int defaultTimeout
          The number of milliseconds before a message times out and is resent.
private  java.util.Hashtable<rice.p2p.commonapi.Id,java.lang.Integer> doomedClients
          Clients who have not acknowledged a message after a certain number of attempts are suspected to be dead and put into this hashtable.
private  int fatalNumLostMsg
          The number of messages that can be lost before a client is definitely declared dead.
private  java.util.HashSet<rice.p2p.commonapi.Id> killedClients
          Clients who have been suspected to be dead for a certain number of times are declared dead and subsequently ignored.
private  int maxNumTries
          This integer variable indicates the number of attempts that are made to send a message before the client is suspected to be dead.
private  LinkedHashSet<MessageID> receivedMessagesIDs
          The list receivedMessagesIDs contains the IDs of received messages to guarantee that each message is delivered only once.
private  long sleep
          This long specifies how long the thread will wait after each round in the loop.
 
Constructor Summary
ScribeApplication.messageHandler()
          The unique constructor creates all necessary lists and sets the variables to the default values defined in the property file postina.properties.
 
Method Summary
 void cancelMessage(MessageID msgId)
          A message that has timed out a certain number of times (specified in the properties) will not be resent anymore.
 long checkAcks()
          This method checks if any message has timed out since the last check and takes the appropriate action.
 boolean clientIsAlive(rice.p2p.commonapi.Id client)
          This method indicates if a given client has been declared dead in the past.
 void doomClient(rice.p2p.commonapi.NodeHandle nh, rice.p2p.commonapi.Id client)
          This method does the necessary handling when a client is suspected dead.
 void handleAck(AckMessage ack)
          This method handles an incoming acknowledgment.
 void handleMessage(rice.p2p.commonapi.Message message)
          This is the main entry method of the message handler.
 void handleReliable(PostinaReliableScribeTransportMessage msg)
          This method actually handles a newly arrived message (sending acknowledgment etc).
 void killClient(rice.p2p.commonapi.NodeHandle handle, rice.p2p.commonapi.Id client)
          A client that has not acknowledged a certain number of messages and not sent any new messages is considered dead and ignored in the future.
 void pardonClient(rice.p2p.commonapi.Id client)
          If the same message is lost several times, a client is suspected dead and finally, after a specified number of times, declared dead. This method resets the counter to 0 which is necessary when a message from this client arrives (confirming that it is still alive).
 void removeOldMessages(long storageTime)
          The buffer containing received messages has to be cleaned from time to time. This is done by this method which removes old messages from the buffer.
 void resendMessage(MessageID msgId)
          This method resends a message.
 void run()
           
 void sendAck(PostinaReliableScribeTransportMessage msg)
          This message confirms the arrival of message by sending back an acknowledgment to the originator of the message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

defaultTimeout

private final int defaultTimeout
The number of milliseconds before a message times out and is resent.


doomedClients

private final java.util.Hashtable<rice.p2p.commonapi.Id,java.lang.Integer> doomedClients
Clients who have not acknowledged a message after a certain number of attempts are suspected to be dead and put into this hashtable. The key is the Id of the client, the value is an integer indicating the number of times the client has been suspected dead.


fatalNumLostMsg

private final int fatalNumLostMsg
The number of messages that can be lost before a client is definitely declared dead.


killedClients

private final java.util.HashSet<rice.p2p.commonapi.Id> killedClients
Clients who have been suspected to be dead for a certain number of times are declared dead and subsequently ignored.


maxNumTries

private final int maxNumTries
This integer variable indicates the number of attempts that are made to send a message before the client is suspected to be dead.


receivedMessagesIDs

private final LinkedHashSet<MessageID> receivedMessagesIDs
The list receivedMessagesIDs contains the IDs of received messages to guarantee that each message is delivered only once.


sleep

private long sleep
This long specifies how long the thread will wait after each round in the loop. The value is computed depending on the next timeout.

Constructor Detail

ScribeApplication.messageHandler

public ScribeApplication.messageHandler()
The unique constructor creates all necessary lists and sets the variables to the default values defined in the property file postina.properties.

Method Detail

cancelMessage

public void cancelMessage(MessageID msgId)
A message that has timed out a certain number of times (specified in the properties) will not be resent anymore. This cancellation is done by this method.

Parameters:
msgId - The unique identifier of the message that has to be cancelled because it has timed out too often.

checkAcks

public long checkAcks()
This method checks if any message has timed out since the last check and takes the appropriate action.

Returns:
The method returns a long indicating the number of milliseconds after which the next message will time out. The calling thread uses this value to determine for how much time it has to sleep/wait.

clientIsAlive

public boolean clientIsAlive(rice.p2p.commonapi.Id client)
This method indicates if a given client has been declared dead in the past.

Parameters:
client - The client of which the status has to be known.
Returns:
Returns false if the client has been declared dead, true true otherwise.

doomClient

public void doomClient(rice.p2p.commonapi.NodeHandle nh,
                       rice.p2p.commonapi.Id client)
This method does the necessary handling when a client is suspected dead. It increases the counter of number of times the client has been suspected dead and declares the client dead if it has been suspected dead a several number of times.

Parameters:
nh - The nodehandle of the client.
client - The unique identifier of the client that is suspected dead.

handleAck

public void handleAck(AckMessage ack)
This method handles an incoming acknowledgment.

Parameters:
ack - The acknowledgment to handle.

handleMessage

public void handleMessage(rice.p2p.commonapi.Message message)
This is the main entry method of the message handler. Each arriving message is checked and the appropriate handler of the message is then called.

Parameters:
message -

handleReliable

public void handleReliable(PostinaReliableScribeTransportMessage msg)
This method actually handles a newly arrived message (sending acknowledgment etc).

Parameters:
msg - The new direct message that has to be handled.

killClient

public void killClient(rice.p2p.commonapi.NodeHandle handle,
                       rice.p2p.commonapi.Id client)
A client that has not acknowledged a certain number of messages and not sent any new messages is considered dead and ignored in the future.

Parameters:
handle - The nodehandle of the client being declared dead. This is used to inform listeners.
client - The Id of the client being declared dead.

pardonClient

public void pardonClient(rice.p2p.commonapi.Id client)
If the same message is lost several times, a client is suspected dead and finally, after a specified number of times, declared dead. This method resets the counter to 0 which is necessary when a message from this client arrives (confirming that it is still alive).

Parameters:
client - The client which has to be pardoned, i.e. which has confirmed that it is still alive.

removeOldMessages

public void removeOldMessages(long storageTime)
The buffer containing received messages has to be cleaned from time to time. This is done by this method which removes old messages from the buffer.

Parameters:
storageTime - This long indicates the time in milliseconds that messages are kept in the buffer.

resendMessage

public void resendMessage(MessageID msgId)
This method resends a message. This is necessary when the message has timed out, i.e. no acknowledgment arrived.

Parameters:
msgId - The unique identifier of the message that has to be resent.

run

public void run()
Specified by:
run in interface java.lang.Runnable

sendAck

public void sendAck(PostinaReliableScribeTransportMessage msg)
This message confirms the arrival of message by sending back an acknowledgment to the originator of the message.

Parameters:
msg - The message of which the arrival has to be confirmed.