Download this file

IRM.cc    253 lines (202 with data), 7.8 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
//
// 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 "IRM.h"
Define_Module(IRM);
IRM::IRM() {
}
IRM::~IRM() {
}
void IRM::initPointers() {
ConTable = ModuleAccess<ConnectionTable>(MOD_CONNTABLE).get();
DifAllocator = ModuleAccess<DA>(MOD_DA).get();
}
void IRM::initialize() {
initPointers();
initSignalsAndListeners();
}
void IRM::handleMessage(cMessage* msg) {
if (!msg->isSelfMessage()) {
//Find output gate based on input
cGate* g = ConTable->findOutputGate(msg->getArrivalGate());
//Send out if gate exist
if (g)
send(msg, g);
else {
EV << "Received message but destination gate is not in the ConnectionTable!" << endl;
}
}
//Process self-message
else {
}
}
void IRM::initSignalsAndListeners() {
cModule* catcher = this->getParentModule()->getParentModule();
//Signals that this module emits
sigIRMAllocReq = registerSignal(SIG_IRM_AllocateRequest);
sigIRMDeallocReq = registerSignal(SIG_IRM_DeallocateRequest);
// Allocate Request from App
this->lisAllocReq = new LisIRMAllocReq(this);
catcher->subscribe(SIG_AE_AllocateRequest, this->lisAllocReq);
// Deallocate Request from App
this->lisDeallocReq = new LisIRMDeallocReq(this);
catcher->subscribe(SIG_AE_DeallocateRequest, this->lisDeallocReq);
}
bool IRM::createBindings(Flow* flow) {
Enter_Method("createBindings()");
EV << "Attempts to create bindings and bind registration of gates"<< endl;
//Retrieve IPC process with allocated flow and prepared bindings
ConnectionTableEntry* cte = ConTable->findEntryByFlow(flow);
//if (!cte) {
// EV << "======================" << endl << flow->info() << endl;
// return false;
//}
cModule* Ipc = cte->getIpc();
cModule* Ap = this->getParentModule();
//Decide portId
int portId = flow->getSrcPortId();
/*
if ( DifAllocator->isAppLocal(flow->getDstApni().getApn()) )
portId = flow->getDstPortId();
else if ( DifAllocator->isAppLocal(flow->getSrcApni().getApn()) )
portId
else
throw("Binding to inconsistant PortId occured!");
*/
// Retrieve IPC gates
std::ostringstream nam1;
nam1 << GATE_NORTHIO_ << portId;
cGate* g1i = Ipc->gateHalf(nam1.str().c_str(), cGate::INPUT);
cGate* g1o = Ipc->gateHalf(nam1.str().c_str(), cGate::OUTPUT);
// Add AP gates
std::ostringstream nam2;
nam2 << GATE_SOUTHIO_ << portId;
Ap->addGate(nam2.str().c_str(), cGate::INOUT, false);
cGate* g2i = Ap->gateHalf(nam2.str().c_str(), cGate::INPUT);
cGate* g2o = Ap->gateHalf(nam2.str().c_str(), cGate::OUTPUT);
// Add IRM gates
this->addGate(nam2.str().c_str(), cGate::INOUT, false);
cGate* g3i = this->gateHalf(nam2.str().c_str(), cGate::INPUT);
cGate* g3o = this->gateHalf(nam2.str().c_str(), cGate::OUTPUT);
//TODO: Status check
// Connect gates together
g1o->connectTo(g2i);
g2i->connectTo(g3i);
g3o->connectTo(g2o);
g2o->connectTo(g1i);
//Set south-half of the routing in ConnectionTable
bool status = ConTable->setSouthGates(flow, g3i, g3o);
return status;
}
bool IRM::receiveAllocationRequestFromAe(Flow* flow) {
Enter_Method("receiveAllocateRequest()");
EV << this->getFullPath() << " received Allocation Request" << endl;
//Command target FA to allocate flow
FABase* fab = ConTable->getFa(flow);
bool status = false;
if (fab) {
//signalizeAllocateRequest(fl);
status = fab->receiveAllocateRequest(flow);
//If AllocationRequest NOT ended by creating connections
if (!status)
EV << "Flow not allocated!\n" << flow << endl;
}
else
EV << "FA could not be found in ConnectionTable!" << endl;
return status;
}
bool IRM::receiveDeallocationRequestFromAe(Flow* flow) {
Enter_Method("receiveDeallocateRequest()");
EV << this->getFullPath() << " received DeallocationRequest" << endl;
//Command target FA to allocate flow
FABase* fab = ConTable->getFa(flow);
bool status = false;
if (fab) {
//signalizeDeallocateRequest(fl);
status = fab->receiveDeallocateRequest(flow);
}
else
EV << "FA could not be found in ConnectionTable!" << endl;
return status;
}
void IRM::signalizeAllocateRequest(Flow* flow) {
//EV << "!!!!VYemitovano" << endl;
//EV << "Emits AllocReq Flow = " << flow->getSrcApni() << "_" << flow->getDstApni() << endl;
emit(sigIRMAllocReq, flow);
}
void IRM::newFlow(Flow* flow) {
Enter_Method("newFlow()");
//Create a new record in ConnectionTable
ConTable->insertNew(flow);
//Ask DA which IPC to use to reach dst App
const Address* ad = DifAllocator->resolveApnToBestAddress(flow->getDstApni().getApn());
if (ad == NULL) {
EV << "DifAllocator returned NULL for resolving " << flow->getDstApni().getApn() << endl;
return;
}
Address addr = *ad;
//TODO: Vesely - New IPC must be enrolled or DIF created
if (!DifAllocator->isDifLocal(addr.getDifName())) {
EV << "Local CS does not have any IPC in DIF " << addr.getDifName() << endl;
return;
}
//Retrieve DIF's local IPC member
cModule* targetIpc = DifAllocator->getDifMember(addr.getDifName());
//Store info into ConnectionTable
ConTable->setFa(flow, DifAllocator->findFaInsideIpc(targetIpc));
}
void IRM::signalizeDeallocateRequest(Flow* flow) {
emit(sigIRMDeallocReq, flow);
}
ConnectionTable* IRM::getConTable() const {
return ConTable;
}
bool IRM::receiveAllocationResponsePositiveFromIpc(Flow* flow) {
Enter_Method("allocationResponsePositive()");
bool status = createBindings(flow);
return status;
}
bool IRM::deleteBindings(Flow* flow) {
Enter_Method("deleteBindings()");
EV << "Attempts to delete bindings"<< endl;
//Retrieve IPC process with allocated flow and prepared bindings
ConnectionTableEntry* cte = ConTable->findEntryByFlow(flow);
cModule* Ipc = cte->getIpc();
cModule* Ap = this->getParentModule();
//Decide portId
int portId = flow->getSrcPortId();
// Retrieve IPC gates
std::ostringstream nam1;
nam1 << GATE_NORTHIO_ << portId;
cGate* g1i = Ipc->gateHalf(nam1.str().c_str(), cGate::INPUT);
cGate* g1o = Ipc->gateHalf(nam1.str().c_str(), cGate::OUTPUT);
// Add AP gates
std::ostringstream nam2;
nam2 << GATE_SOUTHIO_ << portId;
cGate* g2i = Ap->gateHalf(nam2.str().c_str(), cGate::INPUT);
cGate* g2o = Ap->gateHalf(nam2.str().c_str(), cGate::OUTPUT);
// Add IRM gates
cGate* g3i = this->gateHalf(nam2.str().c_str(), cGate::INPUT);
cGate* g3o = this->gateHalf(nam2.str().c_str(), cGate::OUTPUT);
// Connect gates together
g1o->disconnect();
g2i->disconnect();
g3i->disconnect();
g3o->disconnect();
g2o->disconnect();
g1i->disconnect();
return !g1o->isConnected() && !g1i->isConnected()
&& !g2o->isConnected() && !g2i->isConnected()
&& !g3o->isConnected() && !g3i->isConnected();
}