viceroy
Class RemoteViceroyPeer

java.lang.Object
  |
  +--viceroy.RemoteViceroyPeer
All Implemented Interfaces:
LookupPeer, Resource, ViceroyPeer

public class RemoteViceroyPeer
extends java.lang.Object
implements ViceroyPeer

Represents a remote viceroy peer. this is a stub class that actually sends messages to a real LocalViceroyPeer on remote peer. this class is immutable

Author:
Anat Talmy
, Oren Dobzinski

Field Summary
private  Address address
          The address of this peer in the communication world.
private  ViceroyPeerManager communicationManager
          A communication manager.
private  HashValue id
          This peer's identifier in the viceroy network
private  int level
          The peer's level
 
Constructor Summary
RemoteViceroyPeer(Address add, HashValue idd, int _level, ViceroyPeerManager manager)
          constructs a new RemoteViceroyPeer.
 
Method Summary
 void changingLevelReceived(ViceroyPeer changing)
          Is activated when a changing level message was received.
 void discoverReceived(ViceroyPeer sender)
          Is activated when some peer wants to join in.
 void findRelative(ViceroyPeer searcher, Relative type)
          Finds the relative of this peer Impl note: The algorithm for the left child is as follows: find a successor with this.level+1 .
 Address getAddress()
          Returns a real-world address for sending messages
 int getLevel()
          Gets the level of this peer.
 int hashCode()
          Return the peer's id
 HashValue hashValue()
          Calculates the hash value.
 void joinDataReceived(ViceroyPeer yourSuccessor, ViceroyPeer yourPredecessor, java.util.SortedMap yourValues)
          Is activated when a JoinData message arrives
 void leaveDataReceived(ViceroyPeer _successor, ViceroyPeer _predecessor, java.util.SortedMap _values)
          Is activated when a LeaveData message arrives from the predecessor of this peer
 void leavingPeerReceived(ViceroyPeer leaving, ViceroyPeer leavingPre, ViceroyPeer leavingSuc, java.util.SortedMap leavingValues)
          Is activated once a LeavingPeerMessage arrives.
 void lookup(Resource valueToLook, LookupAlgorithm method)
          Performs a lookup operation on this peer Note: this method is used when this peer initiate a lookup request.
 void lookup(Resource valueToLook, LookupPeer returnAddress, LookupAlgorithm method)
          Performs a lookup operation on this peer Note: this method is used when this peer recieves a lookup a lookup message that was initiated by another peer.
 void lookupAnswerReceived(Resource lookedUp, ViceroyPeer answer, MessagePath path)
          Is activated when a lookup answer was received.
 void newInboundRelativeReceived(ViceroyPeer inbound, Relative type)
          Activated when a new inbound relative is received: when someone informs this peer that this peer is its parent, left child or right child.
 void newLeftChildReceived(ViceroyPeer _leftChild)
          Activated when an answer is received after this peer has initiated findLeftChild method.
 void newParentReceived(ViceroyPeer _parent)
          Activated when an answer is received after this peer has initiated findParent method.
 void newPredecessorReceived(ViceroyPeer pre)
          Sets the predecessor to the given value
 void newRightChildReceived(ViceroyPeer _rightChild)
          Activated when an answer is received after this peer has initiated findRightChild method.
 void newSuccessorReceived(ViceroyPeer succ)
          Sets the successor to the given value
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

address

private Address address
The address of this peer in the communication world. This is its real-world address.


id

private HashValue id
This peer's identifier in the viceroy network


communicationManager

private ViceroyPeerManager communicationManager
A communication manager. Sends and receives messages, deals with listeners that this peer will register to.


level

private int level
The peer's level

Constructor Detail

RemoteViceroyPeer

public RemoteViceroyPeer(Address add,
                         HashValue idd,
                         int _level,
                         ViceroyPeerManager manager)
constructs a new RemoteViceroyPeer. The new object is immutable.

Parameters:
add - The address of the remote peer
idd - The id of the remote peer
_level - The level of the remote peer
manager - a communication manager. Sends and receives messages, deals with listeners that this peer will register to.
Method Detail

joinDataReceived

public void joinDataReceived(ViceroyPeer yourSuccessor,
                             ViceroyPeer yourPredecessor,
                             java.util.SortedMap yourValues)
                      throws java.lang.IllegalStateException
Is activated when a JoinData message arrives

Specified by:
joinDataReceived in interface ViceroyPeer
Parameters:
yourSuccessor - the new successor that I should have
yourPredecessor - the new predeccessor that I should have
yourValues - the values that I should be in charge of
Throws:
java.lang.IllegalStateException - when this peer's state is not PeerState.JOINING
See Also:
PeerState.JOINING

hashCode

public int hashCode()
Return the peer's id

Overrides:
hashCode in class java.lang.Object
Returns:
the peer's id

newPredecessorReceived

public void newPredecessorReceived(ViceroyPeer pre)
Sets the predecessor to the given value

Specified by:
newPredecessorReceived in interface ViceroyPeer
Parameters:
pre - the new value of predeccesor

newSuccessorReceived

public void newSuccessorReceived(ViceroyPeer succ)
Sets the successor to the given value

Specified by:
newSuccessorReceived in interface ViceroyPeer
Parameters:
succ - the new value of successor

lookup

public void lookup(Resource valueToLook,
                   LookupPeer returnAddress,
                   LookupAlgorithm method)
Performs a lookup operation on this peer Note: this method is used when this peer recieves a lookup a lookup message that was initiated by another peer.

Specified by:
lookup in interface LookupPeer
Parameters:
valueToLook - the value to lookup
returnAddress - the peer who initiated this search
method - the lookup algorithm to use
Throws:
java.lang.IllegalStateException - if the peer is not in a legal state for looking up.
See Also:
LookupState

lookup

public void lookup(Resource valueToLook,
                   LookupAlgorithm method)
            throws ResourceNotFoundException
Performs a lookup operation on this peer Note: this method is used when this peer initiate a lookup request.

Specified by:
lookup in interface LookupPeer
Parameters:
valueToLook - the value to lookup
method - the lookup algorithm to use
Throws:
java.lang.IllegalStateException - if the peer is not in a legal state for looking up.
ResourceNotFoundException

getAddress

public Address getAddress()
Returns a real-world address for sending messages

Specified by:
getAddress in interface LookupPeer
Returns:
a real-world address for sending messages

hashValue

public HashValue hashValue()
Calculates the hash value.

Specified by:
hashValue in interface Resource
Returns:
a HashValue object with the hash-value.

leavingPeerReceived

public void leavingPeerReceived(ViceroyPeer leaving,
                                ViceroyPeer leavingPre,
                                ViceroyPeer leavingSuc,
                                java.util.SortedMap leavingValues)
Is activated once a LeavingPeerMessage arrives. if indeed the leaving peer is one of my neighbours a replacement for the leaving peer is looked for. handles both regular leave and crash leave (when the peer crashes, or disappears) Implementation Note: this method will probably be activated by the ViceroyPeerManager whenever it finds that a certain peer is no longer available (using timeout)

Specified by:
leavingPeerReceived in interface ViceroyPeer
Parameters:
leaving - the leaving peer

leaveDataReceived

public void leaveDataReceived(ViceroyPeer _successor,
                              ViceroyPeer _predecessor,
                              java.util.SortedMap _values)
                       throws java.lang.IllegalStateException
Is activated when a LeaveData message arrives from the predecessor of this peer

Specified by:
leaveDataReceived in interface ViceroyPeer
Parameters:
_successor - the successor of the leaving peer
_predecessor - the predecessor of the leaving peer
_values - the values of the leaving peer
Throws:
java.lang.IllegalStateException

getLevel

public int getLevel()
Gets the level of this peer.

Specified by:
getLevel in interface ViceroyPeer
Returns:
the level of this peer

newLeftChildReceived

public void newLeftChildReceived(ViceroyPeer _leftChild)
                          throws java.lang.IllegalStateException
Activated when an answer is received after this peer has initiated findLeftChild method.

Specified by:
newLeftChildReceived in interface ViceroyPeer
Parameters:
_leftChild - the received answer
Throws:
java.lang.IllegalStateException

newRightChildReceived

public void newRightChildReceived(ViceroyPeer _rightChild)
                           throws java.lang.IllegalStateException
Activated when an answer is received after this peer has initiated findRightChild method.

Specified by:
newRightChildReceived in interface ViceroyPeer
Parameters:
_rightChild - the received answer
Throws:
java.lang.IllegalStateException

lookupAnswerReceived

public void lookupAnswerReceived(Resource lookedUp,
                                 ViceroyPeer answer,
                                 MessagePath path)
Is activated when a lookup answer was received. Updates the local data structures with the given answer, and checks for its validity

Specified by:
lookupAnswerReceived in interface ViceroyPeer
Parameters:
lookedUp - the resource that was looked up
answer - the peer that holds this resource
path - the path that the message went through
Throws:
java.lang.IllegalStateException - if the peer is not in the correct state - if it didn't initiated this lookup

newParentReceived

public void newParentReceived(ViceroyPeer _parent)
                       throws java.lang.IllegalStateException
Activated when an answer is received after this peer has initiated findParent method.

Specified by:
newParentReceived in interface ViceroyPeer
Parameters:
_parent - the received answer
Throws:
java.lang.IllegalStateException

findRelative

public void findRelative(ViceroyPeer searcher,
                         Relative type)
Finds the relative of this peer Impl note: The algorithm for the left child is as follows: find a successor with this.level+1 . We should find the closest hash value to me (clockwise closest to me) provided that it has a level higher than mine by one. therefore we will run on the successors until we find one with the requested level. this is my left child.

Specified by:
findRelative in interface ViceroyPeer
Parameters:
searcher - the peer which initiated this search.
type - The type of the relative

discoverReceived

public void discoverReceived(ViceroyPeer sender)
                      throws java.lang.IllegalStateException,
                             ResourceNotFoundException
Is activated when some peer wants to join in.

Specified by:
discoverReceived in interface ViceroyPeer
Parameters:
sender - the peer who sent this message
java.lang.IllegalStateException
ResourceNotFoundException

newInboundRelativeReceived

public void newInboundRelativeReceived(ViceroyPeer inbound,
                                       Relative type)
Activated when a new inbound relative is received: when someone informs this peer that this peer is its parent, left child or right child.

Specified by:
newInboundRelativeReceived in interface ViceroyPeer
Parameters:
inbound - the peer that is the inbound connection
type - the type of relative

changingLevelReceived

public void changingLevelReceived(ViceroyPeer changing)
Is activated when a changing level message was received. This message indicates that some peer's level has changed, because of a re-calculation of the level. This might happen if a new peer joined as the successor of the changing peer.

Specified by:
changingLevelReceived in interface ViceroyPeer
Parameters:
changing - the peer whose level has changed