码迷,mamicode.com
首页 > Web开发 > 详细

【OMNet++】tictoc示例二

时间:2015-05-11 13:02:29      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:omnet++

这个例子讲述从一个节点产生消息,随机发送。达到目的节点显示消息达到,结束仿真。ned如下

//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
package zhao;
simple Txc
{
    parameters:
        @display("i=block/routing");  //显示字符串指定Txc2模块的图标为路由器
    gates:
        input in[];
        output out[];    //声明Txc模块的输入门和输出门为向量
}
network Tictoc
{
    types:
        channel C extends ned.DelayChannel{delay=100ms;}
        //定义了一种ned,DelayChannel类型为c,延迟时间为100ms
    submodules:
        tic[6]:Txc; //指定组成此模型的简单模块为6个Tic模块
    connections:
       tic[0].out++-->C-->tic[1].in++;
       tic[0].in++<--C<--tic[1].out++;

       tic[1].out++-->C-->tic[2].in++;
       tic[1].in++<--C<--tic[2].out++;

       tic[1].out++-->C-->tic[4].in++;
       tic[1].in++<--C<--tic[4].out++;

       tic[3].out++-->C-->tic[4].in++;
       tic[3].in++<--C<--tic[4].out++;

       tic[4].out++-->C-->tic[5].in++;
       tic[4].in++<--C<--tic[5].out++;

}

技术分享
cc文件如下

#include<stdio.h>
#include<string.h>
#include<omnetpp.h>
class Txc:public cSimpleModule
{
    //定义成员函数
protected:
    virtual void forwardMessage(cMessage *msg);
    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
};
Define_Module(Txc);//宏注册Txc模块
void Txc::initialize()
{
   //初始化参数
    if (getIndex()==0)
    {
        //启动进程调度初始消息作为一个自我消息
        char msgname[20];
        sprintf(msgname,"tic-%d",getIndex());
        cMessage *msg=new cMessage(msgname);//生成消息msgname
        scheduleAt(0.0,msg);//在0.0时刻调度msg事件
    }
}
void Txc::handleMessage(cMessage *msg)
{
   //如果消息达到索引号为3的模块,则删除此消息,仿真停止,否则模块发送此消息到其他模块。
    if(getIndex()==3)
    {
        EV<<"Message"<<msg<<" arrived!"<<endl;//显示消息到达
    }
    else
    {
        forwardMessage(msg);//发送消息
    }
}
void Txc::forwardMessage(cMessage *msg)
{
    //找出每个模块的输出门的大小,随机通过一个输出门发送消息出去
    int n=gateSize("out");//获取门向量的个数
    int k=intuniform(0,n-1);//随机产生一个符合【0,n-1】均匀分布的整数
    EV<<"Forwarding message"<<msg<<"on port out["<<k<<"]"<<endl;
    send(msg,"out",k); //从第k个门的out输出消息

}

技术分享

【OMNet++】tictoc示例二

标签:omnet++

原文地址:http://blog.csdn.net/u012503639/article/details/45642005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!