Switch to unified view

a b/src/DAF/AE/AESender.cc
1
//
2
// This program is free software: you can redistribute it and/or modify
3
// it under the terms of the GNU Lesser General Public License as published by
4
// the Free Software Foundation, either version 3 of the License, or
5
// (at your option) any later version.
6
// 
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
// GNU Lesser General Public License for more details.
11
// 
12
// You should have received a copy of the GNU Lesser General Public License
13
// along with this program.  If not, see http://www.gnu.org/licenses/.
14
// 
15
16
#include "AESender.h"
17
18
Define_Module(AESender);
19
20
21
AESender::AESender() : AE() {
22
    S_TIM_START           = "StartCommunication";
23
    S_TIM_COM             = "MakeCommunication";
24
    S_TIM_STOP            = "StopCommunication";
25
26
    S_PAR_START           = "startAt";
27
    S_PAR_STOP            = "stopAt";
28
    S_PAR_SEND            = "sendAfter";
29
30
    S_PAR_DSTAPNAME       = "dstApName";
31
    S_PAR_DSTAPINSTANCE   = "dstApInstance";
32
    S_PAR_DSTAENAME       = "dstAeName";
33
    S_PAR_DSTAEINSTANCE   = "dstAeInstance";
34
35
    S_PAR_RATE            = "rate";
36
    S_PAR_RATE_VAR        = "ratevar";
37
    S_PAR_SIZE            = "size";
38
    S_PAR_SIZE_VAR        = "sizevar";
39
40
    S_MSG_PING            = "PING-";
41
    S_PAR_PING            = "pingAt";
42
    S_VAL_MODULEPATH      = "getFullPath()";
43
}
44
45
AESender::~AESender() {
46
    connectionState = NIL;
47
    FlowObject = NULL;
48
    Irm = NULL;
49
    Cdap = NULL;
50
}
51
52
void AESender::prepareAllocateRequest() {
53
    //Schedule AllocateRequest
54
    cMessage* m1 = new cMessage(S_TIM_START);
55
    scheduleAt(startAt, m1);
56
}
57
58
59
void AESender::prepareDeallocateRequest() {
60
    //Schedule DeallocateRequest
61
    cMessage* m3 = new cMessage(S_TIM_STOP);
62
    scheduleAt(stopAt, m3);
63
}
64
65
void AESender::initialize()
66
{
67
    //Init pointers
68
    initPointers();
69
    //Source info
70
    initNamingInfo();
71
    //Setup signals
72
    initSignalsAndListeners();
73
    //Init QoSRequirements
74
    initQoSRequiremets();
75
76
    //Timers
77
    startAt = simTime() + par(S_PAR_START);
78
    stopAt  = simTime() + par(S_PAR_STOP);
79
    sendAfter  = par(S_PAR_SEND);
80
    if(sendAfter<1){
81
        sendAfter = 1;
82
    }
83
84
    //Time between PDUS (s)
85
    rate    = par(S_PAR_RATE).doubleValue();
86
    ratevar    = par(S_PAR_RATE_VAR).doubleValue();
87
88
    //SIZE of PDU
89
    size    = par(S_PAR_SIZE);
90
    sizevar    = par(S_PAR_SIZE_VAR);
91
92
    //Destination for flow
93
    dstApName     = this->par(S_PAR_DSTAPNAME).stringValue();
94
    dstApInstance = this->par(S_PAR_DSTAPINSTANCE).stringValue();
95
    dstAeName     = this->par(S_PAR_DSTAENAME).stringValue();
96
    dstAeInstance = this->par(S_PAR_DSTAEINSTANCE).stringValue();
97
98
    //Schedule AllocateRequest
99
    if (startAt > 0)
100
        prepareAllocateRequest();
101
    //Schedule DeallocateRequest
102
    /*
103
    if (stopAt > 0)
104
        prepareDeallocateRequest();
105
    */
106
    myPath = this->getFullPath();
107
108
    send = 0;
109
    received = 0;
110
    pingreceived = 0;
111
    sendSize = 0;
112
    receivedSize = 0;
113
    pingreceivedSize = 0;
114
115
    minDelay = 999;
116
    maxDelay = -1;
117
    firstR = -1;
118
    lastR = 0;
119
120
    //Watchers
121
    WATCH(FlowObject);
122
    WATCH(send);
123
    WATCH(received);
124
    WATCH(pingreceived);
125
    WATCH(sendSize);
126
    WATCH(receivedSize);
127
    WATCH(pingreceivedSize);
128
    WATCH(minDelay);
129
    WATCH(maxDelay);
130
}
131
132
void AESender::finish()
133
{
134
    EV << "At "<<this->getApni()<<endl;
135
    EV << send << " ("<<sendSize << ")"<<endl;
136
    EV << received << " ("<<receivedSize << ")"<<endl;
137
    EV << pingreceived << " ("<<pingreceivedSize << ")"<<endl;
138
    EV << minDelay << " / "<<maxDelay<<endl;
139
    EV << firstR << " -> "<<lastR<<endl;
140
    EV << "-----------------"<<endl;
141
}
142
143
void AESender::handleSelfMessage(cMessage *msg) {
144
    //EV << flows.back().info() << endl;
145
    if ( !strcmp(msg->getName(), S_TIM_START) ) {
146
        //FIXME: Vesely - last flow in a list?!
147
148
        //Flow
149
        APNamingInfo src = this->getApni();
150
        APNamingInfo dst = APNamingInfo( APN(this->dstApName), this->dstApInstance,
151
                                         this->dstAeName, this->dstAeInstance);
152
153
        FlowObject = new Flow(src, dst);
154
        FlowObject->setQosParameters(this->getQoSRequirements());
155
156
        //Insert it to the Flows ADT
157
        insertFlow();
158
159
        sendAllocationRequest(FlowObject);
160
161
        //Schedule ComRequest
162
        cMessage* m = new cMessage(S_TIM_COM);
163
        scheduleAt(simTime()+sendAfter, m);
164
    }
165
    else if ( !strcmp(msg->getName(), S_TIM_STOP) ) {
166
        sendDeallocationRequest(FlowObject);
167
    }
168
    else if ( !strcmp(msg->getName(), S_TIM_COM) ) {
169
        if(stopAt > simTime()){
170
            int msgSize = size + intuniform(-sizevar,sizevar);
171
            double msgWait = rate + intuniform(-ratevar,ratevar);
172
173
            //Create PING messsage
174
            CDAP_M_Read* ping = new CDAP_M_Read(S_VAL_MODULEPATH);
175
            object_t obj;
176
            obj.objectName = S_VAL_MODULEPATH;
177
            obj.objectClass = "string";
178
            obj.objectInstance = -1;
179
            obj.objectVal = (cObject*)(&myPath);
180
            ping->setObject(obj);
181
182
            ping->setByteLength(msgSize);
183
184
            //Send message
185
            sendData(FlowObject, ping);
186
            send++;
187
            sendSize += msgSize;
188
189
            //Schedule ComRequest
190
            cMessage* m = new cMessage(S_TIM_COM);
191
            scheduleAt(simTime()+msgWait, m);
192
        }
193
    }
194
    else
195
        EV << this->getFullPath() << " received unknown self-message " << msg->getName();
196
    delete(msg);
197
}
198
199
void AESender::handleMessage(cMessage *msg)
200
{
201
    if ( msg->isSelfMessage() )
202
            this->handleSelfMessage(msg);
203
}
204
205
void AESender::processMRead(CDAPMessage* msg) {
206
    CDAP_M_Read* msg1 = check_and_cast<CDAP_M_Read*>(msg);
207
208
    EV << "Received M_Read";
209
    object_t object = msg1->getObject();
210
    EV << " with object '" << object.objectClass << "'" << endl;
211
212
    if ( strstr(object.objectName.c_str(), S_VAL_MODULEPATH) ) {
213
        std::string* source = (std::string*)(object.objectVal);
214
        std::ostringstream os;
215
        os << "Ping requested by " <<  *source << endl;
216
        bubble(os.str().c_str());
217
        EV << os.str().c_str();
218
219
        //Create PING response
220
        CDAP_M_Read_R* pong = new CDAP_M_Read_R(S_VAL_MODULEPATH);
221
        object_t obj;
222
        obj.objectName = S_VAL_MODULEPATH;
223
        obj.objectClass = "string";
224
        obj.objectInstance = -1;
225
        obj.objectVal = (cObject*)(&myPath);
226
        pong->setObject(obj);
227
        pong->setByteLength(msg->getByteLength());
228
229
        sendData(FlowObject, pong);
230
231
        pingreceived++;
232
        pingreceivedSize += msg->getByteLength();
233
        simtime_t delay = simTime() - msg->getCreationTime();
234
        if(minDelay>delay){
235
            minDelay = delay;
236
        }
237
        if(maxDelay<delay){
238
            maxDelay = delay;
239
        }
240
        if(firstR<0)  {
241
            firstR = simTime();
242
        }
243
        lastR = simTime();
244
    }
245
}
246
247
void AESender::processMReadR(CDAPMessage* msg) {
248
    CDAP_M_Read_R* msg1 = check_and_cast<CDAP_M_Read_R*>(msg);
249
250
    EV << "Received M_Read_R";
251
    object_t object = msg1->getObject();
252
    EV << " with object '" << object.objectClass << "'" << endl;
253
254
    if ( strstr(object.objectName.c_str(), S_VAL_MODULEPATH) ) {
255
        std::string* source = (std::string*)(object.objectVal);
256
        std::ostringstream os;
257
        os << "Ping replied by " <<  *source << endl;
258
        bubble(os.str().c_str());
259
        EV << os.str().c_str();
260
261
        received++;
262
        receivedSize += msg->getByteLength();
263
        simtime_t delay = simTime() - msg->getCreationTime();
264
        if(minDelay>delay){
265
            minDelay = delay;
266
        }
267
        if(maxDelay<delay){
268
            maxDelay = delay;
269
        }
270
        if(firstR<0)  {
271
            firstR = simTime();
272
        }
273
        lastR = simTime();
274
    }
275
}