Changes between Version 17 and Version 18 of OverSimDevelop
- Timestamp:
- Apr 20, 2009, 10:24:41 AM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
OverSimDevelop
v17 v18 256 256 // initializeApp is called when the module is being created. 257 257 // Use this function instead of the constructor for initializing variables. 258 void MyApplication::initializeApp(int stage) {259 258 void MyApplication::initializeApp(int stage) 259 { 260 260 // initializeApp will be called twice, each with a different stage. 261 261 // stage can be either MIN_STAGE_APP (this module is being created), or MAX_STAGE_APP (all modules were created). … … 289 289 // finalize is called when the module is being destroyed 290 290 291 void MyApplication::finishApp() {292 291 void MyApplication::finishApp() 292 { 293 293 // first we'll delete our timer 294 294 … … 317 317 318 318 for (int i = 0; i < numToSend; i++) { 319 320 319 OverlayKey randomKey(intuniform(1, largestKey)); // let's create a random key 321 320 … … 362 361 // Unhandled or unknown packets can be safely deleted here. 363 362 364 void MyApplication::handleUDPMessage(cMessage* msg) { 363 void MyApplication::handleUDPMessage(cMessage* msg) 364 { 365 365 // we are only expecting messages of type MyMessage 366 366 … … 387 387 #!cpp 388 388 class MyOverlay : public BaseOverlay { 389 public: 390 389 public: 391 390 // Routing parameters 392 391 int myKey; // our overlay key … … 408 407 // obligatory: called when we need the next hop to route a packet to the given key 409 408 NodeVector* findNode(const OverlayKey& key, // key to route to 410 int numRedundantNodes,// how many candidates for next hop we want (see getMaxNumSiblings)411 int numSiblings,// how many siblings we'll return (?) (see getMaxRedundantNodes)412 BaseOverlayMessage* msg);// message being routed409 int numRedundantNodes, // how many candidates for next hop we want (see getMaxNumSiblings) 410 int numSiblings, // how many siblings we'll return (?) (see getMaxRedundantNodes) 411 BaseOverlayMessage* msg); // message being routed 413 412 414 413 // obligatory: In general, called when we need to know whether node is amongst numSiblings closest nodes to key. 415 414 // But normally it is called with node set to this node, and asking whether this node is responsible for key. 416 415 bool isSiblingFor(const NodeHandle& node, // which node (usually thisNode) we're referring to 417 const OverlayKey& key,// key in question418 int numSiblings,// how many siblings we're querying about419 bool* err);// set to false when we couldn't determine the range416 const OverlayKey& key, // key in question 417 int numSiblings, // how many siblings we're querying about 418 bool* err); // set to false when we couldn't determine the range 420 419 421 420 // obligatory: Set the maximum number of siblings that can be queried about (usually 1) … … 479 478 // Return whether we know if the given node is responsible for the key 480 479 bool MyOverlay::isSiblingFor(const NodeHandle& node, 481 482 483 480 const OverlayKey& key, 481 int numSiblings, 482 bool* err) 484 483 { 485 484 if (node == thisNode && key == thisNode.key) { // is it our node and our key? … … 491 490 // Return the next step for the routing of the given message 492 491 NodeVector *MyOverlay::findNode(const OverlayKey& key, 493 494 495 492 int numRedundantNodes, 493 int numSiblings, 494 BaseOverlayMessage* msg) 496 495 { 497 496 NodeVector* nextHops; … … 674 673 Now we need to build !OverSim. There are two types of builds: debug (no optimizations, include debug information) and release (optimizations, with reduced debug information). Go to the root folder and type one of the following: 675 674 675 {{{ 676 #!sh 676 677 make MODE=release # release mode 677 678 make MODE=debug # debug mode 678 679 make # also sets debug mode 680 }}} 679 681 680 682 That should compile your new modules. To clear all compiled files, use make clean as usual. … … 771 773 {{{ 772 774 #!cpp 773 void MyOverlay::getNeighbors(const OverlayKey &neighborKey) { 774 MyNeighborCall *msg = new MyNeighborCall(); 775 msg->setDestinationKey(neighborKey); 776 777 // The function we'll be using to send an RPC is sendRouteRpcCall. 778 // The first value is to which tier we'll be talking. Can be either OVERLAY_COMP, TIER1_COMP, TIER2_COMP, and so on. 779 // The second parameter is the node to which we'll send the message. Can be either an OverlayKey or a TransportAddress. 780 // The third parameter is the message. 781 sendRouteRpcCall(OVERLAY_COMP, neighborKey, msg); 775 void MyOverlay::getNeighbors(const OverlayKey &neighborKey) 776 { 777 MyNeighborCall *msg = new MyNeighborCall(); 778 msg->setDestinationKey(neighborKey); 779 780 // The function we'll be using to send an RPC is sendRouteRpcCall. 781 // The first value is to which tier we'll be talking. Can be either OVERLAY_COMP, TIER1_COMP, TIER2_COMP, and so on. 782 // The second parameter is the node to which we'll send the message. Can be either an OverlayKey or a TransportAddress. 783 // The third parameter is the message. 784 sendRouteRpcCall(OVERLAY_COMP, neighborKey, msg); 785 } 786 }}} 787 788 Now, let's see how the RPC calls are implemented: 789 790 {{{ 791 #!cpp 792 // Handle an incoming Call message 793 // Only delete msg if the RPC is handled here, and you won't respond using sendRpcResponse! 794 795 bool MyOverlay::handleRpcCall(BaseCallMessage *msg) { 796 797 // There are many macros to simplify the handling of RPCs. The full list is in <OverSim>/src/common/RpcMacros.h. 798 799 // start a switch 800 RPC_SWITCH_START(msg); 801 802 // enters the following block if the message is of type MyNeighborCall (note the shortened parameter!) 803 RPC_ON_CALL(MyNeighbor) { 804 MyNeighborCall *mrpc = (MyNeighborCall*)msg; // get Call message 805 MyNeighborResponse *rrpc = new MyNeighborResponse(); // create response 806 rrpc->setRespondingNode(thisNode); 807 rrpc->setPrevNeighbor(prevNode); 808 rrpc->setNextNeighbor(nextNode); 809 810 // now send the response. sendRpcResponse can automatically tell where to send it to. 811 // note that sendRpcResponse will delete mrpc (aka msg)! 812 sendRpcResponse(mrpc, rrpc); 813 814 RPC_HANDLED = true; // set to true, since we did handle this RPC (default is false) 782 815 } 783 }}} 784 785 Now, let's see how the RPC calls are implemented: 786 787 {{{ 788 #!cpp 789 // Handle an incoming Call message 790 // Only delete msg if the RPC is handled here, and you won't respond using sendRpcResponse! 791 792 bool MyOverlay::handleRpcCall(BaseCallMessage *msg) { 793 794 // There are many macros to simplify the handling of RPCs. The full list is in <OverSim>/src/common/RpcMacros.h. 795 796 // start a switch 797 RPC_SWITCH_START(msg); 798 799 // enters the following block if the message is of type MyNeighborCall (note the shortened parameter!) 800 RPC_ON_CALL(MyNeighbor) { 801 MyNeighborCall *mrpc = (MyNeighborCall*)msg; // get Call message 802 803 MyNeighborResponse *rrpc = new MyNeighborResponse(); // create response 804 rrpc->setRespondingNode(thisNode); 805 rrpc->setPrevNeighbor(prevNode); 806 rrpc->setNextNeighbor(nextNode); 807 808 // now send the response. sendRpcResponse can automatically tell where to send it to. 809 // note that sendRpcResponse will delete mrpc (aka msg)! 810 sendRpcResponse(mrpc, rrpc); 811 812 RPC_HANDLED = true; // set to true, since we did handle this RPC (default is false) 813 } 814 815 // end the switch 816 RPC_SWITCH_END(); 817 818 // return whether we handled the message or not. 819 // don't delete unhandled messages! 820 return RPC_HANDLED; 821 } 822 823 // Called when an RPC we sent has timed out. 824 // Don't delete msg here! 825 826 void MyOverlay::handleRpcTimeout(BaseCallMessage* msg, 827 const TransportAddress& dest, 828 cPolymorphic* context, int rpcId, 829 const OverlayKey&) 830 { 831 // Same macros as in handleRpc 832 833 // start a switch 834 RPC_SWITCH_START(msg); 835 816 817 // end the switch 818 RPC_SWITCH_END(); 819 820 // return whether we handled the message or not. 821 // don't delete unhandled messages! 822 return RPC_HANDLED; 823 } 824 825 // Called when an RPC we sent has timed out. 826 // Don't delete msg here! 827 void MyOverlay::handleRpcTimeout(BaseCallMessage* msg, 828 const TransportAddress& dest, 829 cPolymorphic* context, int rpcId, 830 const OverlayKey&) 831 { 832 // Same macros as in handleRpc 833 834 // start a switch 835 RPC_SWITCH_START(msg); 836 836 // enters the following block if the message is of type MyNeighborCall (note the shortened parameter!) 837 837 RPC_ON_CALL(MyNeighbor) { … … 839 839 callbackTimeout(mrpc->getDestinationKey()); // call our interface function 840 840 } 841 842 843 841 // end the switch 842 RPC_SWITCH_END(); 843 } 844 844 845 845 // Called when we receive an RPC response from another node. 846 846 // Don't delete msg here! 847 847 848 void MyOverlay::handleRpcResponse(BaseResponseMessage* msg, 849 cPolymorphic* context, 850 int rpcId, 851 simtime_t rtt) 852 { 853 // The macros are here similar. Just use RPC_ON_RESPONSE instead of RPC_ON_CALL. 854 855 // start a switch 856 RPC_SWITCH_START(msg); 857 848 void MyOverlay::handleRpcResponse(BaseResponseMessage* msg, 849 cPolymorphic* context, 850 int rpcId, 851 simtime_t rtt) 852 { 853 // The macros are here similar. Just use RPC_ON_RESPONSE instead of RPC_ON_CALL. 854 855 // start a switch 856 RPC_SWITCH_START(msg); 858 857 // enters the following block if the message is of type MyNeighborResponse (note the shortened parameter!) 859 858 RPC_ON_RESPONSE(MyNeighbor) { … … 863 862 mrpc->getNextNeighbor()); // call our interface function 864 863 } 865 866 867 864 // end the switch 865 RPC_SWITCH_END(); 866 } 868 867 }}} 869 868