cs138.chord
Class ChordNodeImpl

java.lang.Object
  extended by cs138.chord.ChordNodeImpl
All Implemented Interfaces:
ChordNode, RemoteChordNode, ManageableNode, java.rmi.Remote

public class ChordNodeImpl
extends java.lang.Object
implements RemoteChordNode

This class is our main implementation of a ChordNode

Author:
rm

Constructor Summary
ChordNodeImpl()
          Creates a new Chord node with the given identifier.
 
Method Summary
 RemoteChordNode findSuccessor(java.math.BigInteger hash)
          Find the Chord node responsible for the given key.
 java.io.Serializable get(java.lang.String key)
          The publicly available "get" method.
 ChordNode[] getFingerTable()
           
 java.lang.String getIdentifier()
           
 java.util.List<java.lang.String> getKeys()
           
 java.io.Serializable getLocal(java.lang.String key)
          Get the data associated with the specified key.
 java.lang.Class<? extends NodeMonitor> getMonitorInterface()
          This returns the Node implementation specific monitor that will be used.
 java.math.BigInteger getNodeHash()
           
 int getNumKeys()
           
 ChordNode getPredecessor()
           
 ChordNode getSuccessor()
           
 void initialize(java.lang.String id, ManageableNode node, NodeMonitor monitor)
          Initializes a new node
 boolean isReady()
          Determines that the node in question has been set up and is ready to be used.
 void leave()
          Called when the node wants to leave the Chord ring.
 ChordNode locate(java.lang.String key)
          Find the Chord node responsible for the given key.
 void notify(RemoteChordNode node)
          The caller of this method believes that "node" should be this node's predecessor.
 void ping()
          This method is used in nodes to determine that they are still up.
 boolean put(java.lang.String key, java.io.Serializable data)
          The publicly available "put" method.
 boolean putLocal(java.lang.String key, java.io.Serializable data)
          Insert a key-value pair into the hash table.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ChordNodeImpl

public ChordNodeImpl()
              throws java.rmi.RemoteException
Creates a new Chord node with the given identifier. The node is not used until it's managed node is initialized.

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

findSuccessor

public RemoteChordNode findSuccessor(java.math.BigInteger hash)
                              throws java.rmi.RemoteException
Find the Chord node responsible for the given key. If the key resides between this node and its successor, return the successor. Otherwise, find the closest preceding node in your finger table and ask it to locate the successor of the key. Don't forget to protect your access to the finger table.

Specified by:
findSuccessor in interface RemoteChordNode
Parameters:
hash - The hash of the key you are looking for
Returns:
The node that is responsible for the hashed key.
Throws:
java.rmi.RemoteException

notify

public void notify(RemoteChordNode node)
            throws java.rmi.RemoteException
The caller of this method believes that "node" should be this node's predecessor. We should verify this claim, and if it is true we should update our predecessor. Remember the new predecessor might be responsible for some of the keys we are currently holding. Make sure that the predecessor cannot be changed by another thread while we are verifying or transferring keys.

Specified by:
notify in interface RemoteChordNode
Parameters:
node - The node that believes it is the new predecessor
Throws:
java.rmi.RemoteException

leave

public void leave()
           throws java.rmi.RemoteException
Called when the node wants to leave the Chord ring. WARNING: This is a graceless leave.

Specified by:
leave in interface ChordNode
Throws:
java.rmi.RemoteException

get

public java.io.Serializable get(java.lang.String key)
                         throws java.rmi.RemoteException
The publicly available "get" method. Locates the appropriate node and gets the value associated with the key from that node.

Specified by:
get in interface ChordNode
Throws:
java.rmi.RemoteException

getLocal

public java.io.Serializable getLocal(java.lang.String key)
                              throws java.rmi.RemoteException
Get the data associated with the specified key. Warning: the predecessor may be the actual owner of the key. If your predecessor recently joined the ring, other nodes may not be aware of it yet. You should forward the get request to the predecessor if it is the rightful owner. Protect your access to the predecessor with a lock on _table.

Specified by:
getLocal in interface RemoteChordNode
Parameters:
key - The key to look up.
Returns:
The value associated with the key.
Throws:
java.rmi.RemoteException

put

public boolean put(java.lang.String key,
                   java.io.Serializable data)
            throws java.rmi.RemoteException
The publicly available "put" method. Locates the appropriate node and inserts the key-value pair at that node. TODO Fill me out

Specified by:
put in interface ChordNode
Throws:
java.rmi.RemoteException

putLocal

public boolean putLocal(java.lang.String key,
                        java.io.Serializable data)
                 throws java.rmi.RemoteException
Insert a key-value pair into the hash table. Objects in this DHT are immutable, so don't overwrite the value for a key if it already exists in the hash table. Warning: the predecessor may be the actual owner of the key. If your predecessor recently joined the ring, other nodes may not be aware of it yet. You should forward the put request to the predecessor if it is the rightful owner. Protect your access to the predecessor with a lock on _table.

Specified by:
putLocal in interface RemoteChordNode
Parameters:
key - The key.
data - The value associated with the key.
Returns:
True if the insertion is successful.
Throws:
java.rmi.RemoteException

locate

public ChordNode locate(java.lang.String key)
                 throws java.rmi.RemoteException
Find the Chord node responsible for the given key. You should just use findSuccessor().

Specified by:
locate in interface ChordNode
Parameters:
key - The key to locate.
Returns:
The Chord node responsible for the key.
Throws:
java.rmi.RemoteException

getFingerTable

public ChordNode[] getFingerTable()
                           throws java.rmi.RemoteException
Specified by:
getFingerTable in interface ChordNode
Throws:
java.rmi.RemoteException

getIdentifier

public java.lang.String getIdentifier()
                               throws java.rmi.RemoteException
Specified by:
getIdentifier in interface ChordNode
Throws:
java.rmi.RemoteException

getKeys

public java.util.List<java.lang.String> getKeys()
                                         throws java.rmi.RemoteException
Specified by:
getKeys in interface ChordNode
Throws:
java.rmi.RemoteException

getNodeHash

public java.math.BigInteger getNodeHash()
                                 throws java.rmi.RemoteException
Specified by:
getNodeHash in interface ChordNode
Throws:
java.rmi.RemoteException

getNumKeys

public int getNumKeys()
               throws java.rmi.RemoteException
Specified by:
getNumKeys in interface ChordNode
Throws:
java.rmi.RemoteException

getPredecessor

public ChordNode getPredecessor()
                         throws java.rmi.RemoteException
Specified by:
getPredecessor in interface ChordNode
Throws:
java.rmi.RemoteException

getSuccessor

public ChordNode getSuccessor()
                       throws java.rmi.RemoteException
Specified by:
getSuccessor in interface ChordNode
Throws:
java.rmi.RemoteException

isReady

public boolean isReady()
                throws java.rmi.RemoteException
Description copied from interface: ChordNode
Determines that the node in question has been set up and is ready to be used.

Specified by:
isReady in interface ChordNode
Returns:
True if ready, false otherwise
Throws:
java.rmi.RemoteException

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