omnet++ 示例13练习题
Exercise: 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. 。
原有例子中存在消息在两个节点间来回“bouncing”(跳),使得消息到达目的节点的路由过程低效。示例要求改进原有路由策略,即避免将消息传给发送者。这里有有两个注意的地方:
- 会出现getSize=1的情况,即消息所在的节点只有一个连接节点,那么此时消息只能原路返回
- 示例中提到的self-message情况,此时的arrivalgate=NULL
forwardMessage函数的代码如下:
void Txc1::forwardMessage(cMessage *msg)
{
int n = gateSize("out");
EV << "The number of gates:" << n << "\n";
int k = intuniform(, n-);
cGate *arrivalGate = msg->getArrivalGate();
if((arrivalGate == NULL)||(n==))
{
EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}
else{
int arrivalGateIndex = arrivalGate->getIndex();
EV << "arrivalGateIndex: " << arrivalGateIndex << ".\n";
while(k == arrivalGateIndex)
k = intuniform(, n-);
EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}
}
结果图: