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 00025 #include <assert.h> 00026 #include "ALMTest.h" 00027 #include "ALMTestTracedMessage_m.h" 00028 00029 Define_Module(ALMTest); 00030 00031 ALMTest::ALMTest() 00032 { 00033 timer = new cMessage( "app_timer"); 00034 joinGroups = true; 00035 } 00036 00037 ALMTest::~ALMTest() 00038 { 00039 cancelAndDelete( timer ); 00040 } 00041 00042 void ALMTest::initializeApp(int stage) { 00043 if( stage != (numInitStages()-1)) 00044 { 00045 return; 00046 } 00047 observer = check_and_cast<MessageObserver*>( 00048 simulation.getModuleByPath("globalObserver.globalFunctions[0].function.observer")); 00049 joinGroups = par("joinGroups"); 00050 msglen = par("messageLength"); 00051 } 00052 00053 void ALMTest::handleTimerEvent( cMessage* msg ) 00054 { 00055 if( msg == timer ) { 00056 double random = uniform( 0, 1 ); 00057 if( (random < 0.1 && joinGroups) || groupNum < 1 ) { 00058 joinGroup( ++groupNum ); 00059 } else if( random < 0.2 && joinGroups ) { 00060 leaveGroup( groupNum-- ); 00061 } else { 00062 sendDataToGroup( intuniform( 1, groupNum )); 00063 } 00064 scheduleAt( simTime() + 10, timer ); 00065 } 00066 } 00067 00068 void ALMTest::handleLowerMessage(cMessage* msg) 00069 { 00070 ALMMulticastMessage* mcast = dynamic_cast<ALMMulticastMessage*>(msg); 00071 if ( mcast != 0 ) { 00072 handleMCast(mcast); 00073 } 00074 } 00075 00076 void ALMTest::handleReadyMessage(CompReadyMessage* msg) 00077 { 00078 if( (getThisCompType() - msg->getComp() == 1) && msg->getReady() ) { 00079 groupNum = 0; 00080 cancelEvent(timer); 00081 scheduleAt(simTime() + 1, timer); 00082 } 00083 delete msg; 00084 } 00085 00086 void ALMTest::handleTransportAddressChangedNotification() 00087 { 00088 //TODO: Implement 00089 assert(false); 00090 } 00091 00092 void ALMTest::handleUDPMessage(cMessage* msg) 00093 { 00094 //TODO: Implement 00095 assert(false); 00096 } 00097 00098 void ALMTest::handleUpperMessage(cMessage* msg) 00099 { 00100 //TODO: Implement 00101 assert(false); 00102 } 00103 00104 void ALMTest::joinGroup(int i) 00105 { 00106 ALMSubscribeMessage* msg = new ALMSubscribeMessage; 00107 msg->setGroupId(OverlayKey(i)); 00108 send(msg, "to_lowerTier"); 00109 00110 observer->joinedGroup(getId(), OverlayKey(i)); 00111 } 00112 00113 void ALMTest::leaveGroup(int i) 00114 { 00115 ALMLeaveMessage* msg = new ALMLeaveMessage; 00116 msg->setGroupId(OverlayKey(i)); 00117 send(msg, "to_lowerTier"); 00118 00119 observer->leftGroup(getId(), OverlayKey(i)); 00120 } 00121 00122 void ALMTest::sendDataToGroup( int i ) 00123 { 00124 ALMMulticastMessage* msg = new ALMMulticastMessage("Multicast message"); 00125 msg->setGroupId(OverlayKey(i)); 00126 00127 ALMTestTracedMessage* traced = new ALMTestTracedMessage("Traced message"); 00128 traced->setTimestamp(); 00129 traced->setGroupId(OverlayKey(i)); 00130 traced->setMcastId(traced->getId()); 00131 traced->setSenderId(getId()); 00132 traced->setByteLength(msglen); 00133 00134 msg->encapsulate(traced); 00135 00136 send(msg, "to_lowerTier"); 00137 00138 observer->sentMessage(traced); 00139 } 00140 00141 void ALMTest::handleMCast( ALMMulticastMessage* mcast ) 00142 { 00143 getParentModule()->getParentModule()->bubble("Received message!"); 00144 EV << "[ALMTest::handleMCast()]\n" 00145 << " App received data message for group: " << mcast->getGroupId() 00146 << endl; 00147 00148 ALMTestTracedMessage* traced = check_and_cast<ALMTestTracedMessage*>(mcast->decapsulate()); 00149 traced->setReceiverId(getId()); 00150 observer->receivedMessage(traced); 00151 00152 delete traced; 00153 00154 delete mcast; 00155 }