ca.mcgill.cs.postina.scribe
Class ScribeApplication

java.lang.Object
  extended by ca.mcgill.cs.postina.scribe.ScribeApplication
All Implemented Interfaces:
rice.p2p.commonapi.Application, rice.p2p.scribe.ScribeClient, rice.p2p.scribe.ScribeMultiClient

public class ScribeApplication
extends java.lang.Object
implements rice.p2p.commonapi.Application, rice.p2p.scribe.ScribeMultiClient

The class ScribeApplication implements the client on the scribe-layer. This class is not intended to be used by a user of Postina. It is a mediator between Scribe and the ScribeNetworkLayer. Important reference for this class: The FreePastry Tutorial, http://www.freepastry.org/FreePastry/tutorial/

Version:
Oct 10, 2007
Author:
Dominik Zindel

Nested Class Summary
private  class ScribeApplication.messageHandler
          The inner class messageHandler is responsible for handling all incoming direct messages.
 
Field Summary
private  java.util.LinkedList<PostinaClientListener> clientListeners
          The ArrayList containing all listeners registered to the ScribeApplication for changes on clients.
private  int directMessageCounter
           
private  rice.p2p.commonapi.Endpoint endpoint
          The endpoint represents the underlying node.
private  java.util.LinkedList<rice.p2p.commonapi.Message> incomingMessages
          Incoming messages are put into this list so that they can be handled by the messagehandler.
private static org.apache.log4j.Logger logger
           
private  int messageCounter
          An integer counting the number of messages that have been sent by this client.
private  java.lang.Thread messageHandlerThread
          The thread handling the incoming message.s
private  java.util.ArrayList<PostinaMsgListener> msgListeners
          The ArrayList containing all message listeners registered to the ScribeApplication.
private  java.util.LinkedList<MessageID> outstandingAcks
          This list contains the unique identifiers of all messages that have been sent and that have not yet been confirmed.
private  java.util.LinkedList<rice.p2p.scribe.Topic> outstandingSubscriptions
          The list of subscriptions that have been issued but not yet confirmed.
private  int receivedMessageCounter
           
private  rice.p2p.scribe.ScribeImpl scribe
          The link to the default implementation of Scribe.
private  java.util.Hashtable<MessageID,PostinaReliableScribeTransportMessage> sentMessages
          A buffer keeping the messages sent by this client.
private  boolean threadIsAlive
          This boolean is only used to properly stop the thread handling the incoming direct messages.
 
Constructor Summary
ScribeApplication(rice.p2p.commonapi.Node node)
          The constructor for this scribe client.
 
Method Summary
 void addClientListener(PostinaClientListener listener)
          This method registers a listener to changes in the client structure.
 void addListener(PostinaMsgListener listener)
          Registers the given listener to this ScribeApplication
private  void addOutstanding(java.util.Collection<rice.p2p.scribe.Topic> topics)
          This method is the counterpart of removeOutstanding and adds the given subscriptions to the list of outstanding subscriptions.
 boolean anycast(rice.p2p.scribe.Topic topic, rice.p2p.scribe.ScribeContent content)
          Called when we receive an anycast.
 void childAdded(rice.p2p.scribe.Topic topic, rice.p2p.commonapi.NodeHandle child)
           
 void childRemoved(rice.p2p.scribe.Topic topic, rice.p2p.commonapi.NodeHandle child)
           
 void deliver(rice.p2p.commonapi.Id id, rice.p2p.commonapi.Message message)
          Part of the Application interface.
 void deliver(rice.p2p.scribe.Topic topic, rice.p2p.scribe.ScribeContent content)
          Called whenever a published message is received.
protected  void destroy()
          Destroy the ScribeApplication, i.e. remove it from the P2P-network.
protected  void directReliableSend(rice.p2p.commonapi.NodeHandle destination, PostinaReliableScribeTransportMessage msg)
          Sends a private message directly to the client indicated.
protected  void directSend(rice.p2p.commonapi.NodeHandle destination, rice.p2p.commonapi.Message msg)
          Send a private message directly to the client indicated.
 boolean forward(rice.p2p.commonapi.RouteMessage message)
           
protected  void informListeners(PostinaMessage message)
          This method informs registered listeners about a new message.
protected  void publish(ScribeTopic topic, PostinaScribeTransportMessage content)
          Publishes the given ScribeTransportMessage to the given topic.
 void removeClientListener(PostinaClientListener listener)
          This method unregisters a listener to changes in the client structure.
protected  void removeListener(PostinaMsgListener listener)
          Unregisters the given listener from this ScribeApplication
private  void removeOutstanding(java.util.Collection<rice.p2p.scribe.Topic> topics)
          This method is called when subscriptions are confirmed. It removes all confirmed subscriptions from the list of outstanding subscriptions.
protected  void sendRequest(rice.p2p.commonapi.NodeHandle destination, ScribeRequestMessage requestMessage)
          Sends a request to another client asking it to perform a specified action.
protected  void subscribe(java.util.Collection<rice.p2p.scribe.Topic> topics, PostinaScribeTransportMessage content)
          Subscribes this ScribeApplication to the Collection of Topics.
protected  void subscribe(ScribeTopic topic, PostinaScribeTransportMessage content)
          Subscribes this ScribeApplication to the given ScribeTopic.
 void subscribeFailed(java.util.Collection<rice.p2p.scribe.Topic> topics)
           
 void subscribeFailed(rice.p2p.scribe.Topic topic)
           
 void subscribeSuccess(java.util.Collection<rice.p2p.scribe.Topic> topics)
           
protected  void unsubscribe(java.util.Collection<rice.p2p.scribe.Topic> topics)
          Unsubscribes this ScribeApplication from the Collection of Topics.
protected  void unsubscribe(ScribeTopic topic)
          Unsubscribes this ScribeApplication from the given ScribeTopic.
 void update(rice.p2p.commonapi.NodeHandle handle, boolean joined)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static final org.apache.log4j.Logger logger

clientListeners

private final java.util.LinkedList<PostinaClientListener> clientListeners
The ArrayList containing all listeners registered to the ScribeApplication for changes on clients. Usually, there is only a single listener, namely the ScribeNetworkLayer.


directMessageCounter

private final int directMessageCounter
See Also:
Constant Field Values

endpoint

private final rice.p2p.commonapi.Endpoint endpoint
The endpoint represents the underlying node. By making calls on the endpoint, it assures that the message will be delivered to the application to whichever the message has to go.


incomingMessages

private final java.util.LinkedList<rice.p2p.commonapi.Message> incomingMessages
Incoming messages are put into this list so that they can be handled by the messagehandler.


messageCounter

private int messageCounter
An integer counting the number of messages that have been sent by this client. This is used to create the unique identifier of a message.


messageHandlerThread

private final java.lang.Thread messageHandlerThread
The thread handling the incoming message.s


msgListeners

private final java.util.ArrayList<PostinaMsgListener> msgListeners
The ArrayList containing all message listeners registered to the ScribeApplication. Usually, there is only a single listener, namely the ScribeNetworkLayer.


outstandingAcks

private final java.util.LinkedList<MessageID> outstandingAcks
This list contains the unique identifiers of all messages that have been sent and that have not yet been confirmed.


outstandingSubscriptions

private final java.util.LinkedList<rice.p2p.scribe.Topic> outstandingSubscriptions
The list of subscriptions that have been issued but not yet confirmed.


receivedMessageCounter

private final int receivedMessageCounter
See Also:
Constant Field Values

scribe

private final rice.p2p.scribe.ScribeImpl scribe
The link to the default implementation of Scribe.


sentMessages

private final java.util.Hashtable<MessageID,PostinaReliableScribeTransportMessage> sentMessages
A buffer keeping the messages sent by this client. It is used for reliability so that a message can be resent in case of a loss. This hashtable is periodically cleaned up, that is old messages are removed.


threadIsAlive

private boolean threadIsAlive
This boolean is only used to properly stop the thread handling the incoming direct messages.

Constructor Detail

ScribeApplication

public ScribeApplication(rice.p2p.commonapi.Node node)
The constructor for this scribe client. It will construct the ScribeApplication.

Parameters:
node - The PastryNode
Method Detail

addClientListener

public void addClientListener(PostinaClientListener listener)
This method registers a listener to changes in the client structure. Currently it is used only to inform about clients leaving the network.

Parameters:
listener - The listener registering.

addListener

public void addListener(PostinaMsgListener listener)
Registers the given listener to this ScribeApplication

Parameters:
listener - The PostinaMsgListener which has to be added to the list of listeners to this application.

addOutstanding

private void addOutstanding(java.util.Collection<rice.p2p.scribe.Topic> topics)
This method is the counterpart of removeOutstanding and adds the given subscriptions to the list of outstanding subscriptions.

Parameters:
topics - The topics to which the client has issued a subscription.

anycast

public boolean anycast(rice.p2p.scribe.Topic topic,
                       rice.p2p.scribe.ScribeContent content)
Called when we receive an anycast. If we return false, it will be delivered elsewhere. Returning true stops the message here. source: the FreePastry Tutorial

Specified by:
anycast in interface rice.p2p.scribe.ScribeClient
Specified by:
anycast in interface rice.p2p.scribe.ScribeMultiClient

childAdded

public void childAdded(rice.p2p.scribe.Topic topic,
                       rice.p2p.commonapi.NodeHandle child)
Specified by:
childAdded in interface rice.p2p.scribe.ScribeClient
Specified by:
childAdded in interface rice.p2p.scribe.ScribeMultiClient

childRemoved

public void childRemoved(rice.p2p.scribe.Topic topic,
                         rice.p2p.commonapi.NodeHandle child)
Specified by:
childRemoved in interface rice.p2p.scribe.ScribeClient
Specified by:
childRemoved in interface rice.p2p.scribe.ScribeMultiClient

deliver

public void deliver(rice.p2p.commonapi.Id id,
                    rice.p2p.commonapi.Message message)
Part of the Application interface. Called when a message is sent directly to a node.

Specified by:
deliver in interface rice.p2p.commonapi.Application

deliver

public void deliver(rice.p2p.scribe.Topic topic,
                    rice.p2p.scribe.ScribeContent content)
Called whenever a published message is received.

Specified by:
deliver in interface rice.p2p.scribe.ScribeClient
Specified by:
deliver in interface rice.p2p.scribe.ScribeMultiClient

destroy

protected void destroy()
Destroy the ScribeApplication, i.e. remove it from the P2P-network.


directReliableSend

protected void directReliableSend(rice.p2p.commonapi.NodeHandle destination,
                                  PostinaReliableScribeTransportMessage msg)
Sends a private message directly to the client indicated. Message will be delivered exactly once.

Parameters:
destination - The NodeHandle of the client which should receive the direct message.
msg - The PostinaReliableScribeTransportMessage that has to be sent to the client.

directSend

protected void directSend(rice.p2p.commonapi.NodeHandle destination,
                          rice.p2p.commonapi.Message msg)
Send a private message directly to the client indicated.

Parameters:
destination - The NodeHandle of the client which should receive the direct message.
msg - The Message that has to be sent to the client.

forward

public boolean forward(rice.p2p.commonapi.RouteMessage message)
Specified by:
forward in interface rice.p2p.commonapi.Application

informListeners

protected void informListeners(PostinaMessage message)
This method informs registered listeners about a new message.

Parameters:
message - The newly arrived message that is passed to the listeners.

publish

protected void publish(ScribeTopic topic,
                       PostinaScribeTransportMessage content)
Publishes the given ScribeTransportMessage to the given topic.

Parameters:
topic - The ScribeTopic to which the content should be published.
content - The PostinaScribeTransportMessage (containing the message) that should be delivered to all subscribed clients.

removeClientListener

public void removeClientListener(PostinaClientListener listener)
This method unregisters a listener to changes in the client structure.

Parameters:
listener - The listener unregistering.

removeListener

protected void removeListener(PostinaMsgListener listener)
Unregisters the given listener from this ScribeApplication

Parameters:
listener - The PostinaMsgListener which has to be removed from the list of listeners to this application.

removeOutstanding

private void removeOutstanding(java.util.Collection<rice.p2p.scribe.Topic> topics)
This method is called when subscriptions are confirmed. It removes all confirmed subscriptions from the list of outstanding subscriptions.

Parameters:
topics - The topics of the subscriptions that have been confirmed.

sendRequest

protected void sendRequest(rice.p2p.commonapi.NodeHandle destination,
                           ScribeRequestMessage requestMessage)
Sends a request to another client asking it to perform a specified action.

Parameters:
destination - The NodeHandle of the client that should perform the requested action.
requestMessage - The ScribeRequestMessage defining the action to be taken by the client specified as destination

subscribe

protected void subscribe(java.util.Collection<rice.p2p.scribe.Topic> topics,
                         PostinaScribeTransportMessage content)
Subscribes this ScribeApplication to the Collection of Topics.

Parameters:
topics - The Collection of Topics to which this ScribeApplication has to subscribe to.
content - The PostinaScribeTransportMessage is an optional message that can be routed together with the subscription.

subscribe

protected void subscribe(ScribeTopic topic,
                         PostinaScribeTransportMessage content)
Subscribes this ScribeApplication to the given ScribeTopic.

Parameters:
topic - The ScribeTopic to which the ScribeApplication has to subscribe to.
content - The PostinaScribeTransportMessage is an optional message that can be routed together with the subscription.

subscribeFailed

public void subscribeFailed(java.util.Collection<rice.p2p.scribe.Topic> topics)
Specified by:
subscribeFailed in interface rice.p2p.scribe.ScribeMultiClient

subscribeFailed

public void subscribeFailed(rice.p2p.scribe.Topic topic)
Specified by:
subscribeFailed in interface rice.p2p.scribe.ScribeClient
Specified by:
subscribeFailed in interface rice.p2p.scribe.ScribeMultiClient

subscribeSuccess

public void subscribeSuccess(java.util.Collection<rice.p2p.scribe.Topic> topics)
Specified by:
subscribeSuccess in interface rice.p2p.scribe.ScribeMultiClient

unsubscribe

protected void unsubscribe(java.util.Collection<rice.p2p.scribe.Topic> topics)
Unsubscribes this ScribeApplication from the Collection of Topics.

Parameters:
topics - The Collection of Topics from which this ScribeApplication has to unsubscribe..

unsubscribe

protected void unsubscribe(ScribeTopic topic)
Unsubscribes this ScribeApplication from the given ScribeTopic.

Parameters:
topic - The ScribeTopic from which this ScribeApplication has to unsubscribe.

update

public void update(rice.p2p.commonapi.NodeHandle handle,
                   boolean joined)
Specified by:
update in interface rice.p2p.commonapi.Application