You'll notice that this simple "routing" is not very efficient: often the packet keeps bouncing between two nodes for a while before it is sent to a different direction. This can be improved somewhat if nodes don't send the packet back to the sender. Implement this.
Hints: cMessage::getArrivalGate(), cGate::getIndex().
Note that if the message didn't arrive via a gate but was a self-message, then getArrivalGate() returns NULL.
Source: http://www.omnetpp.org/doc/omnetpp/tictoc-tutorial/part3.html
TicToc10 Solution:
void Txc10::forwardMessage(cMessage *msg)
{
// In this example, we just pick a random gate (gate outs size) to send it on.
// We draw a random number between 0 and the size of gate `out[]'.
int n = gateSize("out");
int k = intuniform(0,n-1);
cGate *arrivalGate = msg->getArrivalGate();
//int arrivalGateIndex = arrivalGate->getIndex(); // null pointer exception runtime error
// because the first message sent by scheduleAt is a self-message
if (arrivalGate == NULL) //self-message, do what?
{
// In this case, you may want to actually do something once the self
// message "fires". Otherwise, as you've seen, the simulation ends there.
// In short, you could just send the self message.
EV << "Forwarding self message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}
else if (arrivalGate != NULL) // if it is not a self-message, get arrivalGateIndex
{
int arrivalGateIndex = arrivalGate->getIndex();
if (n >= 2) //if out gates are more than 1, easy
//this line is needed to avoid infinite loop because you can send back to the sender
//if there is only one output gate
{
while (arrivalGateIndex == k)
{
k = intuniform(0,n-1);
}
}
EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}
}
1 comment:
Thanks
Post a Comment