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);
}
}
結果圖:
