Switch to unified view

a b/src/DAF/AE/AEExtendedPing.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 "AEExtendedPing.h"
17
18
Define_Module(AEExtendedPing);
19
20
//Consts
21
const char* TIM_START_E           = "StartCommunication";
22
const char* TIM_CONNECT_E         = "StartConnection";
23
const char* TIM_STOP_E            = "StopCommunication";
24
const char* MSG_PING_E            = "PING-";
25
const char* PAR_START_E           = "startAt";
26
const char* PAR_STOP_E            = "stopAt";
27
const char* PAR_CONNECT_E         = "connectAt";
28
const char* PAR_PING_E            = "pingAt";
29
const char* PAR_RATE_E            = "rate";
30
const char* PAR_SIZE_E            = "size";
31
const char* PAR_DSTAPNAME_E       = "dstApName";
32
const char* PAR_DSTAPINSTANCE_E   = "dstApInstance";
33
const char* PAR_DSTAENAME_E       = "dstAeName";
34
const char* PAR_DSTAEINSTANCE_E   = "dstAeInstance";
35
const char* PAR_AUTH_TYPE_E       = "authType";
36
const char* PAR_AUTH_NAME_E       = "authName";
37
const char* PAR_AUTH_OTHER_E      = "authOther";
38
const char* PAR_AUTH_PASS_E       = "authPassword";
39
const char* VAL_MODULEPATH_E      = "getFullPath()";
40
const char* VAL_CONNECTION_E      = "connect";
41
const char* VAL_RELEASE_E         = "release";
42
43
44
AEExtendedPing::AEExtendedPing() {
45
}
46
47
AEExtendedPing::~AEExtendedPing() {
48
49
}
50
51
void AEExtendedPing::prepareAllocateRequest() {
52
    //Schedule AllocateRequest
53
    cMessage* m1 = new cMessage(TIM_START_E);
54
    scheduleAt(startAt, m1);
55
}
56
57
void AEExtendedPing::preparePing() {
58
    //Schedule Data transfer
59
    for (int i = 0; i < rate && pingAt + i < stopAt; i++) {
60
        std::ostringstream ss;
61
        ss << MSG_PING_E << i;
62
        cMessage* m2 = new cMessage(ss.str().c_str());
63
        scheduleAt(pingAt + i, m2);
64
    }
65
}
66
67
void AEExtendedPing::prepareDeallocateRequest() {
68
    //Schedule DeallocateRequest
69
    cMessage* m3 = new cMessage(TIM_STOP_E);
70
    scheduleAt(stopAt, m3);
71
}
72
73
void AEExtendedPing::prepareConnectionRequest(){
74
    cMessage* m4 = new cMessage(TIM_CONNECT_E);
75
    scheduleAt(connectAt, m4);
76
}
77
78
void AEExtendedPing::initialize()
79
{
80
    //Init pointers
81
    initPointers();
82
    //Source info
83
    initNamingInfo();
84
    //Setup signals
85
    initSignalsAndListeners();
86
    //Init QoSRequirements
87
    initQoSRequiremets();
88
89
    //Timers
90
    startAt = simTime() + par(PAR_START_E);
91
    stopAt  = simTime() + par(PAR_STOP_E);
92
    connectAt = simTime() + par(PAR_CONNECT_E);
93
    pingAt  = simTime() + par(PAR_PING_E);
94
    rate    = par(PAR_RATE_E);
95
    size    = par(PAR_SIZE_E);
96
97
    //Authentication for application
98
    authType = par(PAR_AUTH_TYPE_E);
99
    authName = this->par(PAR_AUTH_NAME_E).stringValue();
100
    authPassword = this->par(PAR_AUTH_PASS_E).stringValue();
101
    authOther = this->par(PAR_AUTH_OTHER_E).stringValue();
102
103
    //Destination for flow
104
    dstApName     = this->par(PAR_DSTAPNAME_E).stringValue();
105
    dstApInstance = this->par(PAR_DSTAPINSTANCE_E).stringValue();
106
    dstAeName     = this->par(PAR_DSTAENAME_E).stringValue();
107
    dstAeInstance = this->par(PAR_DSTAEINSTANCE_E).stringValue();
108
109
    //Schedule AllocateRequest
110
    if (startAt > 0)
111
        prepareAllocateRequest();
112
    //Schedule Data transfer
113
    if (pingAt > 0)
114
        preparePing();
115
116
    //Schedule CACEConnectionRequest
117
    if (connectAt > 0)
118
        prepareConnectionRequest();
119
120
    //Schedule DeallocateRequest
121
    if (stopAt > 0)
122
        prepareDeallocateRequest();
123
124
    myPath = this->getFullPath();
125
126
    //Watchers
127
    WATCH_LIST(flows);
128
    WATCH(connectionState);
129
}
130
131
void AEExtendedPing::handleSelfMessage(cMessage *msg) {
132
    //EV << flows.back().info() << endl;
133
    if ( !strcmp(msg->getName(), TIM_START_E) ) {
134
        //FIXME: Vesely - last flow in a list?!
135
136
        //Flow
137
        APNamingInfo src = this->getApni();
138
        APNamingInfo dst = APNamingInfo( APN(this->dstApName), this->dstApInstance,
139
                                         this->dstAeName, this->dstAeInstance);
140
141
        Flow fl = Flow(src, dst);
142
        fl.setQosParameters(this->getQoSRequirements());
143
144
        //Insert it to the Flows ADT
145
        insertFlow(fl);
146
147
        sendAllocationRequest(&flows.back());
148
    }
149
    else if ( !strcmp(msg->getName(), TIM_CONNECT_E)){
150
        //Create MConnect message
151
        CDAP_M_Connect* connect = new CDAP_M_Connect(VAL_CONNECTION_E);
152
        authValue_t aValue;
153
        aValue.authName = authName;
154
        aValue.authPassword = authPassword;
155
        aValue.authOther = authOther;
156
157
        auth_t auth;
158
        auth.authType = authType;
159
        auth.authValue = aValue;
160
161
        connect->setAuth(auth);
162
        connect->setAbsSyntax(GPB);
163
        connect->setOpCode(M_CONNECT);
164
165
        //Send message
166
        sendData(&flows.back(), connect);
167
    }
168
    else if ( !strcmp(msg->getName(), TIM_STOP_E) ) {
169
        CDAP_M_Release* releaseRequest = new CDAP_M_Release(VAL_RELEASE_E);
170
        releaseRequest->setInvokeID(0);
171
172
        sendData(&flows.back(), releaseRequest);
173
174
    }
175
    else if ( strstr(msg->getName(), MSG_PING_E) ) {
176
        //Create PING messsage
177
        CDAP_M_Read* ping = new CDAP_M_Read(VAL_MODULEPATH_E);
178
        object_t obj;
179
        obj.objectName = VAL_MODULEPATH_E;
180
        obj.objectClass = "string";
181
        obj.objectInstance = -1;
182
        obj.objectVal = (cObject*)(&myPath);
183
        ping->setObject(obj);
184
        ping->setByteLength(size);
185
186
        //Send message
187
        sendData(&flows.back(), ping);
188
    }
189
    else
190
        EV << this->getFullPath() << " received unknown self-message " << msg->getName();
191
    delete(msg);
192
}
193
194
void AEExtendedPing::handleMessage(cMessage *msg)
195
{
196
    if ( msg->isSelfMessage() )
197
            this->handleSelfMessage(msg);
198
}
199
200
void AEExtendedPing::processMRead(CDAPMessage* msg) {
201
    CDAP_M_Read* msg1 = check_and_cast<CDAP_M_Read*>(msg);
202
203
    EV << "Received M_Read";
204
    object_t object = msg1->getObject();
205
    EV << " with object '" << object.objectClass << "'" << endl;
206
207
    if ( strstr(object.objectName.c_str(), VAL_MODULEPATH_E) ) {
208
        std::string* source = (std::string*)(object.objectVal);
209
        std::ostringstream os;
210
        os << "Ping requested by " <<  *source << endl;
211
        bubble(os.str().c_str());
212
        EV << os.str().c_str();
213
214
        //Create PING response
215
        CDAP_M_Read_R* pong = new CDAP_M_Read_R(VAL_MODULEPATH_E);
216
        object_t obj;
217
        obj.objectName = VAL_MODULEPATH_E;
218
        obj.objectClass = "string";
219
        obj.objectInstance = -1;
220
        obj.objectVal = (cObject*)(&myPath);
221
        pong->setObject(obj);
222
223
        sendData(&flows.back(), pong);
224
    }
225
}
226
227
void AEExtendedPing::processMReadR(CDAPMessage* msg) {
228
    CDAP_M_Read_R* msg1 = check_and_cast<CDAP_M_Read_R*>(msg);
229
230
    EV << "Received M_Read_R";
231
    object_t object = msg1->getObject();
232
    EV << " with object '" << object.objectClass << "'" << endl;
233
234
    if ( strstr(object.objectName.c_str(), VAL_MODULEPATH_E) ) {
235
        std::string* source = (std::string*)(object.objectVal);
236
        std::ostringstream os;
237
        os << "Ping replied by " <<  *source << endl;
238
        bubble(os.str().c_str());
239
        EV << os.str().c_str();
240
    }
241
}
242