BaseApp.cc

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00024 #include <IPAddressResolver.h>
00025 #include <NotificationBoard.h>
00026 #include <UDPAppBase.h>
00027 #include <UDPSocket.h>
00028 #include <assert.h>
00029 
00030 #include <CommonMessages_m.h>
00031 #include <BaseRpc.h>
00032 #include <GlobalNodeListAccess.h>
00033 #include <GlobalStatisticsAccess.h>
00034 #include <UnderlayConfiguratorAccess.h>
00035 
00036 #include "BaseApp.h"
00037 
00038 using namespace std;
00039 
00040 BaseApp::BaseApp()
00041 {
00042     notificationBoard = NULL;
00043 
00044     overlay = NULL;
00045 }
00046 
00047 BaseApp::~BaseApp()
00048 {
00049     finishRpcs();
00050 }
00051 
00052 int BaseApp::numInitStages() const
00053 {
00054     return MAX_STAGE_APP + 1;
00055 }
00056 
00057 void BaseApp::initialize(int stage)
00058 {
00059     if (stage == MIN_STAGE_APP) {
00060         // fetch parameters
00061         debugOutput = par("debugOutput");
00062 
00063         globalNodeList = GlobalNodeListAccess().get();
00064         underlayConfigurator = UnderlayConfiguratorAccess().get();
00065         globalStatistics = GlobalStatisticsAccess().get();
00066         notificationBoard = NotificationBoardAccess().get();
00067 
00068         // subscribe to the notification board
00069         notificationBoard->subscribe(this, NF_OVERLAY_TRANSPORTADDRESS_CHANGED);
00070         notificationBoard->subscribe(this, NF_OVERLAY_NODE_LEAVE);
00071         notificationBoard->subscribe(this, NF_OVERLAY_NODE_GRACEFUL_LEAVE);
00072 
00073         // determine the terminal's transport address
00074         if (getParentModule()->getSubmodule("interfaceTable", 0) != NULL) {
00075             thisNode.ip = IPAddressResolver()
00076                           .addressOf(getParentModule()).get4();
00077         } else {
00078             thisNode.ip = IPAddressResolver()
00079                           .addressOf(getParentModule()->getParentModule()).get4();
00080         }
00081 
00082         thisNode.port = -1;
00083 
00084         WATCH(thisNode);
00085 
00086         // statistics
00087         numOverlaySent = 0;
00088         numOverlayReceived = 0;
00089         bytesOverlaySent = 0;
00090         bytesOverlayReceived = 0;
00091         numUdpSent = 0;
00092         numUdpReceived = 0;
00093         bytesUdpSent = 0;
00094         bytesUdpReceived = 0;
00095 
00096         creationTime = simTime();
00097 
00098         WATCH(numOverlaySent);
00099         WATCH(numOverlayReceived);
00100         WATCH(bytesOverlaySent);
00101         WATCH(bytesOverlayReceived);
00102         WATCH(numUdpSent);
00103         WATCH(numUdpReceived);
00104         WATCH(bytesUdpSent);
00105         WATCH(bytesUdpReceived);
00106 
00107         // init rpcs
00108         initRpcs();
00109     }
00110 
00111     if (stage >= MIN_STAGE_APP && stage <= MAX_STAGE_APP)
00112         initializeApp(stage);
00113 }
00114 
00115 void BaseApp::initializeApp(int stage)
00116 {
00117     // ...
00118 }
00119 
00120 // Process messages passed up from the overlay.
00121 void BaseApp::handleMessage(cMessage* msg)
00122 {
00123     BaseRpcMessage* rpcMessage = dynamic_cast<BaseRpcMessage*>(msg);
00124 
00125     if (rpcMessage!=NULL) {
00126         // process rpc messages
00127         internalHandleRpcMessage(rpcMessage);
00128     } else if (msg->isSelfMessage()) {
00129         // Process self-messages.
00130         handleTimerEvent(msg);
00131     } else if (msg->arrivedOn("from_lowerTier") ||
00132                msg->arrivedOn("direct_in")) {
00133         CompReadyMessage* readyMsg = dynamic_cast<CompReadyMessage*>(msg);
00134         if (readyMsg != NULL) {
00135             handleReadyMessage(readyMsg);
00136             return;
00137         }
00138         // common API
00139         CommonAPIMessage* commonAPIMsg = dynamic_cast<CommonAPIMessage*>(msg);
00140         if (commonAPIMsg != NULL) {
00141             handleCommonAPIMessage(commonAPIMsg);
00142         } else if (msg->arrivedOn("from_lowerTier")) {
00143             // TODO: What kind of messages to we want to measure here?
00144                 cPacket* packet = check_and_cast<cPacket*>(msg);
00145             RECORD_STATS(numOverlayReceived++;
00146                          bytesOverlayReceived += packet->getByteLength());
00147             handleLowerMessage(msg);
00148         }
00149         else delete msg;
00150     } else if (msg->arrivedOn("from_upperTier")) {
00151         handleUpperMessage(msg);
00152     } else if (msg->arrivedOn("udpIn")) {
00153         cPacket* packet = check_and_cast<cPacket*>(msg);
00154         RECORD_STATS(numUdpReceived++; bytesUdpReceived += packet->getByteLength());
00155         // debug message
00156         if (debugOutput && !ev.isDisabled()) {
00157             UDPControlInfo* udpControlInfo =
00158                 check_and_cast<UDPControlInfo*>(msg->getControlInfo());
00159             EV << "[BaseApp:handleMessage() @ " << thisNode.ip
00160             << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00161             << "    Received " << *msg << " from "
00162             << udpControlInfo->getSrcAddr() << endl;
00163         }
00164         handleUDPMessage(msg);
00165     } else if (msg->arrivedOn("trace_in")) {
00166         handleTraceMessage(msg);
00167     } else {
00168         delete msg;
00169     }
00170 }
00171 
00172 CompType BaseApp::getThisCompType()
00173 {
00174     std::string name(this->getName());
00175 
00176     if (name == std::string("tier1")) {
00177         return TIER1_COMP;
00178     } else if (name == std::string("tier2")) {
00179         return TIER2_COMP;
00180     } else if (name == std::string("tier3")) {
00181         return TIER3_COMP;
00182     }
00183 
00184     std::string parentName(this->getParentModule()->getName());
00185 
00186     if (parentName == std::string("tier1")) {
00187         return TIER1_COMP;
00188     } else if (parentName == std::string("tier2")) {
00189         return TIER2_COMP;
00190     } else if (parentName == std::string("tier3")) {
00191         return TIER3_COMP;
00192     } else {
00193         throw cRuntimeError("BaseApp::getThisCompType(): "
00194                              "Unknown module type!");
00195     }
00196 
00197     return INVALID_COMP;
00198 }
00199 
00200 
00201 void BaseApp::handleTimerEvent(cMessage* msg)
00202 {
00203     // process self-messages
00204 }
00205 
00206 void BaseApp::receiveChangeNotification(int category, const cPolymorphic * details)
00207 {
00208     Enter_Method_Silent();
00209     if (category == NF_OVERLAY_TRANSPORTADDRESS_CHANGED) {
00210         handleTransportAddressChangedNotification();
00211     } else if (category == NF_OVERLAY_NODE_LEAVE) {
00212         handleNodeLeaveNotification();
00213     } else if (category == NF_OVERLAY_NODE_GRACEFUL_LEAVE) {
00214         handleNodeGracefulLeaveNotification();
00215     }
00216 }
00217 
00218 void BaseApp::handleTransportAddressChangedNotification()
00219 {
00220     // ...
00221 }
00222 
00223 void BaseApp::handleNodeLeaveNotification()
00224 {
00225     // ...
00226 }
00227 
00228 void BaseApp::handleNodeGracefulLeaveNotification()
00229 {
00230     // ...
00231 }
00232 
00233 void BaseApp::callRoute(const OverlayKey& key, cPacket* msg,
00234                         const std::vector<TransportAddress>& sourceRoute,
00235                         RoutingType routingType)
00236 {
00237     // create route-message (common API)
00238     KBRroute* routeMsg = new KBRroute();
00239     routeMsg->setDestKey(key);
00240 
00241     if (!(sourceRoute.size() == 1 && sourceRoute[0].isUnspecified())) {
00242         routeMsg->setSourceRouteArraySize(sourceRoute.size());
00243         for (uint32_t i = 0; i < sourceRoute.size(); ++i) {
00244             routeMsg->setSourceRoute(i, sourceRoute[i]);
00245         }
00246     }
00247     routeMsg->encapsulate(msg);
00248     routeMsg->setSrcComp(thisCompType);
00249     routeMsg->setDestComp(thisCompType);
00250     routeMsg->setRoutingType(routingType);
00251 
00252     routeMsg->setType(KBR_ROUTE);
00253 
00254     sendDirect(routeMsg, overlay->getCompRpcGate(OVERLAY_COMP));
00255 
00256     // debug message
00257     if (debugOutput && !ev.isDisabled()) {
00258         EV << "[BaseApp::callRoute() @ " << thisNode.ip
00259         << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00260         << "    Sending " << *msg
00261         << " to destination key " << key
00262         << " with source route ";
00263 
00264         for (uint32_t i = 0; i < sourceRoute.size(); ++i) {
00265             EV << sourceRoute[i] << " ";
00266         }
00267 
00268         EV << endl;
00269     }
00270 
00271     // count
00272     RECORD_STATS(numOverlaySent++; bytesOverlaySent += msg->getByteLength());
00273 }
00274 
00275 void BaseApp::deliver(OverlayKey& key, cMessage* msg)
00276 {
00277     // deliver...
00278 
00279     delete msg;
00280 }
00281 
00282 void BaseApp::forward(OverlayKey* key, cPacket** msg, NodeHandle* nextHopNode)
00283 {
00284     // usually do nothing
00285 }
00286 
00287 void BaseApp::forwardResponse(const OverlayKey& key, cPacket* msg,
00288                               const NodeHandle& nextHopNode)
00289 {
00290     OverlayCtrlInfo* ctrlInfo =
00291         check_and_cast<OverlayCtrlInfo*>(msg->removeControlInfo());
00292 
00293     //create forwardResponse message (common API)
00294     KBRforward* forwardMsg = new KBRforward();
00295     forwardMsg->setDestKey(key);
00296     forwardMsg->setNextHopNode(nextHopNode);
00297     forwardMsg->setControlInfo(ctrlInfo);
00298     forwardMsg->encapsulate(msg);
00299 
00300     forwardMsg->setType(KBR_FORWARD_RESPONSE);
00301 
00302     if (getThisCompType() == TIER1_COMP) {
00303         send(forwardMsg, "to_lowerTier");
00304     } else {
00305         sendDirect(forwardMsg, overlay->getCompRpcGate(OVERLAY_COMP));
00306     }
00307 }
00308 
00309 void BaseApp::update(const NodeHandle& node, bool joined)
00310 {
00311 }
00312 
00313 void BaseApp::handleCommonAPIMessage(CommonAPIMessage* commonAPIMsg)
00314 {
00315     cPacket* tempMsg = commonAPIMsg->decapsulate();
00316 
00317     // process interface control information
00318     OverlayCtrlInfo* overlayCtrlInfo =
00319         dynamic_cast<OverlayCtrlInfo*>(commonAPIMsg->removeControlInfo());
00320 
00321     if (overlayCtrlInfo != NULL) {
00322         tempMsg->setControlInfo(overlayCtrlInfo);
00323     }
00324 
00325     switch (commonAPIMsg->getType()) {
00326         case KBR_DELIVER:
00327         {
00328             KBRdeliver* apiMsg = dynamic_cast<KBRdeliver*>(commonAPIMsg);
00329             OverlayKey key = apiMsg->getDestKey();
00330             NodeHandle nextHopNode = overlay->getThisNode();
00331 
00332             //first call forward, then deliver
00333             forward(&key, &tempMsg, &nextHopNode);
00334 
00335             if(tempMsg != NULL) {
00336                 //if key or nextHopNode is changed send msg back to overlay
00337                 if ((!key.isUnspecified() && key != apiMsg->getDestKey()) ||
00338                     (!nextHopNode.isUnspecified()
00339                             && nextHopNode != overlay->getThisNode())) {
00340                     forwardResponse(key, tempMsg, nextHopNode);
00341                 }
00342                 else {
00343                     RECORD_STATS(numOverlayReceived++;
00344                                  bytesOverlayReceived += tempMsg->getByteLength());
00345 
00346                     assert(overlayCtrlInfo->getTransportType()
00347                            == ROUTE_TRANSPORT);
00348 
00349                     // debug message
00350                     if (debugOutput) {
00351                         EV << "[BaseApp:handleCommonAPIMessage() @ "
00352                         << thisNode.ip << " ("
00353                         << overlay->getThisNode().key.toString(16) << ")]\n"
00354                         << "    Received " << *tempMsg << " from "
00355                         << overlayCtrlInfo->getSrcNode() << endl;
00356                     }
00357 
00358                     //handle RPC first
00359                     BaseRpcMessage* rpcMessage
00360                         = dynamic_cast<BaseRpcMessage*>(tempMsg);
00361                     if (rpcMessage!=NULL) {
00362                         internalHandleRpcMessage(rpcMessage);
00363                     } else {
00364                         deliver(apiMsg->getDestKey(), tempMsg);
00365                     }
00366                 }
00367             }
00368             break;
00369         }
00370 
00371         case KBR_FORWARD:
00372         {
00373             KBRforward* apiMsg = dynamic_cast<KBRforward*>(commonAPIMsg);
00374             OverlayKey key = apiMsg->getDestKey();
00375             NodeHandle nextHopNode = apiMsg->getNextHopNode();
00376 
00377             forward(&key, &tempMsg, &nextHopNode);
00378 
00379             //if message ist not deleted send it back
00380             if(tempMsg != NULL) {
00381                 if(nextHopNode == apiMsg->getNextHopNode())
00382                     //do we need this?
00383                     nextHopNode = NodeHandle::UNSPECIFIED_NODE;
00384                 forwardResponse(key, tempMsg, nextHopNode);
00385             }
00386             break;
00387         }
00388 
00389         case KBR_UPDATE:
00390         {
00391             KBRupdate* apiMsg = dynamic_cast<KBRupdate*>(commonAPIMsg);
00392             update(apiMsg->getNode(), apiMsg->getJoined());
00393 
00394             break;
00395         }
00396 
00397         default:
00398         {
00399             delete tempMsg;
00400         }
00401     }
00402     delete commonAPIMsg;
00403 }
00404 
00405 void BaseApp::handleUpperMessage(cMessage* msg)
00406 {
00407     delete msg;
00408 }
00409 
00410 void BaseApp::handleLowerMessage(cMessage* msg)
00411 {
00412     delete msg;
00413 }
00414 
00415 void BaseApp::handleUDPMessage(cMessage *msg)
00416 {
00417     delete msg;
00418 }
00419 
00420 void BaseApp::handleReadyMessage(CompReadyMessage* msg)
00421 {
00422     delete msg;
00423 }
00424 
00425 void BaseApp::handleTraceMessage(cMessage* msg)
00426 {
00427     throw cRuntimeError("This application cannot handle trace data. "
00428                          "You have to overwrite handleTraceMessage() in your "
00429                          "application to make trace files work");
00430 }
00431 
00432 void BaseApp::sendMessageToLowerTier(cPacket* msg)
00433 {
00434     RECORD_STATS(numOverlaySent++; bytesOverlaySent += msg->getByteLength());
00435 
00436     send(msg, "to_lowerTier");
00437 }
00438 
00439 void BaseApp::finish()
00440 {
00441     // record scalar data
00442     simtime_t time = globalStatistics->calcMeasuredLifetime(creationTime);
00443 
00444     string baseAppName = string("BaseApp (") += string(this->getName())
00445                          += string("): ");
00446 
00447     if (time != 0) {
00448         globalStatistics->addStdDev(baseAppName + string("Sent Messages/s to "
00449                                                          "Overlay"),
00450                                     numOverlaySent / time);
00451         globalStatistics->addStdDev(baseAppName +
00452                                     string("Received Messages/s from Overlay"),
00453                                     numOverlayReceived / time);
00454         globalStatistics->addStdDev(baseAppName + string("Sent Bytes/s to "
00455                                                          "Overlay"),
00456                                     bytesOverlaySent / time);
00457         globalStatistics->addStdDev(baseAppName + string("Received Bytes/s "
00458                                                          "from Overlay"),
00459                                     bytesOverlayReceived / time);
00460         globalStatistics->addStdDev(baseAppName + string("Sent Messages/s to "
00461                                                          "UDP"),
00462                                     numUdpSent / time);
00463         globalStatistics->addStdDev(baseAppName +
00464                                     string("Received Messages/s from UDP"),
00465                                     numUdpReceived / time);
00466         globalStatistics->addStdDev(baseAppName + string("Sent Bytes/s to UDP"),
00467                                     bytesUdpSent / time);
00468         globalStatistics->addStdDev(baseAppName + string("Received Bytes/s "
00469                                                          "from UDP"),
00470                                     bytesUdpReceived / time);
00471 
00472     }
00473 
00474     finishApp();
00475 }
00476 
00477 void BaseApp::finishApp()
00478 {
00479     // ...
00480 }
00481 
00482 void BaseApp::bindToPort(int port)
00483 {
00484     EV << "[BaseApp::bindToPort() @ " << thisNode.ip
00485        << ":  Binding to UDP port " << port << endl;
00486 
00487     thisNode.port = port;
00488 
00489     cMessage *msg = new cMessage("UDP_C_BIND", UDP_C_BIND);
00490     UDPControlInfo *ctrl = new UDPControlInfo();
00491     ctrl->setSrcPort(port);
00492     ctrl->setSockId(UDPSocket::generateSocketId());
00493     msg->setControlInfo(ctrl);
00494     send(msg, "udpOut");
00495 }
00496 
00497 void BaseApp::sendMessageToUDP(const TransportAddress& destAddr, cPacket *msg)
00498 {
00499     // send message to UDP, with the appropriate control info attached
00500     msg->removeControlInfo();
00501     msg->setKind(UDP_C_DATA);
00502 
00503     UDPControlInfo *ctrl = new UDPControlInfo();
00504     ctrl->setSrcPort(thisNode.port);
00505     ctrl->setSrcAddr(thisNode.ip);
00506     ctrl->setDestAddr(destAddr.ip);
00507     ctrl->setDestPort(destAddr.port);
00508     msg->setControlInfo(ctrl);
00509 
00510     if (ev.isGUI()) {
00511         BaseRpcMessage* rpc = dynamic_cast<BaseRpcMessage*>(msg);
00512         if (rpc) rpc->setStatType(APP_DATA_STAT);
00513     }
00514 
00515     // debug message
00516     if (debugOutput) {
00517         EV << "[BaseApp::sendMessageToUDP() @ " << thisNode.ip
00518         << " (" << overlay->getThisNode().key.toString(16) << ")]\n"
00519         << "    Sending " << *msg << " to " << destAddr.ip
00520         << endl;
00521     }
00522 
00523 
00524     RECORD_STATS(numUdpSent++; bytesUdpSent += msg->getByteLength());
00525     send(msg, "udpOut");
00526 }
00527 
00528 //private
00529 bool BaseApp::internalHandleRpc( BaseCallMessage* msg )
00530 {
00531     // if RPC was handled return true, else tell the parent class to handle it
00532     return BaseRpc::internalHandleRpc(msg);
00533 }
00534 
00535 bool BaseApp::internalHandleRpcResponse(BaseResponseMessage* msg,
00536                                         cPolymorphic* context,
00537                                         int rpcId, simtime_t rtt)
00538 {
00539     // if RPC was handled return true, else tell the parent class to handle it
00540     return BaseRpc::internalHandleRpcResponse(msg, context, rpcId, rtt);
00541 }
00542 
00543 void BaseApp::internalSendRouteRpc(BaseRpcMessage* message,
00544                                    const OverlayKey& destKey,
00545                                    const std::vector<TransportAddress>&
00546                                        sourceRoute,
00547                                    RoutingType routingType) {
00548     callRoute(destKey, message, sourceRoute, routingType);
00549 }
00550 
00551 void BaseApp::internalSendRpcResponse(BaseCallMessage* call,
00552                                       BaseResponseMessage* response)
00553 {
00554     // default values for UDP transport
00555     TransportType transportType = UDP_TRANSPORT;
00556     CompType compType = INVALID_COMP;
00557     const TransportAddress* destNode = &TransportAddress::UNSPECIFIED_NODE;//&(call->getSrcNode());
00558     const OverlayKey* destKey = &OverlayKey::UNSPECIFIED_KEY;
00559 
00560     TransportAddress tempNode;
00561 
00562     OverlayCtrlInfo* overlayCtrlInfo =
00563         dynamic_cast<OverlayCtrlInfo*>(call->getControlInfo());
00564 
00565     if (overlayCtrlInfo &&
00566         overlayCtrlInfo->getTransportType() == ROUTE_TRANSPORT) {
00567         //destNode = &(overlayCtrlInfo->getSrcNode());
00568         if (overlayCtrlInfo->getSrcNode().isUnspecified())
00569             destNode = &(overlayCtrlInfo->getLastHop());
00570         else
00571             destNode = &(overlayCtrlInfo->getSrcNode());
00572         transportType = ROUTE_TRANSPORT;
00573         compType = static_cast<CompType>(overlayCtrlInfo->getSrcComp());
00574         if (static_cast<RoutingType>(overlayCtrlInfo->getRoutingType())
00575                 == FULL_RECURSIVE_ROUTING) {
00576             destKey = &(overlayCtrlInfo->getSrcNode().key);//&(call->getSrcNode().key);
00577             destNode = &NodeHandle::UNSPECIFIED_NODE;
00578         }
00579     } else {
00580         UDPControlInfo* udpCtrlInfo =
00581             check_and_cast<UDPControlInfo*>(call->getControlInfo());
00582 
00583         tempNode = TransportAddress(udpCtrlInfo->getSrcAddr(), udpCtrlInfo->getSrcPort());
00584         destNode = &tempNode;
00585 
00586     }
00587 
00588     sendRpcResponse(transportType, compType,
00589                     *destNode, *destKey, call, response);
00590 }

Generated on Wed Mar 25 16:32:47 2009 for OverSim by  doxygen 1.5.5