00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00027 #include <CommonMessages_m.h>
00028 #include <GlobalNodeListAccess.h>
00029 #include <UnderlayConfiguratorAccess.h>
00030 #include <GlobalStatisticsAccess.h>
00031
00032 #include <vector>
00033 #include <string>
00034
00035 #include <NeighborCache.h>
00036 #include <Vivaldi.h>
00037 #include <OverlayAccess.h>
00038 #include <assert.h>
00039
00040
00041 #include "BaseRpc.h"
00042 #include "RpcMacros.h"
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 BaseRpc::BaseRpc()
00055 {
00056 defaultRpcListener = NULL;
00057 }
00058
00059
00060 void BaseRpc::initRpcs()
00061 {
00062
00063 globalStatistics = GlobalStatisticsAccess().get();
00064
00065 rpcUdpTimeout = par("rpcUdpTimeout");
00066 rpcKeyTimeout = par("rpcKeyTimeout");
00067 rpcExponentialBackoff = par("rpcExponentialBackoff");
00068
00069 rpcsPending = 0;
00070 rpcStates.clear();
00071
00072 defaultRpcListener = new RpcListener();
00073
00074
00075 numPingSent = 0;
00076 bytesPingSent = 0;
00077 numPingResponseSent = 0;
00078 bytesPingResponseSent = 0;
00079
00080 WATCH(numPingSent);
00081 WATCH(bytesPingSent);
00082 WATCH(numPingResponseSent);
00083 WATCH(bytesPingResponseSent);
00084
00085
00086 overlay = OverlayAccess().get(this);
00087
00088
00089 thisCompType = getThisCompType();
00090 overlay->registerComp(thisCompType, this);
00091
00092 cModule *mod = getParentModule();
00093 neighborCache = NULL;
00094 while (neighborCache == NULL) {
00095 neighborCache = (NeighborCache*)mod->getSubmodule("neighborCache");
00096 mod = mod->getParentModule();
00097 if (!mod)
00098 opp_error ("BaseRpc::initRpc: Module type contains no ping cache!");
00099 }
00100 }
00101
00102
00103 void BaseRpc::finishRpcs()
00104 {
00105 cancelAllRpcs();
00106
00107
00108 if (defaultRpcListener != NULL) {
00109 delete defaultRpcListener;
00110 defaultRpcListener = NULL;
00111 }
00112 }
00113
00114 void BaseRpc::cancelAllRpcs()
00115 {
00116
00117 for (RpcStates::iterator i = rpcStates.begin();
00118 i != rpcStates.end(); i++) {
00119 cancelAndDelete(i->second.callMsg);
00120 cancelAndDelete(i->second.timeoutMsg);
00121 delete i->second.dest;
00122 i->second.dest = NULL;
00123 delete i->second.context;
00124 i->second.context = NULL;
00125 }
00126 rpcStates.clear();
00127 }
00128
00129 uint32_t BaseRpc::sendRpcCall(TransportType transportType,
00130 CompType destComp,
00131 const TransportAddress& dest,
00132
00133 const OverlayKey& destKey,
00134 BaseCallMessage* msg,
00135 cPolymorphic* context,
00136 RoutingType routingType,
00137 simtime_t timeout,
00138 int retries,
00139 int rpcId,
00140 RpcListener* rpcListener)
00141 {
00142
00143 uint32_t nonce;
00144 do {
00145 nonce = intuniform(1, 2147483647);
00146 } while (rpcStates.count(nonce) > 0);
00147
00148 if (timeout == -1) {
00149 switch (transportType) {
00150 case INTERNAL_TRANSPORT:
00151 timeout = 0;
00152 break;
00153 case UDP_TRANSPORT:
00154 timeout = rpcUdpTimeout;
00155 break;
00156 case ROUTE_TRANSPORT:
00157 timeout = (destKey.isUnspecified() ?
00158 rpcUdpTimeout :
00159 rpcKeyTimeout);
00160 break;
00161 default:
00162 throw cRuntimeError("BaseRpc::sendRpcMessage(): "
00163 "Unknown RpcTransportType!");
00164 }
00165 }
00166
00167 if (rpcListener == NULL)
00168 rpcListener = defaultRpcListener;
00169
00170
00171 RpcState state;
00172 state.id = rpcId;
00173 state.timeSent = simTime();
00174 state.dest = dest.dup();
00175 state.destKey = destKey;
00176 state.srcComp = getThisCompType();
00177 state.destComp = destComp;
00178 state.listener = rpcListener;
00179 state.timeoutMsg = new RpcTimeoutMessage();
00180 state.timeoutMsg->setNonce(nonce);
00181 state.retries = retries;
00182 state.rto = timeout;
00183 state.transportType = transportType;
00184 state.routingType = routingType;
00185 state.context = context;
00186
00187 if (rpcStates.count(nonce) > 0)
00188 error("RPC nonce collision");
00189
00190
00191 msg->setNonce(nonce);
00192 if (transportType == ROUTE_TRANSPORT)
00193 msg->setSrcNode(overlay->getThisNode());
00194 else
00195 msg->setSrcNode(thisNode);
00196 msg->setType(RPC);
00197
00198
00199 state.callMsg = dynamic_cast<BaseCallMessage*>(msg->dup());
00200 assert(!msg->getEncapsulatedMsg() || !msg->getEncapsulatedMsg()->getControlInfo());
00201
00202
00203 rpcStates[nonce] = state;
00204
00205
00206 if (state.rto != 0)
00207 scheduleAt(simTime() + state.rto, state.timeoutMsg);
00208
00209
00210 std::vector<TransportAddress> sourceRoute;
00211 sourceRoute.push_back(dest);
00212 sendRpcMessageWithTransport(transportType, destComp, routingType,
00213 sourceRoute, destKey, msg);
00214
00215 return nonce;
00216 }
00217
00218
00219
00220 void BaseRpc::cancelRpcMessage(uint32_t nonce)
00221 {
00222 if (rpcStates.count(nonce)==0)
00223 return;
00224 RpcState state = rpcStates[nonce];
00225 rpcStates.erase(nonce);
00226 cancelAndDelete(state.callMsg);
00227 cancelAndDelete(state.timeoutMsg);
00228 delete state.dest;
00229 state.dest = NULL;
00230 delete state.context;
00231 state.context = NULL;
00232 }
00233
00234
00235 void BaseRpc::internalHandleRpcMessage(BaseRpcMessage* msg)
00236 {
00237
00238 BaseCallMessage* rpCall = dynamic_cast<BaseCallMessage*>(msg);
00239 if (rpCall != NULL) {
00240 bool rpcHandled = true;
00241 if (!handleRpc(rpCall)) rpcHandled = internalHandleRpc(rpCall);
00242 if (!rpcHandled) {
00243 EV << "[BaseRpc::internalHandleRpcMessage() @ " << thisNode.ip
00244 << " (" << thisNode.key.toString(16) << ")]\n"
00245 << " Error: RPC '" << msg->getFullName()<< "' was not handled"
00246 << endl;
00247 delete msg;
00248 }
00249 return;
00250 }
00251
00252
00253 int nonce = msg->getNonce();
00254
00255
00256 if (rpcStates.count(nonce)==0) {
00257 EV << "[BaseRpc::internalHandleRpcMessage() @ " << thisNode.ip
00258 << " " << thisNode.key.toString(16) << ")]\n"
00259 << " RPC: Nonce Unknown"
00260 << endl;
00261 delete msg;
00262 return;
00263 }
00264
00265
00266 RpcState state = rpcStates[nonce];
00267 rpcStates.erase(nonce);
00268
00269
00270 if (msg->isSelfMessage() &&
00271 (dynamic_cast<RpcTimeoutMessage*>(msg) != NULL)) {
00272
00273
00274
00275 state.retries--;
00276 if (state.retries>=0) {
00277
00278 std::vector<TransportAddress> sourceRoute;
00279 sourceRoute.push_back(*state.dest);
00280 sendRpcMessageWithTransport(state.transportType, state.destComp,
00281 state.routingType,
00282 sourceRoute,
00283 state.destKey,
00284 dynamic_cast<BaseCallMessage*>
00285 (state.callMsg->dup()));
00286
00287 if (rpcExponentialBackoff) {
00288 state.rto *= 2;
00289 }
00290
00291 if (state.rto!=0)
00292 scheduleAt(simTime() + state.rto, msg);
00293
00294 state.timeSent = simTime();
00295 rpcStates[nonce] = state;
00296 return;
00297 }
00298
00299 if (state.listener != NULL)
00300 state.listener->handleRpcTimeout(state.callMsg, *state.dest,
00301 state.context, state.id,
00302 state.destKey);
00303
00304
00305 internalHandleRpcTimeout(state.callMsg, *state.dest, state.context,
00306 state.id, state.destKey);
00307 handleRpcTimeout(state.callMsg, *state.dest, state.context,
00308 state.id, state.destKey);
00309
00310 } else {
00311
00312
00313 if (state.destKey.isUnspecified()) {
00314 const NodeHandle* stateHandle =
00315 dynamic_cast<const NodeHandle*>(state.dest);
00316 if (stateHandle != NULL &&
00317 stateHandle->key != msg->getSrcNode().key) {
00318
00319 EV << "[BaseRpc::internalHandleRpcMessage() @ "
00320 << thisNode.ip
00321 << " " << thisNode.key.toString(16) << ")]\n"
00322 << " Dropping RPC: Invalid source key"
00323 << endl;
00324
00325
00326 rpcStates[nonce] = state;
00327 delete msg;
00328 return;
00329 }
00330 }
00331
00332
00333 simtime_t rtt = simTime() - state.timeSent;
00334 BaseResponseMessage* response
00335 = dynamic_cast<BaseResponseMessage*>(msg);
00336
00337
00338
00339
00340
00341
00342 if (state.listener != NULL)
00343 state.listener->handleRpcResponse(response, state.context,
00344 state.id, rtt);
00345
00346
00347 if (state.transportType == UDP_TRANSPORT) {
00348 if (neighborCache->vivaldiEnabled()) {
00349
00350 std::vector<double> nodeCoords;
00351 nodeCoords.resize(response->getCoordsArraySize());
00352 for (uint32_t i = 0; i < response->getCoordsArraySize(); i++)
00353 nodeCoords[i] = response->getCoords(i);
00354 neighborCache->updateNode(response->getSrcNode(), rtt,
00355 nodeCoords, response->getError());
00356 } else {
00357 neighborCache->updateNode(response->getSrcNode(), rtt);
00358 }
00359 }
00360
00361
00362 internalHandleRpcResponse(response, state.context, state.id, rtt);
00363 handleRpcResponse(response, state.context, state.id, rtt);
00364
00365
00366 delete response->removeControlInfo();
00367 delete response;
00368 }
00369
00370
00371 delete state.callMsg;
00372 cancelAndDelete(state.timeoutMsg);
00373 delete state.dest;
00374
00375
00376 state.dest = NULL;
00377 state.context = NULL;
00378 state.callMsg = NULL;
00379 state.timeoutMsg = NULL;
00380 }
00381
00382
00383 bool BaseRpc::internalHandleRpc( BaseCallMessage* msg )
00384 {
00385 RPC_SWITCH_START( msg );
00386 RPC_DELEGATE( Ping, pingRpc );
00387 RPC_SWITCH_END( );
00388
00389 return RPC_HANDLED;
00390 }
00391
00392 bool BaseRpc::internalHandleRpcResponse(BaseResponseMessage* msg,
00393 cPolymorphic* context,
00394 int rpcId, simtime_t rtt)
00395 {
00396
00397 RPC_SWITCH_START( msg );
00398 RPC_ON_RESPONSE( Ping ) {
00399 pingRpcResponse(_PingResponse, context, rpcId, rtt);
00400 }
00401 RPC_SWITCH_END( );
00402
00403 return RPC_HANDLED;
00404 }
00405
00406 void BaseRpc::internalHandleRpcTimeout(BaseCallMessage* msg,
00407 const TransportAddress& dest,
00408 cPolymorphic* context,
00409 int rpcId, const OverlayKey& destKey)
00410 {
00411 RPC_SWITCH_START( msg ) {
00412 RPC_ON_CALL( Ping ) {
00413 pingRpcTimeout(_PingCall, dest, context, rpcId);
00414 }
00415 }
00416 RPC_SWITCH_END( )
00417 }
00418
00419
00420 bool BaseRpc::handleRpc( BaseCallMessage* msg )
00421 {
00422 return false;
00423 }
00424
00425 void BaseRpc::sendRpcResponse(TransportType transportType,
00426 CompType compType,
00427 const TransportAddress& dest,
00428 const OverlayKey& destKey,
00429 BaseCallMessage* call,
00430 BaseResponseMessage* response)
00431 {
00432 if (call == NULL || response == NULL) {
00433 throw cRuntimeError("call or response = NULL!");
00434 }
00435
00436
00437 if (neighborCache->vivaldiEnabled()) {
00438 const std::vector<double>& nodeCoord =
00439 neighborCache->getVivaldiAccess().getOwnCoordinates();
00440 response->setError(neighborCache->getVivaldiAccess().getOwnError());
00441 response->setCoordsArraySize(nodeCoord.size());
00442 for (uint32_t i = 0; i < nodeCoord.size(); i++) {
00443 response->setCoords(i, nodeCoord[i]);
00444 }
00445 }
00446
00447 assert(transportType == INTERNAL_TRANSPORT ||
00448 !dest.isUnspecified() ||
00449 !destKey.isUnspecified());
00450
00451 if (transportType == ROUTE_TRANSPORT)
00452 response->setSrcNode(overlay->getThisNode());
00453 else
00454 response->setSrcNode(thisNode);
00455 response->setType(RPC);
00456 response->setNonce(call->getNonce());
00457 response->setStatType(call->getStatType());
00458
00459 RoutingType routingType = NO_OVERLAY_ROUTING;
00460 OverlayCtrlInfo* overlayCtrlInfo = NULL;
00461 if (dynamic_cast<OverlayCtrlInfo*>(call->getControlInfo()))
00462 overlayCtrlInfo =
00463 static_cast<OverlayCtrlInfo*>(call->removeControlInfo());
00464 else
00465 delete call->removeControlInfo();
00466
00467
00468 std::vector<TransportAddress> sourceRoute;
00469 if (overlayCtrlInfo && transportType == ROUTE_TRANSPORT) {
00470 routingType =
00471 static_cast<RoutingType>(overlayCtrlInfo->getRoutingType());
00472 for (uint32_t i = overlayCtrlInfo->getVisitedHopsArraySize(); i > 0; --i) {
00473 if (overlayCtrlInfo->getVisitedHops(i - 1) !=
00474 overlay->getThisNode()) {
00475 sourceRoute.push_back(overlayCtrlInfo->getVisitedHops(i - 1));
00476 }
00477 }
00478 }
00479 sourceRoute.push_back(dest);
00480
00481 sendRpcMessageWithTransport(transportType, compType,
00482 routingType, sourceRoute,
00483 destKey, response);
00484 delete overlayCtrlInfo;
00485 delete call;
00486 }
00487
00488
00489 void BaseRpc::sendRpcResponse(BaseCallMessage* call,
00490 BaseResponseMessage* response)
00491 {
00492 const TransportAddress* destNode = &(call->getSrcNode());
00493 const OverlayKey* destKey = &(call->getSrcNode().key);
00494
00495 OverlayCtrlInfo* overlayCtrlInfo =
00496 dynamic_cast<OverlayCtrlInfo*>(call->getControlInfo());
00497
00498
00499 if (overlayCtrlInfo &&
00500 overlayCtrlInfo->getTransportType() == INTERNAL_TRANSPORT) {
00501 sendRpcResponse(INTERNAL_TRANSPORT,
00502 static_cast<CompType>(overlayCtrlInfo->getSrcComp()),
00503 *destNode, *destKey, call, response);
00504 } else {
00505 internalSendRpcResponse(call, response);
00506 }
00507 }
00508
00509 void BaseRpc::sendRpcMessageWithTransport(TransportType transportType,
00510 CompType destComp,
00511 RoutingType routingType,
00512 const std::vector<TransportAddress>& sourceRoute,
00513 const OverlayKey& destKey,
00514 BaseRpcMessage* message)
00515 {
00516 switch (transportType) {
00517 case UDP_TRANSPORT: {
00518 sendMessageToUDP(sourceRoute[0], message);
00519 break;
00520 }
00521 case ROUTE_TRANSPORT: {
00522 internalSendRouteRpc(message, destKey,
00523 sourceRoute, routingType);
00524 break;
00525 }
00526 case INTERNAL_TRANSPORT: {
00527 cGate *destCompGate = overlay->getCompRpcGate(destComp);
00528 if (destCompGate == NULL) {
00529 opp_error("BaseRpc::sendRpcMessageWithTransport():"
00530 " INTERNAL_RPC to unknown RpcCompType!");
00531 }
00532 OverlayCtrlInfo *overlayCtrlInfo = new OverlayCtrlInfo();
00533 overlayCtrlInfo->setSrcComp(getThisCompType());
00534 overlayCtrlInfo->setDestComp(destComp);
00535 overlayCtrlInfo->setTransportType(INTERNAL_TRANSPORT);
00536 message->setControlInfo(overlayCtrlInfo);
00537 sendDirect(message, destCompGate);
00538 break;
00539 }
00540 default:
00541 opp_error("BaseRpc::sendRpcMessageWithTransport: "
00542 "invalid transportType!");
00543 break;
00544 }
00545 }
00546
00547
00548 void BaseRpc::pingResponse(PingResponse* response, cPolymorphic* context,
00549 int rpcId, simtime_t rtt)
00550 {
00551 }
00552
00553 void BaseRpc::pingTimeout(PingCall* call, const TransportAddress& dest,
00554 cPolymorphic* context, int rpcId)
00555 {
00556 }
00557
00558 void BaseRpc::pingRpc( PingCall* call )
00559 {
00560 std::string pongName(call->getName());
00561 if (pongName == "PING")
00562 pongName = "PONG";
00563 else {
00564 pongName = "PONG: [ ";
00565 pongName += call->getName();
00566 pongName += " ]";
00567 }
00568
00569 PingResponse* response = new PingResponse(pongName.c_str());
00570 response->setBitLength( PINGRESPONSE_L(response) );
00571 RECORD_STATS(numPingResponseSent++; bytesPingResponseSent +=
00572 response->getByteLength());
00573
00574 sendRpcResponse(call, response );
00575 }
00576
00577 void BaseRpc::pingRpcResponse(PingResponse* response,
00578 cPolymorphic* context, int rpcId, simtime_t rtt)
00579 {
00580 NodeHandle& srcNode = response->getSrcNode();
00581 WaitingContexts* waitingContexts =
00582 neighborCache->getNodeContexts(srcNode);
00583
00584 pingResponse(response, context, rpcId, rtt);
00585
00586 if (waitingContexts && waitingContexts->size() > 0) {
00587 for (uint32_t i = 0; i < waitingContexts->pingListeners.size(); ++i) {
00588 PingResponse *pr = new PingResponse();
00589 pr->setNonce(0);
00590 if (neighborCache->getNodeHandle(srcNode).isUnspecified())
00591 pr->setSrcNode(srcNode);
00592 else
00593 pr->setSrcNode(neighborCache->getNodeHandle(srcNode));
00594
00595
00596 if (waitingContexts->pingListeners[i] != NULL)
00597 waitingContexts->pingListeners[i]
00598 ->handleRpcResponse(pr, waitingContexts->pingContexts[i],
00599 waitingContexts->pingIds[i], rtt);
00600
00601 handleRpcResponse(pr, waitingContexts->pingContexts[i],
00602 waitingContexts->pingIds[i], rtt);
00603 pingResponse(pr, waitingContexts->pingContexts[i],
00604 waitingContexts->pingIds[i], rtt);
00605
00606 delete pr;
00607 }
00608 }
00609 delete waitingContexts;
00610 }
00611
00612 void BaseRpc::pingRpcTimeout(PingCall* pingCall,
00613 const TransportAddress& dest,
00614 cPolymorphic* context,
00615 int rpcId)
00616 {
00617 neighborCache->setNodeTimeout(dest);
00618 WaitingContexts* waitingContexts =
00619 neighborCache->getNodeContexts(dest);
00620
00621 pingTimeout(pingCall, dest, context, rpcId);
00622
00623 if (waitingContexts && waitingContexts->size() > 0) {
00624 for (uint32_t i = 0; i < waitingContexts->pingListeners.size(); ++i) {
00625 PingCall* copy = static_cast<PingCall*>(pingCall->dup());
00626 copy->setNonce(0);
00627
00628
00629 if (waitingContexts->pingListeners[i] != NULL)
00630 waitingContexts->pingListeners[i]
00631 ->handleRpcTimeout(copy, dest,
00632 waitingContexts->pingContexts[i],
00633 waitingContexts->pingIds[i],
00634 OverlayKey::UNSPECIFIED_KEY);
00635
00636 handleRpcTimeout(copy, dest, waitingContexts->pingContexts[i],
00637 waitingContexts->pingIds[i],
00638 OverlayKey::UNSPECIFIED_KEY);
00639 pingTimeout(copy, dest, waitingContexts->pingContexts[i],
00640 waitingContexts->pingIds[i]);
00641
00642 delete copy;
00643 }
00644 }
00645 delete waitingContexts;
00646 }
00647 void BaseRpc::pingNode(const TransportAddress& dest, simtime_t timeout,
00648 int retries, cPolymorphic* context,
00649 const char* caption, RpcListener* rpcListener,
00650 int rpcId, TransportType transportType,
00651 bool overrideCache)
00652 {
00653 simtime_t rtt;
00654 if (overrideCache || neighborCache->getNodeRtt(dest) ==
00655 NEIGHBORSTATE_UNKNOWN || neighborCache->getNodeRtt(dest) ==
00656 NEIGHBORSTATE_TIMEOUT) {
00657
00658 PingCall* call = new PingCall(caption);
00659 call->setBitLength(PINGCALL_L(call));
00660 RECORD_STATS(numPingSent++; bytesPingSent += call->getByteLength());
00661
00662 if (transportType == UDP_TRANSPORT ||
00663 (transportType != ROUTE_TRANSPORT &&
00664 getThisCompType() == OVERLAY_COMP)) {
00665 sendUdpRpcCall(dest, call, context, timeout, retries, rpcId,
00666 rpcListener);
00667 } else {
00668 sendRouteRpcCall(getThisCompType(), dest, call, context,
00669 DEFAULT_ROUTING, timeout, retries, rpcId,
00670 rpcListener);
00671 }
00672 neighborCache->insertNodeContext(dest, context, rpcListener, rpcId);
00673 } else if ((rtt = neighborCache->getNodeRtt(dest)) !=
00674 NEIGHBORSTATE_WAITING) {
00675
00676 PingResponse *pr = new PingResponse();
00677 pr->setNonce(0);
00678 if (neighborCache->getNodeHandle(dest).isUnspecified())
00679 pr->setSrcNode(dest);
00680 else
00681 pr->setSrcNode(neighborCache->getNodeHandle(dest));
00682
00683
00684 if (rpcListener != NULL)
00685 rpcListener->handleRpcResponse(pr, context, rpcId, rtt);
00686
00687 pingResponse(pr, context, rpcId, rtt);
00688 handleRpcResponse(pr, context, rpcId, rtt);
00689
00690 delete pr;
00691 } else {
00692 assert(neighborCache->getNodeRtt(dest) == NEIGHBORSTATE_WAITING);
00693 neighborCache->insertNodeContext(dest, context, rpcListener, rpcId);
00694 }
00695 }