-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuestionA1.cc
45 lines (37 loc) · 1.03 KB
/
QuestionA1.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
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
class TxcA1 : public cSimpleModule {
protected:
virtual void forwardMessage(cMessage *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(TxcA1);
void TxcA1::initialize() {
if (getIndex() == 0) {
char msgname[20];
sprintf(msgname, "tic-%d", getIndex());
cMessage *msg = new cMessage(msgname);
scheduleAt(0.0, msg);
}
}
void TxcA1::handleMessage(cMessage *msg) {
if (getIndex() == 3) {
EV << "Message " << msg << " arrived.\n";
delete msg;
} else {
forwardMessage(msg);
}
}
void TxcA1::forwardMessage(cMessage *msg) {
int n = gateSize("port");
cGate *arrival = msg->getArrivalGate();
int k;
do {
k = intuniform(0, n-1);
} while (arrival != nullptr && n > 1 && k == arrival->getIndex());
EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "port$o", k);
}