概述
通过阅读Txc1.cc的文件,.cc文件是用来描述ned中模块的行为,在该代码中重新定义了来自cSimpleModule中的两个方法initialize()和handleMessage()
Txc1::initialize()初始化是仿真的起点,为了启动从tic-toc-tic-toc的仿真过程,选择tic创建一个消息,并通过它的门将消息传输出去,“tictocMsg”是一个任意字符串,它将是消息对象的名称。
Txc1::handleMessage():每当与消息到来时处理的操作。在这里只通过门的out端去做出响应。因为tic
和toc
都做了同样的事情,所以消息会在两者之间跳变。
注意这个行为过程将会在Txc1类中的对象持续进行,因此这种方式是针对同一类型的操作。
代码
void Txc1::initialize()
{
// Initialize is called at the beginning of the simulation.
// To bootstrap the tic-toc-tic-toc process, one of the modules needs
// to send the first message. Let this be `tic'.
// Am I Tic or Toc?
if (strcmp("tic", getName()) == 0) {
// create and send first message on gate "out". "tictocMsg" is an
// arbitrary string which will be the name of the message object.
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
void Txc1::handleMessage(cMessage *msg)
{
// The handleMessage() method is called whenever a message arrives
// at the module. Here, we just send it to the other module, through
// gate `out'. Because both `tic' and `toc' does the same, the message
// will bounce between the two.
send(msg, "out"); // send out the message
}
总结
omnet中的消息(入包、帧、作业等)和事件(定时器、超时)都有cMessage对象或其子类表示。在你发送或调度它们之后,它们将被模拟内核保存在“计划事件”或“未来事件”列表中,直到它们的时间到来,并通过handleMessage()传递给模块。
请注意,这个模拟中没有设置停止条件:它会永远继续下去。您将能够从GUI停止它。(您也可以在配置文件中指定模拟时间限制或CPU时间限制,