OverSim
Main Page
Related Pages
Namespaces
Classes
Files
File List
File Members
MyApplication.cc
Go to the documentation of this file.
1
//
2
// Copyright (C) 2009 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
23
#include <string>
24
25
#include "
UnderlayConfigurator.h
"
26
#include "
GlobalStatistics.h
"
27
28
#include "
MyMessage_m.h
"
29
30
#include "
MyApplication.h
"
31
32
Define_Module
(
MyApplication
);
33
34
// initializeApp() is called when the module is being created.
35
// Use this function instead of the constructor for initializing variables.
36
void
MyApplication::initializeApp
(
int
stage)
37
{
38
// initializeApp will be called twice, each with a different stage.
39
// stage can be either MIN_STAGE_APP (this module is being created),
40
// or MAX_STAGE_APP (all modules were created).
41
// We only care about MIN_STAGE_APP here.
42
43
if
(stage !=
MIN_STAGE_APP
)
return
;
44
45
// copy the module parameter values to our own variables
46
sendPeriod
= par(
"sendPeriod"
);
47
numToSend
= par(
"numToSend"
);
48
largestKey
= par(
"largestKey"
);
49
50
// initialize our statistics variables
51
numSent
= 0;
52
numReceived
= 0;
53
54
// tell the GUI to display our variables
55
WATCH(
numSent
);
56
WATCH(
numReceived
);
57
58
// start our timer!
59
timerMsg
=
new
cMessage(
"MyApplication Timer"
);
60
scheduleAt(simTime() +
sendPeriod
,
timerMsg
);
61
62
bindToPort
(2000);
63
}
64
65
66
// finish is called when the module is being destroyed
67
void
MyApplication::finishApp
()
68
{
69
// finish() is usually used to save the module's statistics.
70
// We'll use globalStatistics->addStdDev(), which will calculate min, max, mean and deviation values.
71
// The first parameter is a name for the value, you can use any name you like (use a name you can find quickly!).
72
// In the end, the simulator will mix together all values, from all nodes, with the same name.
73
74
globalStatistics
->
addStdDev
(
"MyApplication: Sent packets"
,
numSent
);
75
globalStatistics
->
addStdDev
(
"MyApplication: Received packets"
,
numReceived
);
76
}
77
78
79
// handleTimerEvent is called when a timer event triggers
80
void
MyApplication::handleTimerEvent
(cMessage* msg)
81
{
82
// is this our timer?
83
if
(msg ==
timerMsg
) {
84
// reschedule our message
85
scheduleAt(simTime() +
sendPeriod
,
timerMsg
);
86
87
// if the simulator is still busy creating the network,
88
// let's wait a bit longer
89
if
(
underlayConfigurator
->
isInInitPhase
())
return
;
90
91
for
(
int
i = 0; i <
numToSend
; i++) {
92
93
// let's create a random key
94
OverlayKey
randomKey(intuniform(1,
largestKey
));
95
96
MyMessage
*myMessage;
// the message we'll send
97
myMessage =
new
MyMessage
();
98
myMessage->
setType
(
MYMSG_PING
);
// set the message type to PING
99
myMessage->
setSenderAddress
(
thisNode
);
// set the sender address to our own
100
myMessage->setByteLength(100);
// set the message length to 100 bytes
101
102
numSent
++;
// update statistics
103
104
EV <<
thisNode
.
getIp
() <<
": Sending packet to "
105
<< randomKey <<
"!"
<< std::endl;
106
107
callRoute
(randomKey, myMessage);
// send it to the overlay
108
}
109
}
else
{
110
// unknown message types are discarded
111
delete
msg;
112
}
113
}
114
115
// deliver() is called when we receive a message from the overlay.
116
// Unknown packets can be safely deleted here.
117
void
MyApplication::deliver
(
OverlayKey
& key, cMessage* msg)
118
{
119
// we are only expecting messages of type MyMessage, throw away any other
120
MyMessage
*myMsg =
dynamic_cast<
MyMessage
*
>
(msg);
121
if
(myMsg == NULL) {
122
delete
msg;
// type unknown!
123
return
;
124
}
125
126
// are we a PING? send a PONG!
127
if
(myMsg->
getType
() ==
MYMSG_PING
) {
128
myMsg->
setType
(
MYMSG_PONG
);
// change type
129
130
EV <<
thisNode
.
getIp
() <<
": Got packet from "
131
<< myMsg->
getSenderAddress
() <<
", sending back!"
132
<< std::endl;
133
134
// send it back to its owner
135
sendMessageToUDP
(myMsg->
getSenderAddress
(), myMsg);
136
}
else
{
137
// only handle PING messages
138
delete
msg;
139
}
140
}
141
142
// handleUDPMessage() is called when we receive a message from UDP.
143
// Unknown packets can be safely deleted here.
144
void
MyApplication::handleUDPMessage
(cMessage* msg)
145
{
146
// we are only expecting messages of type MyMessage
147
MyMessage
*myMsg =
dynamic_cast<
MyMessage
*
>
(msg);
148
149
if
(myMsg && myMsg->
getType
() ==
MYMSG_PONG
) {
150
EV <<
thisNode
.
getIp
() <<
": Got reply!"
<< std::endl;
151
numReceived
++;
152
}
153
154
// Message isn't needed any more -> delete it
155
delete
msg;
156
}
src
applications
myapplication
MyApplication.cc
Generated on Fri Dec 7 2012 13:37:52 for OverSim by
1.8.1.2