Parent: [6ccca6] (diff)

Child: [b323f7] (diff)

Download this file

CACE.cc    279 lines (227 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
#include "CACE.h"
Define_Module(CACE);
void CACE::initialize()
{
initPointers();
initSignalsAndListeners();
potentialConTimer = 0;
currentConRetries = 0;
maxConRetries = 1;
}
void CACE::handleMessage(cMessage *msg)
{
//check message and process it to CACEStateMachine
if (dynamic_cast<CDAPMessage*>(msg)) {
CDAPMessage* cmsg = check_and_cast<CDAPMessage*>(msg);
CACEStateMachine(cmsg);
}
}
void CACE::initPointers(){
//TODO potentially rib daemon for setting status
ae = dynamic_cast<AE*>(this->getParentModule()->getParentModule()->getSubmodule(MOD_IAE));
}
void CACE::initSignalsAndListeners(){
cModule* catcher = this->getParentModule()->getParentModule();
cModule* catcher2 = this->getParentModule()->getSubmodule(MOD_CDAPAUTH);
//Signals emitted by this module
sigCACEConResPosi = registerSignal(SIG_CACE_ConnectionResponsePositive);
sigCACEConResNega = registerSignal(SIG_CACE_ConnectionResponseNegative);
sigCACERelRes = registerSignal(SIG_CACE_ReleaseResponse);
sigCACEAuthReq = registerSignal(SIG_CACE_AuthenticationRequest);
//Signals that this module is processing
//connection request from AE
lisCACEConReq = new LisCACEConReq(this);
catcher->subscribe(SIG_AE_ConnectionRequest, lisCACEConReq);
//release request from AE
lisCACERelReq = new LisCACERelReq(this);
catcher->subscribe(SIG_AE_ReleaseRequest, lisCACERelReq);
//authentication validation response from Auth
lisCACEAuthRes = new LisCACEAuthRes(this);
catcher2->subscribe(SIG_Auth_AuthenticationResponse, lisCACEAuthRes);
}
void CACE::changeConnectionState(CDAPConnectionState connectionState){
ae->changeConStatus(connectionState);
}
void CACE::treatAuthRes(CDAPMessage *cmsg) {
CDAP_M_Connect_R* cmsgCR = dynamic_cast<CDAP_M_Connect_R*>(cmsg);
take(cmsg);
if (cmsgCR->getResult().resultValue == R_SUCCESS) {
processMConnectResPosi(cmsgCR);
}
else {
processMConnectResNega(cmsgCR);
}
}
void CACE::CACEStateMachine(CDAPMessage *cmsg){
//M_Connect
if (dynamic_cast<CDAP_M_Connect*>(cmsg)) {
if (ae->getConStatus() == CONNECTION_PENDING) {
//TODO: potentialConnectionTimer
CDAP_M_Connect* msgC = check_and_cast<CDAP_M_Connect*>(cmsg);
//validate M_Connect message
if (msgC->getAbsSyntax() == GPB &&
msgC->getOpCode() == M_CONNECT) {
//check connection retries
if (currentConRetries <= maxConRetries) {
changeConnectionState(AUTHENTICATING);
//emit signal to auth module
signalizeAuthenticationRequest(msgC);
}
//unautorized number of retries
else {
//TODO: probably immediate deallocation
processMRelease();
}
}
//not valid M_Connect message
else {
//TODO: probably immediate deallocation
processMRelease();
}
}
//message came in bad connection state
else {
//TODO: probably immediate deallocation
processMRelease();
}
}
//M_Connect_R
else if (dynamic_cast<CDAP_M_Connect_R*>(cmsg)){
if (ae->getConStatus() == AUTHENTICATING) {
CDAP_M_Connect_R* msgCR = check_and_cast<CDAP_M_Connect_R*>(cmsg);
if (msgCR->getResult().resultValue == 0) {//SUCCESS
signalizeConnResponsePositive(cmsg);
}
else {
signalizeConnResponseNegative(cmsg);
}
}
else {
//TODO: probably immediate deallocation
processMRelease();
}
}
//M_Release
else if (dynamic_cast<CDAP_M_Release*>(cmsg)){
if (!dynamic_cast<CDAP_M_Release*>(cmsg)->getInvokeID()){
changeConnectionState(NIL);
//deallocate connection
Flows p = ae->getFlows();
ae->sendDeallocationRequest(&p.back());
}
else {
processMReleaseR(cmsg);
changeConnectionState(NIL);
}
}
//M_Release_R
else if (dynamic_cast<CDAP_M_Release_R*>(cmsg) != NULL){
signalizeReleaseResponse(cmsg);
}
else {
//TODO: probably immediate deallocation
processMRelease();
}
}
void CACE::processMConnect(CDAPMessage *cmsg){
Enter_Method("processMConnect()");
//set CDAPConnection state
changeConnectionState(AUTHENTICATING);
take(check_and_cast<cOwnedObject*>(cmsg) );
//set handle for messages
handle = cmsg->getHandle();
//Send message
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, cmsg->getHandle());
send(cmsg, out);
}
void CACE::processMConnectResPosi(CDAPMessage *cmsg){
Enter_Method("processMConnectResPosi()");
take(cmsg);
//set CDAPConnection state
changeConnectionState(ESTABLISHED);
currentConRetries = 0;
handle = cmsg->getHandle();
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, cmsg->getHandle());
send(cmsg, out);
}
void CACE::processMConnectResNega(CDAPMessage *cmsg){
Enter_Method("processMConnectResNega()");
changeConnectionState(CONNECTION_PENDING);
//increment connection retries
currentConRetries++;
take(cmsg);
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, cmsg->getHandle());
send(cmsg, out);
}
void CACE::processMRelease(){
Enter_Method("processMRelease()");
//set CDAPConnection State
changeConnectionState(NIL);
CDAP_M_Release* msg = new CDAP_M_Release("release");
msg->setInvokeID(0);
msg->setHandle(handle);
//set message type
msg->setOpCode(M_RELEASE);
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, msg->getHandle());
send(msg, out);
}
void CACE::processMRelease(CDAPMessage *cmsg){
Enter_Method("processMRelease()");
take(check_and_cast<cOwnedObject*>(cmsg));
CDAP_M_Release* msg = dynamic_cast<CDAP_M_Release*>(cmsg);
if (msg->getInvokeID()) {
//setCDAPConnection state
changeConnectionState(RELEASING);
}
else {
//set CDAPConnection state
changeConnectionState(NIL);
}
//set message type
msg->setOpCode(M_RELEASE);
//Send message
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, cmsg->getHandle());
send(cmsg, out);
}
void CACE::processMReleaseR(CDAPMessage *cmsg){
Enter_Method("processMRelease()");
//TODO:potentialy "after 2MPL+A invokes Deallocate"
CDAP_M_Release_R* releaseResponse = new CDAP_M_Release_R("releaseResponse");
result_t result;
result.resultReason = "success";
result.resultValue = 0;
releaseResponse->setResult(result);
releaseResponse->setHandle(handle);
//set message type
releaseResponse->setOpCode(M_RELEASE_R);
cGate* out = gateHalf(GATE_SPLITIO, cGate::OUTPUT, releaseResponse->getHandle());
send(releaseResponse, out);
}
void CACE::sendMessage(CDAPMessage *cmsg){
}
void CACE::signalizeConnResponseNegative(CDAPMessage* cmsg){
emit(sigCACEConResNega, cmsg);
}
void CACE::signalizeConnResponsePositive(CDAPMessage* cmsg){
emit(sigCACEConResPosi, cmsg);
}
void CACE::signalizeReleaseResponse(CDAPMessage* cmsg){
emit(sigCACERelRes, cmsg);
}
void CACE::signalizeAuthenticationRequest(CDAPMessage* cmsg){
emit(sigCACEAuthReq, cmsg);
}