-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeServerProcess.cc
88 lines (67 loc) · 2.3 KB
/
EdgeServerProcess.cc
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
#include "DynaPacket_m.h"
#include "MQTTMessage_m.h"
using namespace omnetpp;
#define STACKSIZE 16384
/**
* Dynamically launched process in the server; see NED file for more info
*/
class EdgeServerProcess : public cSimpleModule
{
public:
EdgeServerProcess() : cSimpleModule(STACKSIZE) {}
virtual void activity() override;
};
Define_Module(EdgeServerProcess);
void EdgeServerProcess::activity()
{
// retrieve parameters
cPar& processingTime = getParentModule()->par("processingTime");
cGate *serverOutGate = getParentModule()->gate("port$o");
int clientAddr = 0, ownAddr = 0;
WATCH(clientAddr);
WATCH(ownAddr);
DynaPacket *pk;
DynaDataPacket *datapk;
// receive the CONN_REQ we were created to handle
EV << "Started, waiting for DYNA_CONN_REQ\n";
pk = check_and_cast<DynaPacket *>(receive());
clientAddr = pk->getSrcAddress();
ownAddr = pk->getDestAddress();
// respond to CONN_REQ by CONN_ACK
EV << "client is addr=" << clientAddr << ", sending DYNA_CONN_ACK\n";
pk->setName("DYNA_CONN_ACK");
pk->setKind(DYNA_CONN_ACK);
pk->setSrcAddress(ownAddr);
pk->setDestAddress(clientAddr);
pk->setServerProcId(getId());
sendDirect(pk, serverOutGate);
// process data packets until DISC_REQ comes
for ( ; ; ) {
EV << "waiting for DATA(query) (or DYNA_DISC_REQ)\n";
pk = check_and_cast<DynaPacket *>(receive());
int type = pk->getKind();
if (type == DYNA_DISC_REQ)
break;
if (type != DYNA_DATA)
throw cRuntimeError("protocol error!");
datapk = (DynaDataPacket *)pk;
EV << "got DATA(query), processing...\n";
wait((double)processingTime);
EV << "sending DATA(result)\n";
datapk->setName("DATA(result)");
datapk->setKind(DYNA_DATA);
datapk->setSrcAddress(ownAddr);
datapk->setDestAddress(clientAddr);
datapk->setPayload("result");
sendDirect(datapk, serverOutGate);
}
// connection teardown in response to DISC_REQ
EV << "got DYNA_DISC_REQ, sending DYNA_DISC_ACK\n";
pk->setName("DYNA_DISC_ACK");
pk->setKind(DYNA_DISC_ACK);
pk->setSrcAddress(ownAddr);
pk->setDestAddress(clientAddr);
sendDirect(pk, serverOutGate);
EV << "exiting\n";
deleteModule();
}