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