omnet++ 中tictoc10-12学习笔记

tictoc10总结:加入了多模块的连接和消息发送

一、ned文件

simple Txc10
{
    parameters:
        @display("i=block/routing");
    gates:
        input in[];  
        output out[];
}

这里声明in[]和out[]为  门向量

network Tictoc10
{
    @display("bgb=981,541");
    submodules:
        tic[6]: Txc10 {
            @display("p=166,132");
        }
    connections://实现了0/1、1/2、1/4、3/4、4/5直接的连接。
        tic[0].out++ --> {  delay = 100ms; } --> tic[1].in++;
        tic[0].in++ <-- {  delay = 100ms; } <-- tic[1].out++;

        tic[1].out++ --> {  delay = 100ms; } --> tic[2].in++;
        tic[1].in++ <-- {  delay = 100ms; } <-- tic[2].out++;

        tic[1].out++ --> {  delay = 100ms; } --> tic[4].in++;
        tic[1].in++ <-- {  delay = 100ms; } <-- tic[4].out++;

        tic[3].out++ --> {  delay = 100ms; } --> tic[4].in++;
        tic[3].in++ <-- {  delay = 100ms; } <-- tic[4].out++;

        tic[4].out++ --> {  delay = 100ms; } --> tic[5].in++;
        tic[4].in++ <-- {  delay = 100ms; } <-- tic[5].out++;
}

实现了0/1、1/2、1/4、3/4、4/5直接的连接。

网络结构如图所示

 一个节点的输入门和输出门只能和另一个节点的输入门和输出门连接一次,他们之间的连接是一一对应的。一个节点和多个节点连接用门向量

这里的++我理解的是:第一次连接,第二次连接!

void Txc10::initialize()
{//getIndex获取当前0模块的索引,0就是tic[0]。语句就是判断当前模块是否为tic[0]模块
    if (getIndex() == 0) {//当前模块时都是tic【0】模块
        // Boot the process scheduling the initial message as a self-message.
        char msgname[20];
        sprintf(msgname, "tic-%d", getIndex());
        cMessage *msg = new cMessage(msgname);
        scheduleAt(0.0, msg);
    }
}
void Txc10::handleMessage(cMessage *msg)
{
    if (getIndex() == 3) {
        // Message arrived.
        EV << "Message " << msg << " arrived.\n";
        delete msg;
    }
    else {
        //我们需要转发消息。
        forwardMessage(msg);
    }
}
void Txc10::forwardMessage(cMessage *msg)
{
    // In this example, we just pick a random gate to send it on.
    // We draw a random number between 0 and the size of gate `out[]'.
    int n = gateSize("out");
        //Gatesize门的大小,是返回当前门的数量,其实就是表示这个模块连接了几个模块,或者说有几条线
    int k = intuniform(0, n-1);//产生一个随机数

    EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
    send(msg, "out", k);
    //k是门的索引,目的就是产生一个随机的门向量索引
    //再根据当前的门的名称以及输出门的索引把消息发送出去
}

tictoc11:总结:channel关键字的引入

这一节很简单

//之前的
 tic[0].out++ --> {  delay = 100ms; } --> tic[1].in++;
 tic[0].in++ <-- {  delay = 100ms; } <-- tic[1].out++;
//现在的
 tic[0].out++ --> Channel --> tic[1].in++;
 tic[0].in++ <-- Channel <-- tic[1].out++;
types:
    channel Channel extends ned.DelayChannel {
        delay = 100ms;
    }
    submodules:
        tic[6]: Txc11;
    connections:
        tic[0].out++ --> Channel --> tic[1].in++;
        tic[0].in++ <-- Channel <-- tic[1].out++;

一共有3中channel,一个是ned.idealchannel,一个是ned.delaychannel(本例用),一个是ned.dataratechannel

cc文件和tictoc是一样的

tictoc12总结:用inout替换了in门和out门

simple Txc12
{
    parameters:
        @display("i=block/routing");
    gates:
        inout gate[];  // declare two way connections
}

inout gete[ ]向量替换了之前的in门和out门

        tic[0].gate++ <--> Channel <--> tic[1].gate++;
        tic[1].gate++ <--> Channel <--> tic[2].gate++;
        tic[1].gate++ <--> Channel <--> tic[4].gate++;
        tic[3].gate++ <--> Channel <--> tic[4].gate++;
        tic[4].gate++ <--> Channel <--> tic[5].gate++;

仿真结果和上边两个是一样的,这么写大大简化了代码结构。

门有三种类型:

input:当前门是输入门,只能和输出门连接,只能接收消息。

output:当前门是输出门,只能和输入门连接,只能发送消息。

inout:既是输入门又是输出门,既能发送消息又能接收消息。

cc文件中这句代码看一下

 send(msg, "gate$o", k);

标识符表示是发出消息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值