Control the simulation end
Usually, the simulation will end automatically once there are no more events in the queue. Remember, this is also the reason why we always need at least one message to be scheduled during initialize for at least one node, otherwise the simulator will not start at all.
Another way of limiting the simulation time is to set it up in the omnetpp.ini file like this:
sim-time-limit = 10000s
Just put it at the beginning of the file (for better readability). This is very useful for simualtions that do not come naturally to an end, e.g our color changing code. Once the limit has been reached, the simulation will stop with a message saying "Simulation time limit reached - at t=10000s, event # "
In many situations, it is very useful to be able to finish the simulation from the code itself. For example, when in handlaeMessage() a message arrives that we did not really plan for or forgot to handle:
void Node::handleMessage(cMessage *msg)
{
...
// if the message comes from somewhere else
else {
if (msg->getKind() == CHANGE_TO_YELLOW) {
cDisplayString& dispStr = getDisplayString();
dispStr.parse("b=60,10,rect,yellow,black,2");
}
else {
EV << "UNEXPECTED EXTERNAL EVENT at time " << simTime() << "\n";
endSimulation();
}
delete msg;
}
...
This function can be used anytime in the code and it is a good idea to use when something weird happens, so that you are immediately aware of the problem. Like in the example above, do not forget to write in the event queue what happened and when, so that debugging gets easier later.