BaseRpc Class Reference

#include <BaseRpc.h>

Inheritance diagram for BaseRpc:

RpcListener BaseApp BaseOverlay DHT DHTTestApp DHTXMLRealworldApp GIASearchApp KBRTestApp RealWorldTestApp SimpleClient Broose Chord Gia Pastry Vast List of all members.

Detailed Description

Base class for RPCs.

Base class for RPCs.

Author:
Bernhard Heep (initial)

Sebastian Mies (rpc, lookup)

Gregoire Menuel (separation from BaseRpc)


Public Member Functions

uint32_t sendRpcMessage (const TransportAddress &dest, BaseCallMessage *msg, RpcListener *rpcListener=NULL, const OverlayKey &destKey=OverlayKey::UNSPECIFIED_KEY, int rpcId=-1, simtime_t timeout=-1, int retries=0)
 Sends a Remote-Procedure-Call message to the underlay.
uint32_t sendRpcMessage (int destType, const TransportAddress &dest, BaseCallMessage *msg, RpcListener *rpcListener=NULL, const OverlayKey &destKey=OverlayKey::UNSPECIFIED_KEY, int rpcId=-1, simtime_t timeout=-1, int retries=0)
 Sends a Remote-Procedure-Call message to the underlay.
void cancelRpcMessage (uint32_t nonce)
 Cancels a Remote-Procedure-Call.
void sendRpcResponse (int destType, BaseCallMessage *call, BaseResponseMessage *response)
 Send Remote-Procedure response message and deletes call message.
void sendRpcResponse (BaseCallMessage *call, BaseResponseMessage *response)

Protected Member Functions

virtual bool internalHandleRpc (BaseCallMessage *msg)
 Handles internal rpc requests.
void initRpcs ()
 Initializes Remote-Procedure state.
void finishRpcs ()
 Deinitializes Remote-Procedure state.
virtual void internalHandleRpcMessage (BaseRpcMessage *msg)
 Handles incoming rpc messages and delegates them to the corresponsing listeners or handlers.
virtual bool handleRpc (BaseCallMessage *msg)
 Processes Remote-Procedure-Call invokation messages.
virtual void sendRpcMessageToDestination (int destType, const TransportAddress &dest, const OverlayKey &destKey, BaseOverlayMessage *message)

Protected Attributes

NodeHandle thisNode
 Virtual destructor.
bool debugOutput

Private Types

typedef hash_map< int, RpcStateRpcStates

Private Attributes

int rpcsPending
RpcListenerdefaultRpcListener
RpcStates rpcStates

Classes

class  RpcState


Member Typedef Documentation

typedef hash_map<int,RpcState> BaseRpc::RpcStates [private]


Member Function Documentation

bool BaseRpc::internalHandleRpc ( BaseCallMessage *  msg  )  [protected, virtual]

Handles internal rpc requests.


This method is used to implement basic functionionality in the BaseRpc.

Parameters:
msg The call message
Returns:
bool true, if call has been handled.

Reimplemented in BaseOverlay.

00247 {
00248     return false;
00249 }

void BaseRpc::initRpcs (  )  [protected]

Initializes Remote-Procedure state.

00053 {
00054     rpcsPending = 0;
00055     rpcStates.clear();
00056 
00057     defaultRpcListener = new RpcListener();
00058 }

void BaseRpc::finishRpcs (  )  [protected]

Deinitializes Remote-Procedure state.

00062 {
00063     // stop all rpcs
00064     for (RpcStates::iterator i = rpcStates.begin();
00065         i != rpcStates.end(); i++) {
00066         cancelAndDelete(i->second.callMsg);
00067         cancelAndDelete(i->second.timeoutMsg);
00068     }
00069     rpcStates.clear();
00070 
00071     // delete default rpc listener
00072     if (defaultRpcListener!=NULL) {
00073         delete defaultRpcListener;
00074         defaultRpcListener = NULL;
00075     }
00076 }

void BaseRpc::internalHandleRpcMessage ( BaseRpcMessage *  msg  )  [protected, virtual]

Handles incoming rpc messages and delegates them to the corresponsing listeners or handlers.

Parameters:
msg The message to handle.
00165 {
00166     // check if this is a rpc call message
00167     BaseCallMessage* rpCall = dynamic_cast<BaseCallMessage*>(msg);
00168     if (rpCall != NULL) {
00169         bool rpcHandled = true;
00170         if (!handleRpc(rpCall)) rpcHandled = internalHandleRpc(rpCall);
00171         if (!rpcHandled) {
00172             EV << "Error: RPC '" << msg->fullName()
00173                << "' was not handled" << endl;
00174             delete msg;
00175         }
00176         return;
00177     }
00178 
00179     // get nonce
00180     int nonce = msg->getNonce();
00181 
00182     // nonce known? no -> delete message and return
00183     if (rpcStates.count(nonce)==0) {
00184         EV << "RPC: Nonce Unknown!" << endl;
00185         delete msg;
00186         return;
00187     }
00188 
00189     // get state and remove from map
00190     RpcState state = rpcStates[nonce];
00191     rpcStates.erase(nonce);
00192 
00193     // is timeout message?
00194     if ( msg->isSelfMessage() && (dynamic_cast<RpcTimeoutMessage*>(msg) != NULL)) { // yes-> inform listener
00195 
00196         // retry?
00197         state.retries--;
00198         simtime_t timeout = simulation.simTime() - state.timeSent;
00199         if (state.retries>=0) {
00200             sendRpcMessageToDestination(state.destType, state.dest,
00201                                         state.destKey,
00202                                         dynamic_cast<BaseCallMessage*>
00203                                         (state.callMsg->dup()));
00204             if (timeout!=0)
00205                 scheduleAt( simulation.simTime() + timeout, msg );
00206             rpcStates[nonce] = state;
00207             return;
00208         }
00209 
00210         // inform listener
00211         if ( state.listener != NULL )
00212             state.listener->handleRpcTimeout( state.callMsg, state.dest,
00213                                               state.id );
00214 
00215         // inform overlay
00216         handleRpcTimeout( state.callMsg, state.dest, state.id );
00217 
00218     } else { // no-> handle rpc response
00219 
00220         // get parameters
00221         simtime_t rtt = simulation.simTime() - state.timeSent;
00222         BaseResponseMessage* response
00223         = dynamic_cast<BaseResponseMessage*>(msg);
00224 
00225         // inform listener
00226         if ( state.listener != NULL )
00227             state.listener->handleRpcResponse( response, state.id, rtt );
00228 
00229         // inform overlay
00230         handleRpcResponse( response, state.id, rtt );
00231 
00232         // delete response
00233         delete response;
00234     }
00235 
00236     // delete messages
00237     cancelAndDelete(state.callMsg);
00238     cancelAndDelete(state.timeoutMsg);
00239 
00240     // clean up pointers
00241     state.callMsg = NULL;
00242     state.timeoutMsg = NULL;
00243 }

uint32_t BaseRpc::sendRpcMessage ( const TransportAddress dest,
BaseCallMessage *  msg,
RpcListener rpcListener = NULL,
const OverlayKey destKey = OverlayKey::UNSPECIFIED_KEY,
int  rpcId = -1,
simtime_t  timeout = -1,
int  retries = 0 
)

Sends a Remote-Procedure-Call message to the underlay.


Compatibility function. See the other sendRpcMessage function for more information

Parameters:
dest Destination node handle
msg RPC Call Message
rpcListener RPC Listener
destKey first route the RPC to the node that is responsible for kestkey
rpcId RPC id
timeout RPC timeout
retries How often we try to resent rpc call, if it gets lost
Returns:
The nonce of the RPC
00085 {
00086     if (destKey.isUnspecified()) {
00087         return sendRpcMessage( RPC_TO_UDP, dest, msg, rpcListener, destKey, rpcId, timeout, retries );
00088     } else {
00089         // send message to key
00090         return sendRpcMessage( RPC_TO_KEY, dest, msg, rpcListener, destKey, rpcId, timeout, retries );
00091     }
00092 }  

uint32_t BaseRpc::sendRpcMessage ( int  destType,
const TransportAddress dest,
BaseCallMessage *  msg,
RpcListener rpcListener = NULL,
const OverlayKey destKey = OverlayKey::UNSPECIFIED_KEY,
int  rpcId = -1,
simtime_t  timeout = -1,
int  retries = 0 
)

Sends a Remote-Procedure-Call message to the underlay.


If no timeout is provided, a default value of 1.0 for underlay and 5.0 for a overlay rpc is used. After a timeout the message gets retransmitted for at maximum retries times.

The destKey attribute is kept untouched.

Parameters:
destType The type of destination (RPC_TO_UDP, RPC_TO_KEY, RPC_TO_LOWERTIER, RPC_TO_UPPERTIER)
dest Destination node handle
msg RPC Call Message
rpcListener RPC Listener
destKey first route the RPC to the node that is responsible for kestkey
rpcId RPC id
timeout RPC timeout
retries How often we try to resent rpc call, if it gets lost
Returns:
The nonce of the RPC
00101 {
00102 
00103     // create nonce, timeout and set default parameters
00104     uint nonce;
00105     do {
00106         nonce = intuniform( 0, 2147483647);
00107     } while (rpcStates.count(nonce) > 0);
00108 
00109 
00110     if (timeout == -1)
00111         timeout = destKey.isUnspecified() ? 1.0 : 5.0;
00112     if (rpcListener == NULL)
00113         rpcListener = defaultRpcListener;
00114 
00115     // create state
00116     RpcState state;
00117     state.id = rpcId;
00118     state.timeSent = simulation.simTime();
00119     state.dest = dest;
00120     state.destKey = destKey;
00121     state.listener = rpcListener;
00122     state.timeoutMsg = new RpcTimeoutMessage();
00123     state.timeoutMsg->setNonce(nonce);
00124     state.retries = retries;
00125     state.destType = destType;
00126 
00127 
00128 
00129     if (rpcStates.count(nonce) > 0)
00130         error("RPC nonce collision");
00131 
00132     // set message parameters
00133     msg->setNonce( nonce );
00134     msg->setSrcNode( thisNode );
00135     msg->setType(RPC);
00136 
00137     // save copy of call message in RpcState
00138     state.callMsg = dynamic_cast<BaseCallMessage*>(msg->dup());
00139 
00140     // register state
00141     rpcStates[nonce] = state;
00142 
00143     // schedule timeout message
00144     if (timeout!=0)
00145         scheduleAt( simulation.simTime() + timeout, state.timeoutMsg);
00146 
00147     sendRpcMessageToDestination(destType, dest, destKey, msg);
00148 
00149     return nonce;
00150 }

void BaseRpc::cancelRpcMessage ( uint32_t  nonce  ) 

Cancels a Remote-Procedure-Call.

Parameters:
nonce The nonce of the RPC
00154 {
00155     if (rpcStates.count(nonce)==0)
00156         return;
00157     RpcState state = rpcStates[nonce];
00158     rpcStates.erase(nonce);
00159     cancelAndDelete(state.callMsg);
00160     cancelAndDelete(state.timeoutMsg);
00161 }

void BaseRpc::sendRpcResponse ( int  destType,
BaseCallMessage *  call,
BaseResponseMessage *  response 
)

Send Remote-Procedure response message and deletes call message.


The destKey attribute is kept untouched. If call or reponse is NULL no reponse will be sent.

Parameters:
destType the destination type of the RPC
call The corresponding call message to the reponse
response The call return value
00259 {
00260     if (call==NULL || response==NULL) {
00261         if ( call!=NULL )
00262             delete call;
00263         return;
00264     }
00265     response->setSrcNode( thisNode );
00266     response->setType( RPC );
00267     response->setNonce( call->getNonce() );
00268     sendRpcMessageToDestination(destType, call->getSrcNode(), call->getSrcNode().getKey(), response );
00269     delete call;
00270 
00271 }

void BaseRpc::sendRpcResponse ( BaseCallMessage *  call,
BaseResponseMessage *  response 
)

00277 {
00278     sendRpcResponse(RPC_TO_UDP, call, response);
00279 }

bool BaseRpc::handleRpc ( BaseCallMessage *  msg  )  [protected, virtual]

Processes Remote-Procedure-Call invokation messages.


This method should be overloaded when the overlay provides RPC functionality.

Returns:
true, if rpc has been handled

Reimplemented in DHT, Broose, Chord, and Koorde.

00253 {
00254     return false;
00255 }

void BaseRpc::sendRpcMessageToDestination ( int  destType,
const TransportAddress dest,
const OverlayKey destKey,
BaseOverlayMessage *  message 
) [protected, virtual]

Reimplemented in BaseApp, and BaseOverlay.

00286 {
00287     switch(destType) {
00288       case RPC_TO_UDP:
00289           opp_error( "sendRpcMessageToDestination with UDP: Not implemented!" );
00290           delete message;
00291           break;
00292       case RPC_TO_KEY:
00293           opp_error( "sendRpcMessageToDestination with KEY: Not implemented!" );
00294           delete message;
00295           break;
00296       case RPC_TO_UPPERTIER:
00297           send( message, "to_upperTier");
00298           break;
00299       case RPC_TO_LOWERTIER:
00300           send( message, "to_lowerTier");
00301           break;
00302     }
00303 }


Member Data Documentation

NodeHandle BaseRpc::thisNode [protected]

Virtual destructor.

Reimplemented in Gia.

bool BaseRpc::debugOutput [protected]

Reimplemented in BaseApp, BaseOverlay, and DHTTestApp.

int BaseRpc::rpcsPending [private]

RpcListener* BaseRpc::defaultRpcListener [private]

RpcStates BaseRpc::rpcStates [private]


The documentation for this class was generated from the following files:
Generated on Tue Jul 24 16:51:18 2007 for ITM OverSim by  doxygen 1.5.1