cs138.tapestry
Class TapestryNodeImpl

java.lang.Object
  extended by cs138.tapestry.TapestryNodeImpl
All Implemented Interfaces:
ManageableNode, TapestryNode, TapestryNodePrivate, java.rmi.Remote

public class TapestryNodeImpl
extends java.lang.Object
implements TapestryNodePrivate

The actual implementation of the TapestryNode. You will write most of your code here.


Constructor Summary
TapestryNodeImpl()
           
 
Method Summary
 boolean equals(TapestryNode x)
          Returns true if the hashes for both nodes are equal.
 java.lang.Object findObject(java.lang.String key)
          Route towards the surrogate nodes for the key's salted hash values and attempt to get the closest location for the object from the first surrogate node that is successfully contacted.
 TapestryEnvironment getEnvironment()
          Returns the environment which this node was created in.
 java.lang.String getIdentifier()
          Returns the identifying string for this node.
 boolean getLocation(java.lang.String key, java.math.BigInteger hash, cs138.tapestry.TapestryResponseInterface response)
          Called by other nodes that would like to know where it can find replicas for the object associated with the given key.
 java.lang.Class<? extends NodeMonitor> getMonitorInterface()
          This returns the Node implementation specific monitor that will be used.
 TapestryNeighborMap getNeighborMap()
          Returns the remote stub for this node's neighbor map.
 java.math.BigInteger getNodeHash()
          Returns the hash value for this node.
 int getNumObjects()
          Returns the number of objects stored at this node.
 java.lang.Object getObject(java.lang.String key)
          Return a copy of the object identified by the specified key.
 java.util.List<java.lang.String> getObjectList()
          Returns a list of the keys stored at this replica.
 java.util.LinkedList<TapestryObjectLocation> getObjectLocations()
          Returns a list of the object to location mappings stored at this node.
 void hello(TapestryNodePrivate node)
          This method is called by another node when it has recently entered the network.
 void initialize(java.lang.String id, ManageableNode node, NodeMonitor monitor)
          Initializes a new node
 boolean notifySurrogate(TapestryNodePrivate node)
          This node is being informed by the caller that the node specified by the "node" variable has just started and might be the new surrogate for some of the mappings this node is holding.
 void ping()
          This method is used in nodes to determine that they are still up.
 boolean publishLocation(java.lang.String key, java.math.BigInteger hash, TapestryNodeCache cache)
          Called by other nodes in an attempt to publish the location of an object (with a specific salted hash value) at this node.
 void publishObject(java.lang.String key, java.lang.Object data)
          You should store a copy of the object at this node and then attempt to contact the object's surrogate to publish this node as a location for the object.
 void removeLocation(java.lang.String key, TapestryNodeCache cache)
          Called by other nodes when they would like to remove a replica from the object location information for the object specified by the given key.
 void stop()
          Performs a hard shutdown at this node.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TapestryNodeImpl

public TapestryNodeImpl()
                 throws java.rmi.RemoteException
Throws:
java.rmi.RemoteException
Method Detail

initialize

public void initialize(java.lang.String id,
                       ManageableNode node,
                       NodeMonitor monitor)
                throws java.rmi.RemoteException
Description copied from interface: ManageableNode
Initializes a new node

Specified by:
initialize in interface ManageableNode
Parameters:
id - The identifier of the node
node - A gateway node, may be null
monitor - The default monitor to attach
Throws:
java.rmi.RemoteException

stop

public void stop()
          throws java.rmi.RemoteException
Performs a hard shutdown at this node. Any in-progress remote calls to the object will fail with a RemoteException.

Specified by:
stop in interface TapestryNode
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.stop()

notifySurrogate

public boolean notifySurrogate(TapestryNodePrivate node)
                        throws java.rmi.RemoteException
This node is being informed by the caller that the node specified by the "node" variable has just started and might be the new surrogate for some of the mappings this node is holding. This method should identify mappings that should go to "node", transfer those mappings, and then add "node" to the neighbor map. Finally, remove the transferred mappings. Returns false if this node is not the surrogate for the hash of the given node.

Specified by:
notifySurrogate in interface TapestryNodePrivate
Parameters:
node - Another Tapestry node.
Returns:
True if the local node is the surrogate for the hash of the specified node and the operation was successful.
Throws:
java.rmi.RemoteException
See Also:
cs138.tapestry.TapestryNodeFactory.TapestryNodePrivate#notifySurrogate(cs138.tapestry.TapestryNodeFactory.TapestryNodePrivate)

hello

public void hello(TapestryNodePrivate node)
           throws java.rmi.RemoteException
This method is called by another node when it has recently entered the network. You should add node to the neighbor map if appropriate.

Specified by:
hello in interface TapestryNodePrivate
Parameters:
node - Another Tapestry node.
Throws:
java.rmi.RemoteException
See Also:
cs138.tapestry.TapestryNodeFactory.TapestryNodePrivate#hello(cs138.tapestry.TapestryNodeFactory.TapestryNodePrivate)

findObject

public java.lang.Object findObject(java.lang.String key)
                            throws java.rmi.RemoteException
Route towards the surrogate nodes for the key's salted hash values and attempt to get the closest location for the object from the first surrogate node that is successfully contacted. You should make a best effort to deal with failed nodes - if you encounter one, remove it from the neighbor map you got it from and try again from the last node you successfully contacted. If the surrogate node is no longer the surrogate node for the object's hash when you call getLocation (it should return false), start again from beginning. You may try as many times as specified by the environment's maxFindAttempts() method.

Specified by:
findObject in interface TapestryNode
Parameters:
key - The object's key.
Returns:
The object's data.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.findObject(java.lang.String)

publishLocation

public boolean publishLocation(java.lang.String key,
                               java.math.BigInteger hash,
                               TapestryNodeCache cache)
                        throws java.rmi.RemoteException
Called by other nodes in an attempt to publish the location of an object (with a specific salted hash value) at this node. You should return false if this node is not the surrogate for the hash value. Otherwise, create a TapestryObjectLocation object for the specified key (if necessary) and add the specified node as a replica for the object. Return true on success.

Specified by:
publishLocation in interface TapestryNodePrivate
Parameters:
key - The object's key.
hash - The salted hash of the key.
cache - The cache of the node at which a replica of the object is stored.
Returns:
True is the local node is the surrogate node for the salted hash and the operation is successful.
Throws:
java.rmi.RemoteException
See Also:
cs138.tapestry.TapestryNodePrivate#publishLocation(java.lang.String, java.math.BigInteger, cs138.tapestry.TapestryNodePrivate)

getLocation

public boolean getLocation(java.lang.String key,
                           java.math.BigInteger hash,
                           cs138.tapestry.TapestryResponseInterface response)
                    throws java.rmi.RemoteException
Called by other nodes that would like to know where it can find replicas for the object associated with the given key. You should return false if this node is not the surrogate node for hash value. Otherwise, set the response to be the TapestryObjectLocation for the given key and return true. The response variable is used because there is no clean way to specify both a boolean value and an object together as the return value for a method.

Specified by:
getLocation in interface TapestryNodePrivate
Parameters:
key - The object's key.
hash - The key's salted hash.
response - The cache to store the object location information in.
Returns:
True if the local node is the surrogate for the salted hash.
Throws:
java.rmi.RemoteException
See Also:
cs138.tapestry.TapestryNodePrivate#getLocation(java.lang.String, java.math.BigInteger, cs138.tapestry.TapestryEnvironment.TapestryResponseCache)

removeLocation

public void removeLocation(java.lang.String key,
                           TapestryNodeCache cache)
                    throws java.rmi.RemoteException
Called by other nodes when they would like to remove a replica from the object location information for the object specified by the given key. You should remove the specified node from the replicas of the TapestryObjectLocation store for the specified key.

Specified by:
removeLocation in interface TapestryNodePrivate
Parameters:
key - The object's key.
cache - The failed replica of the object.
Throws:
java.rmi.RemoteException
See Also:
TapestryNodePrivate.removeLocation(java.lang.String, cs138.tapestry.TapestryNodeCache)

publishObject

public void publishObject(java.lang.String key,
                          java.lang.Object data)
                   throws java.rmi.RemoteException
You should store a copy of the object at this node and then attempt to contact the object's surrogate to publish this node as a location for the object. Returns immediately after one attempt to publish this object's location at the surrogate (just call republish once then return).

Specified by:
publishObject in interface TapestryNode
Parameters:
key - The key for the object.
data - The object's data.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.publishObject(java.lang.String, java.lang.Object)

getObject

public java.lang.Object getObject(java.lang.String key)
                           throws java.rmi.RemoteException
Return a copy of the object identified by the specified key. It is assumed that either this node holds a copy of the object or the object does not exist.

Specified by:
getObject in interface TapestryNodePrivate
Parameters:
key - The key for the object
Returns:
The value associated with the object.
Throws:
java.rmi.RemoteException
See Also:
cs138.tapestry.TapestryNode#getObject(java.lang.String)

getObjectList

public java.util.List<java.lang.String> getObjectList()
                                               throws java.rmi.RemoteException
Returns a list of the keys stored at this replica.

Specified by:
getObjectList in interface TapestryNode
Returns:
A list of the keys for the objects stored at the local node.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getObjectList()

getIdentifier

public java.lang.String getIdentifier()
                               throws java.rmi.RemoteException
Returns the identifying string for this node.

Specified by:
getIdentifier in interface TapestryNode
Returns:
The node's identifier.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getIdentifier()

getNodeHash

public java.math.BigInteger getNodeHash()
                                 throws java.rmi.RemoteException
Returns the hash value for this node.

Specified by:
getNodeHash in interface TapestryNode
Returns:
The node's hash.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getNodeHash()

getNumObjects

public int getNumObjects()
                  throws java.rmi.RemoteException
Returns the number of objects stored at this node.

Specified by:
getNumObjects in interface TapestryNode
Returns:
The number of objects stored at this node.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getNumObjects()

getNeighborMap

public TapestryNeighborMap getNeighborMap()
                                   throws java.rmi.RemoteException
Returns the remote stub for this node's neighbor map.

Specified by:
getNeighborMap in interface TapestryNode
Returns:
The node's neighbor map (or routing table).
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getNeighborMap()

getEnvironment

public TapestryEnvironment getEnvironment()
                                   throws java.rmi.RemoteException
Returns the environment which this node was created in.

Specified by:
getEnvironment in interface TapestryNode
Returns:
The environment this node was created with.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getEnvironment()

equals

public boolean equals(TapestryNode x)
               throws java.rmi.RemoteException
Returns true if the hashes for both nodes are equal.

Specified by:
equals in interface TapestryNode
Parameters:
x - Another Tapestry node.
Returns:
True if the given node is the same as this node.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.equals(cs138.tapestry.TapestryNode)

getObjectLocations

public java.util.LinkedList<TapestryObjectLocation> getObjectLocations()
                                                                throws java.rmi.RemoteException
Returns a list of the object to location mappings stored at this node.

Specified by:
getObjectLocations in interface TapestryNode
Returns:
A list of object locations stored by this node.
Throws:
java.rmi.RemoteException
See Also:
TapestryNode.getObjectLocations()

ping

public void ping()
          throws java.rmi.RemoteException
Description copied from interface: ManageableNode
This method is used in nodes to determine that they are still up.

Specified by:
ping in interface ManageableNode
Throws:
java.rmi.RemoteException

getMonitorInterface

public java.lang.Class<? extends NodeMonitor> getMonitorInterface()
                                                           throws java.rmi.RemoteException
Description copied from interface: ManageableNode
This returns the Node implementation specific monitor that will be used.

Specified by:
getMonitorInterface in interface ManageableNode
Returns:
The class object corresponding to the Monitor Interface being used.
Throws:
java.rmi.RemoteException