planet.generic.commonapi
Class NodeImpl

java.lang.Object
  extended byplanet.generic.commonapi.NodeImpl
All Implemented Interfaces:
Node, java.io.Serializable
Direct Known Subclasses:
ChordNode, SymphonyNode, TrivialNode

public abstract class NodeImpl
extends java.lang.Object
implements Node, java.io.Serializable

Superclass which represents a node in a peer-to-peer system, regardless of the underlying protocol. All nodes, implement the methods of this class.

Author:
Pedro García, Carles Pairot, Ruben Mondejar
See Also:
Serialized Form

Field Summary
protected  java.util.Hashtable endpoints
          Local EndPoints.
protected  Id id
           
protected  java.util.Hashtable listeners
           
protected  NodeHandle nodeHandle
          NodeHandle for the actual Node.
protected  boolean role
           
 
Constructor Summary
NodeImpl()
          Initializes internal data structures.
 
Method Summary
 void addEdges(java.lang.String resultName, java.util.Iterator it, ResultsConstraint constraint, java.util.Collection edgeCollection, java.lang.String color)
          Adds new ResultsEdges to edgeCollection according to specified constraint.
 void addMessageListener(java.lang.String key, MessageListener listener)
          Adds a listener to the node so that it executes herself when the message response arrives
 RouteMessage buildMessage(RouteMessage toCopy)
          Builds a new RouteMessage with all the values appeared in toCopy, and the specified nextHop.
 RouteMessage buildMessage(java.lang.String key, NodeHandle from, NodeHandle to, NodeHandle nextHop, int type, int mode, java.lang.String appId, Message msg)
          A wrapper method, that sends a RouteMessage with the specified data.
 ResultsEdge buildNewEdge(java.lang.String resultName, Id left, Id right, java.lang.String color)
          Builds a ResultsEdge with the specified bounds and following the resultsName type format.
 void dispatchDataMessage(RouteMessage msg, int requestMode, int refreshMode)
          Make a generic treatment of the DATA messages (application layer messages).
abstract  NodeHandle getClosestNodeHandle(Id id)
          Returns the NodeHandle closest to id.
 Id getId()
          Returns the id of the node *
abstract  java.util.Hashtable getInfo()
          Returns information of the node
 NodeHandle getLocalHandle()
          Returns the local NodeHandle
 EndPoint getRegisteredApplication(java.lang.String instanceName)
          Get the registered application by the instanceName name.
 Application[] getRegisteredApplications()
          Returns all references of the applications that mantains this node.
protected  boolean hasMoreMessages()
          Checks if the incoming queue have a messages to send
 Queue inMessages()
           
protected  void invokeByStepToAllApplications()
          Invokes to each registered Application, by the related EndPoint, the byStep() method.
 boolean isLocalMessage(RouteMessage msg)
          The isLocalMessage's method is an extension method for commonapi specs.
abstract  void join(NodeHandle bootstrap)
          The node joins in the network
abstract  void leave()
          The node leaves the network
protected  RouteMessage nextMessage()
          Return the next message and dequeue this of the incoming queue
 Queue outMessages()
          Returns the present outgoing queue of this node
 boolean playsGoodRole()
          The playsGoodRole's method is an extension method for commonapi specs.
 boolean process(int actualStep)
          Given a time fraction, the node it can do everything what needs during this
 void receive(RouteMessage msg)
          Puts a message in the incoming queue of this node
 EndPoint registerApplication(Application app, java.lang.String instance)
          This returns a VirtualizedNode specific to the given application and instance name to the application, which the application can then use in order to send an receive messages.
 void removeMessageListener(java.lang.String key)
          Remove the message listener of the node
 void send(RouteMessage msg)
          Puts a message in the outcoming queue of this node
 boolean sendMessage(RouteMessage msg)
          Puts the RouteMessage msg to the outgoing queue of this node.
 void sendMessage(RouteMessage rMsg, java.lang.String key, NodeHandle from, NodeHandle to, NodeHandle nextHop, int type, int mode, Message msg)
          A wrapper method, that send a RouteMessage with the specified data.
 boolean sendMessage(java.lang.String key, NodeHandle from, NodeHandle to, int type, int mode, Message msg)
          A wrapper method, that sends a RouteMessage with the specified data.
 boolean sendMessage(java.lang.String key, NodeHandle from, NodeHandle to, NodeHandle nextHop, int type, int mode, Message msg)
          A wrapper method, that sends a RouteMessage with the specified data.
 void setGoodRole(boolean role)
          The setGoodRole's method is an extension method for commonapi specs.
 void setTimer(TimerTask task, long firstTime)
          Sets a task to be executed only one time at specified firstTime.
 void setTimer(TimerTask task, long firstTime, long period)
          Sets a task to be executed periodicly at each period of time.
 Node setValues(Id newId)
          Sets the new Id.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface planet.commonapi.Node
broadcast, buildEdges, fail, getAllLinks, getPred, getSucc, getSuccList, isAlive, localLookup, neighborSet, prettyPrintNode, printNode, range, replicaSet, routeData
 

Field Detail

id

protected Id id

listeners

protected transient java.util.Hashtable listeners

nodeHandle

protected NodeHandle nodeHandle
NodeHandle for the actual Node.


endpoints

protected java.util.Hashtable endpoints
Local EndPoints.


role

protected boolean role
Constructor Detail

NodeImpl

public NodeImpl()
Initializes internal data structures.

Method Detail

join

public abstract void join(NodeHandle bootstrap)
The node joins in the network

Specified by:
join in interface Node
Parameters:
bootstrap - Id of a node in the network

leave

public abstract void leave()
The node leaves the network

Specified by:
leave in interface Node

process

public boolean process(int actualStep)
Given a time fraction, the node it can do everything what needs during this

Specified by:
process in interface Node
Parameters:
actualStep - Actual step in simulation time.
Returns:
Always true.

invokeByStepToAllApplications

protected void invokeByStepToAllApplications()
Invokes to each registered Application, by the related EndPoint, the byStep() method. This inform to each application for a new step. This method must to be invoked at the end of each process(int) node implementation.


receive

public void receive(RouteMessage msg)
             throws QueueFull
Puts a message in the incoming queue of this node

Specified by:
receive in interface Node
Parameters:
msg - received Message
Throws:
QueueFull - if the incoming queue of the actual Node is full.

buildMessage

public RouteMessage buildMessage(java.lang.String key,
                                 NodeHandle from,
                                 NodeHandle to,
                                 NodeHandle nextHop,
                                 int type,
                                 int mode,
                                 java.lang.String appId,
                                 Message msg)
A wrapper method, that sends a RouteMessage with the specified data. If any exception has ocurred during the send, a log with the description is made.

Parameters:
key - Key of the communication.
from - Communication source
to - Communication destination
nextHop - Next hop in the communication.
type - RouteMessage type
mode - RouteMessage mode
appId - Name of the related application.
msg - Data to be sent with the RouteMessage
Returns:
A valid RouteMessage with the specified data or null, if any error has ocurred.

buildMessage

public RouteMessage buildMessage(RouteMessage toCopy)
Builds a new RouteMessage with all the values appeared in toCopy, and the specified nextHop.

Parameters:
toCopy - Message to be cloned.
Returns:
A valid RouteMessage or null if there are any error.

send

public void send(RouteMessage msg)
          throws QueueFull
Puts a message in the outcoming queue of this node

Specified by:
send in interface Node
Parameters:
msg - sended Message
Throws:
QueueFull - if the outgoing queue of the actual Node is full.

sendMessage

public boolean sendMessage(RouteMessage msg)
Puts the RouteMessage msg to the outgoing queue of this node.

Parameters:
msg - RouteMessage to be sent.

sendMessage

public boolean sendMessage(java.lang.String key,
                           NodeHandle from,
                           NodeHandle to,
                           int type,
                           int mode,
                           Message msg)
A wrapper method, that sends a RouteMessage with the specified data. If any exception has ocurred during the send, a log with the description is made.

Parameters:
key - Key of the communication.
from - Communication source
to - Communication destination
type - RouteMessage type
mode - RouteMessage mode
msg - Data to be sent with the RouteMessage

sendMessage

public boolean sendMessage(java.lang.String key,
                           NodeHandle from,
                           NodeHandle to,
                           NodeHandle nextHop,
                           int type,
                           int mode,
                           Message msg)
A wrapper method, that sends a RouteMessage with the specified data. If any exception has ocurred during the send, a log with the description is made.

Parameters:
key - Key of the communication.
from - Communication source
to - Communication destination
nextHop - Next hop in the communication.
type - RouteMessage type
mode - RouteMessage mode
msg - Data to be sent with the RouteMessage

sendMessage

public void sendMessage(RouteMessage rMsg,
                        java.lang.String key,
                        NodeHandle from,
                        NodeHandle to,
                        NodeHandle nextHop,
                        int type,
                        int mode,
                        Message msg)
A wrapper method, that send a RouteMessage with the specified data. If any error has ocurred during the send, a log with the description is made.

Parameters:
rMsg - RouteMessage to be used.
key - Communication key
from - Communication source.
to - Communication destination.
type - RouteMessage type
mode - RouteMessage mode
msg - Data to be sent with the RouteMessage

outMessages

public Queue outMessages()
Returns the present outgoing queue of this node

Specified by:
outMessages in interface Node
Returns:
outgoing Queue of Messages

inMessages

public Queue inMessages()

hasMoreMessages

protected boolean hasMoreMessages()
Checks if the incoming queue have a messages to send

Returns:
return true if has incoming messages to process

nextMessage

protected RouteMessage nextMessage()
Return the next message and dequeue this of the incoming queue

Returns:
return the next Message

getId

public Id getId()
Returns the id of the node *

Specified by:
getId in interface Node
Returns:
Id node identificator

addMessageListener

public void addMessageListener(java.lang.String key,
                               MessageListener listener)
Adds a listener to the node so that it executes herself when the message response arrives

Parameters:
key - String representation of id routing message
listener - MessageListener linked to Message

removeMessageListener

public void removeMessageListener(java.lang.String key)
Remove the message listener of the node

Parameters:
key - String representation of id routing message

getInfo

public abstract java.util.Hashtable getInfo()
Returns information of the node

Returns:
info Hashtable with the information

getLocalHandle

public NodeHandle getLocalHandle()
Returns the local NodeHandle

Specified by:
getLocalHandle in interface Node
Returns:
The actual local NodeHandle
See Also:
Node.getLocalHandle()

setTimer

public void setTimer(TimerTask task,
                     long firstTime,
                     long period)
Sets a task to be executed periodicly at each period of time.

Specified by:
setTimer in interface Node
Parameters:
task - Job to do at each activation of the task.
firstTime - First activation of the task, measured in steps or millis. Its value is the relative time, not in absolute time.
period - Number of steps or millis to periodicly execute the task.
See Also:
Node.setTimer(planet.util.timer.TimerTask, long, long)

setTimer

public void setTimer(TimerTask task,
                     long firstTime)
Sets a task to be executed only one time at specified firstTime.

Specified by:
setTimer in interface Node
Parameters:
task - Job to do at activation.
firstTime - Moment to be activated, in steps or millis, measured in relative time, not in absolute time.
See Also:
Node.setTimer(planet.util.timer.TimerTask, long)

registerApplication

public EndPoint registerApplication(Application app,
                                    java.lang.String instance)
This returns a VirtualizedNode specific to the given application and instance name to the application, which the application can then use in order to send an receive messages.

Specified by:
registerApplication in interface Node
Parameters:
app - The Application
instance - An identifier for a given instance
Returns:
The endpoint specific to this application, which can be used for message sending/receiving. Return null if cannot build the required EndPoint.

getRegisteredApplication

public EndPoint getRegisteredApplication(java.lang.String instanceName)
Get the registered application by the instanceName name.

Specified by:
getRegisteredApplication in interface Node
Parameters:
instanceName - Name of the registered application.
Returns:
null if there isn't an instance of instanceName, or the related endpoint for the application.

getRegisteredApplications

public Application[] getRegisteredApplications()
Returns all references of the applications that mantains this node.

Specified by:
getRegisteredApplications in interface Node
Returns:
An array with all references to the applications that they are mantained for this node.

playsGoodRole

public boolean playsGoodRole()
The playsGoodRole's method is an extension method for commonapi specs. This method is used to allow BehavioursPool decide wether the Node should run behaviours for good peers or instead run behaviours for bad peers. Moreover, this method lets the programmer the responsability of decide node's role. Even it allows a node to have a transient behaviour, sometimes behaving as a good peer and sometines behaving as a bad peer.

Specified by:
playsGoodRole in interface Node
Returns:
True if the Node's Role is positive.

setGoodRole

public void setGoodRole(boolean role)
The setGoodRole's method is an extension method for commonapi specs. This method is used to set the role of the node inside the overlay network. A true value means the node will behave according to the underlying overlay protocol. A false value means the node will misbehave.

Specified by:
setGoodRole in interface Node
Parameters:
role -

isLocalMessage

public boolean isLocalMessage(RouteMessage msg)
The isLocalMessage's method is an extension method for commonapi specs. This method is used to allow BehavioursPool decide wether the incoming RouteMessage is for the local node or for a remote node. Remenber, this decision may only be addressed by the underlying overlay protocol. For example, for Symphony's Lookup protocol a node is responsible for all RouteMessages whose keys have as an immediate succesor the node's id or have as destination the own node.

Specified by:
isLocalMessage in interface Node
Returns:
True if the incoming RouteMessage taken as input its for the local node.
See Also:
BehavioursPool

addEdges

public void addEdges(java.lang.String resultName,
                     java.util.Iterator it,
                     ResultsConstraint constraint,
                     java.util.Collection edgeCollection,
                     java.lang.String color)
Adds new ResultsEdges to edgeCollection according to specified constraint.

Parameters:
resultName - Result type name to use.
it - Iterator over a collection of nodeHandles.
constraint - Specific constraints to test all built edges.
edgeCollection - Edges collection where they have to be added.
color - Fill color of the edge.

buildNewEdge

public ResultsEdge buildNewEdge(java.lang.String resultName,
                                Id left,
                                Id right,
                                java.lang.String color)
Builds a ResultsEdge with the specified bounds and following the resultsName type format.

Parameters:
resultName - Result type name to use.
left - One bound of the link.
right - Second bound of the link.
color - Edge color.
Returns:
A new instance of the ResultsEdge according the specified resultName or null if any error has ocurred.

getClosestNodeHandle

public abstract NodeHandle getClosestNodeHandle(Id id)
Returns the NodeHandle closest to id.

Specified by:
getClosestNodeHandle in interface Node
Parameters:
id - Id to find.
Returns:
The NodeHandle closest to id.

dispatchDataMessage

public void dispatchDataMessage(RouteMessage msg,
                                int requestMode,
                                int refreshMode)
Make a generic treatment of the DATA messages (application layer messages).

Parameters:
msg - RouteMessage to be treated.
requestMode - RouteMessage REQUEST mode for the actual Overlay implementation.
refreshMode - RouteMessage REFRESH mode for the actual Overlay implementation.

setValues

public Node setValues(Id newId)
               throws InitializationException
Sets the new Id.

Specified by:
setValues in interface Node
Parameters:
newId - New Id.
Returns:
The same instance after has been updated.
Throws:
InitializationException
See Also:
Node.setValues(planet.commonapi.Id)