📄 router.cc
字号:
void Router::processNormalDataPackets(samplePacket *packet, int i)
{
// if routing table is yet not constructed then simply
// ignore the packet
if(topologyDiscovered)
{
// if this node is the destination then give
// packet to the sink
int destination = packet->getDestAddress();
if( destination == myAddress)
{
send(packet, "toDataSink");
}
else
{
receiveProcessStorePacket( packet,i );
}
}
else
{
delete packet;
}
}
void Router::receiveProcessStorePacket(cMessage *packet, int i)
{
int type = packet->kind();
if(debug)
{
ev << "Ant Received at Router: " << myAddress << packet->name() << endl;
ev << "Message Kind is: " << type << endl;
}
// increment the counter
numPacketsToSend[i]++;
//if send queue is already full then we need to drop the packet
if(sendNormalQueue[i].length() >= queueMaxLen-5)
{
// to ensure that we could queue the echo message
// at time of high load, hence we would leave
// space for 5 echo packets
if(type == NETLAYER_DATA_PACKET)
{
if(debug)
{
ev << "Queue full, DROPPED !" << endl;
}
delete packet;
numPacketsToSendDropped[i]++;
if(logResults)
{
queueDrops[i].record(numPacketsToSendDropped[i]++);
}
sPtr->incrTotalBitsLost();
return;
}
}
// we mark enqueue time to find out how much time the packet spent in
// queue: this marks the packet delay experienced at a router
packet->setTimestamp();
//insert packet into buffer
if(type == NETLAYER_DATA_PACKET)
{
sendNormalQueue[i].insert((samplePacket *) packet);
}
else if( type == NETLAYER_FORWARD_ANT)
{
sendNormalQueue[i].insert((Ant *) packet);
}
else
{
throw new cException("Unknown Message in receiveProcess %d", type);
}
if(logResults)
{
queueLenPackets[i].record(sendNormalQueue[i].length());
}
if(debug)
{
ev << "Enqueued, queue length=" << sendNormalQueue[i].length() <<endl;
}
}
simtime_t Router::sendPacketInQueue(cMessage *msg, int i)
{
// start transmitting the network packet
simtime_t txTime = 0.0;
int type = msg->kind();
if( type == NETLAYER_DATA_PACKET)
{
samplePacket *packet = (samplePacket *) msg;
// inspect destination field
int destination = packet->getDestAddress();
int nextHopAddress = neighborAtIndex[i];
if(logResults)
{
queueLenPackets[i].record(sendNormalQueue[i].length());
// write queueing time statistics
queueingTime[i].record(simTime() - packet->timestamp());
queueDelayPackets[i].collect(simTime() - packet->timestamp());
}
sPtr->insertQueueDelay(simTime() - packet->timestamp());
// if it is a packet destined for not this station
// index into routing table, find out next hop
// and find output port where it could be sent
int portID = neighborPortID(nextHopAddress,true);
if( portID != -1)
{
packet->setHops(packet->getHops() + 1);
if(debug)
{
ev << "Next Hop Address is" << nextHopAddress <<
" for packet " << packet->name() << "at port"
<< portID;
}
send(packet, portID);
simtime_t transmissionStartTime = simTime();
// schedule sendNomral Message in case
// queue is having more messages to be sent
// wait time is transmission time
txTime = packet->length()/(double) dataRate;
if(debug)
{
ev << "Transmission started at " <<
simtimeToStr(transmissionStartTime);
}
return txTime;
}
else
{
if(debug)
{
ev << "Data Packet Received in" << tcb.state << endl;
ev << "Destion: " << destination << "not known" << endl;
ev << "Could Not Forward: No Destination Entry in Routing Table" << endl;
ev << "Ignoring" << endl;
}
delete packet;
txTime = 0.0000001;
return txTime;
}
} // end type if
else if( type = NETLAYER_FORWARD_ANT)
{
Ant *ant = (Ant *) msg;
// inspect destination field
int destination = ant->getDestNode();
int nextHopAddress = neighborAtIndex[i];
if(logResults)
{
queueLenPackets[i].record(sendNormalQueue[i].length());
// write queueing time statistics
queueingTime[i].record(simTime() - ant->timestamp());
queueDelayPackets[i].collect(simTime() - ant->timestamp());
}
sPtr->insertQueueDelay(simTime() - ant->timestamp());
// if it is a ant destined for not this station
// index into routing table, find out next hop
// and find output port where it could be sent
int portID = neighborPortID(nextHopAddress,true);
if( portID != -1)
{
if(debug)
{
ev << "Next Hop Address is" << nextHopAddress <<
" for ant " << ant->name() << "at port"
<< portID;
}
ant->setSourceModule( (int) ROUTER);
send(ant, portID);
simtime_t transmissionStartTime = simTime();
// schedule sendNomral Message in case
// queue is having more messages to be sent
// wait time is transmission time
txTime = ant->length()/(double) dataRate;
if(debug)
{
ev << "Transmission started at " <<
simtimeToStr(transmissionStartTime);
}
return txTime;
}
else
{
if(debug)
{
ev << "Ant Received in" << tcb.state << endl;
ev << "Destion: " << destination << "not known" << endl;
ev << "Could Not Forward: No Destination Entry in Routing Table" << endl;
ev << "Ignoring" << endl;
}
delete ant;
txTime = 0.0000001;
return txTime;
}
}
else
{
throw new cException("Unknown Message in sendPacketQueue() %d", type);
}
}
void Router::handleBackwardAnt(Ant* msg)
{
findSourceForAnt( msg );
if( tcb.source == ROUTER)
{
send( msg, "toAntNest");
}
else if( tcb.source == ANT_NEST)
{
int neighbor = msg->getNeighborChosen();
int index = findIndexForNeighbor(neighbor);
handleBackwardAntMessageQueue(msg, index);
}
else
{
throw new cException("Unknown source for Ant in Router %d",myAddress);
}
}
void Router::handleBackwardAntMessageQueue(cMessage *msg, int index)
{
if(msg == sendBackwardAnt[index])
{
if( sendPriorityQueue[index].empty() )
{
bAntServiced[index] = NULL;
}
else
{
bAntServiced[index] = (Ant *) sendPriorityQueue[index].getTail();
simtime_t txTime = sendBackwardAntInPriotiyQueue(bAntServiced[index], index);
int *ptr = new int;
*ptr = index;
sendBackwardAnt[index]->setContextPointer( (void *) ptr);
scheduleAt(simTime() + txTime, sendBackwardAnt[index]);
}
}
else if( !bAntServiced[index])
{
bAntServiced[index] = (Ant *) msg;
simtime_t txTime = processBackwardAntWhenQueueIsEmpty( (Ant *) msg, index);
int *ptr = new int;
*ptr = index;
sendBackwardAnt[index]->setContextPointer( (void *) ptr);
scheduleAt(simTime() + txTime, sendBackwardAnt[index]);
}
else
{
processBackwardAnts( (Ant *) msg,index);
}
}
void Router::processBackwardAnts(Ant *msg, int i)
{
if(debug)
{
ev << "Backward Ant Received at Router: " << myAddress << msg->name() << endl;
}
// we mark enqueue time to find out how much time the msg spent in
// queue: this marks the msg delay experienced at a router
msg->setTimestamp();
//insert msg into buffer
sendPriorityQueue[i].insert((Ant *) msg);
if(debug)
{
ev << "Enqueued, queue length=" << sendPriorityQueue[i].length() <<endl;
}
}
simtime_t Router::processBackwardAntWhenQueueIsEmpty(Ant *msg, int i)
{
simtime_t txTime = sendBackwardAntInPriotiyQueue(msg,i);
return txTime;
}
simtime_t Router::sendBackwardAntInPriotiyQueue(Ant *msg, int i)
{
// start transmitting the network packet
simtime_t txTime = 0.0;
int destination = msg->getDestNode();
int nextHopAddress = neighborAtIndex[i];
// if it is an ant destined for not this station
// index into routing table, find out next hop
// and find output port where it could be sent
int portID = neighborPortID(nextHopAddress,true);
if( portID != -1)
{
if(debug)
{
ev << "Next Hop Address is" << nextHopAddress <<
" for ant " << msg->name() << "at port"
<< portID;
}
msg->setSourceModule((int) ROUTER);
send(msg, portID);
simtime_t transmissionStartTime = simTime();
// schedule sendNomral Message in case
// queue is having more messages to be sent
// wait time is transmission time
txTime = msg->length()/(double) dataRate;
if(debug)
{
ev << "Transmission started at " <<
simtimeToStr(transmissionStartTime);
}
return txTime;
}
else
{
if(debug)
{
ev << "Ant Received in" << tcb.state << endl;
ev << "Destination: " << destination << "not known" << endl;
ev << "Could Not Forward: No Destination Entry in Routing Table" << endl;
ev << "Ignoring" << endl;
}
delete msg;
txTime = 0.0000001;
return txTime;
}
}
void Router::initializeQueues()
{
// send this link state packet to all neighbors
// first find out neighbors from the graph
vector<int>::iterator iter;
numNeighbors = topology.size();
int nodes = getNumNodes();
sendNormalQueue = new cQueue[numNeighbors];
sendPriorityQueue = new cQueue[numNeighbors];
sendNormalAndForwardAnt = new cMessage*[numNeighbors];
sendBackwardAnt = new cMessage*[numNeighbors];
msgServiced = new cMessage*[numNeighbors];
bAntServiced = new Ant*[numNeighbors];
if(logResults)
{
queueLenPackets = new cOutVector[numNeighbors];
queueingTime = new cOutVector[numNeighbors];
queueDrops = new cOutVector[numNeighbors];
queueDelayPackets = new cStdDev[numNeighbors];
}
// Different variables that help in processing packets
numPacketsToSend = new double[numNeighbors];
numPacketsToSendDropped = new double[numNeighbors];
rTable = new routingTable(nodes, numNeighbors);
neighborAtIndex = new int[numNeighbors];
int i = 0;
char name[100];
for(iter = topology.begin(); iter != topology.end(); ++iter)
{
int neighborAddress = *iter; // neighbor
indexFromNeighbor[neighborAddress] = i;
neighborAtIndex[i] = neighborAddress;
sendNormalAndForwardAnt[i] = new cMessage;
sendNormalAndForwardAnt[i]->setKind(SEND_NORMAL_PACKET_COM);
sendNormalAndForwardAnt[i]->setName("sendNormalMessage");
sendBackwardAnt[i] = new cMessage;
sendBackwardAnt[i]->setKind(SEND_BACKWARD_ANT_COM);
sendBackwardAnt[i]->setName("sendBackwardAntMessage");
msgServiced[i] = NULL;
bAntServiced[i] = NULL;
if(logResults)
{
sprintf(name,"Queue Length (Forward Packets) for Neighbor %d at Node %d",
neighborAddress, myAddress);
queueLenPackets[i].setName(name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -