OverSim
ALMTest.cc
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006 Institut fuer Telematik, Universitaet Karlsruhe (TH)
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 //
18 
25 #include <assert.h>
26 #include "ALMTest.h"
27 #include "ALMTestTracedMessage_m.h"
28 
30 
32 {
33  timer = new cMessage( "app_timer");
34  joinGroups = true;
35  sendMessages = true;
36  observer = NULL;
37 }
38 
40 {
41  cancelAndDelete( timer );
42 }
43 
44 void ALMTest::initializeApp(int stage)
45 {
46  if( stage != (numInitStages()-1))
47  {
48  return;
49  }
50  observer = check_and_cast<MessageObserver*>(
51  simulation.getModuleByPath("globalObserver.globalFunctions[0].function.observer"));
52  joinGroups = par("joinGroups");
53  msglen = par("messageLength");
54  sendMessages = par("sendMessages");
55 }
56 
58 {
59  cancelEvent(timer);
60  observer->nodeDead(getId());
61 }
62 
63 void ALMTest::handleTimerEvent( cMessage* msg )
64 {
65  if( msg == timer ) {
66  double random = uniform( 0, 1 );
67  if( (random < 0.1 && joinGroups) || groupNum < 1 ) {
68  joinGroup( ++groupNum );
69  } else if( random < 0.2 && joinGroups ) {
70  leaveGroup( groupNum-- );
71  } else if ( sendMessages ) {
72  sendDataToGroup( intuniform( 1, groupNum ));
73  }
74  scheduleAt( simTime() + 10, timer );
75  }
76 }
77 
78 void ALMTest::handleLowerMessage(cMessage* msg)
79 {
80  ALMMulticastMessage* mcast = dynamic_cast<ALMMulticastMessage*>(msg);
81  if ( mcast != 0 ) {
82  handleMCast(mcast);
83  }
84 }
85 
87 {
88  if( (getThisCompType() - msg->getComp() == 1) && msg->getReady() ) {
89  groupNum = 0;
90  cancelEvent(timer);
91  scheduleAt(simTime() + 1, timer);
92  }
93  delete msg;
94 }
95 
97 {
98  //TODO: Implement
99  assert(false);
100 }
101 
102 void ALMTest::handleUDPMessage(cMessage* msg)
103 {
104  //TODO: Implement
105  assert(false);
106 }
107 
108 void ALMTest::handleUpperMessage(cMessage* msg)
109 {
110  //TODO: Implement
111  assert(false);
112 }
113 
115 {
117  msg->setGroupId(OverlayKey(i));
118  send(msg, "to_lowerTier");
119 
120  observer->joinedGroup(getId(), OverlayKey(i));
121 }
122 
124 {
125  ALMLeaveMessage* msg = new ALMLeaveMessage;
126  msg->setGroupId(OverlayKey(i));
127  send(msg, "to_lowerTier");
128 
129  observer->leftGroup(getId(), OverlayKey(i));
130 }
131 
133 {
134  ALMMulticastMessage* msg = new ALMMulticastMessage("Multicast message");
135  msg->setGroupId(OverlayKey(i));
136 
137  ALMTestTracedMessage* traced = new ALMTestTracedMessage("Traced message");
138  traced->setTimestamp();
139  traced->setGroupId(OverlayKey(i));
140  traced->setMcastId(traced->getId());
141  traced->setSenderId(getId());
142  traced->setByteLength(msglen);
143 
144  msg->encapsulate(traced);
145 
146  send(msg, "to_lowerTier");
147 
148  observer->sentMessage(traced);
149 }
150 
152 {
153  getParentModule()->getParentModule()->bubble("Received message!");
154  EV << "[ALMTest::handleMCast()]\n"
155  << " App received data message for group: " << mcast->getGroupId()
156  << endl;
157 
158  ALMTestTracedMessage* traced = check_and_cast<ALMTestTracedMessage*>(mcast->decapsulate());
159  traced->setReceiverId(getId());
160  observer->receivedMessage(traced);
161 
162  delete traced;
163 
164  delete mcast;
165 }