omnet++ 正方形 传递100次消息 实验代码

基于第一个创建的 工程
.ned 代码

//
// This file is part of an OMNeT++/OMNEST simulation example.
//
// Copyright (C) 2003 Ahmet Sekercioglu
// Copyright (C) 2003-2015 Andras Varga
//
// This file is distributed WITHOUT ANY WARRANTY. See the file
// `license' for details on this and other legal matters.
//


simple Txc1
{
    gates:
        input in;
        output out;
}

//
// Two instances (tic and toc) of Txc1 connected both ways.
// Tic and toc will pass messages to one another.
//
network Tictoc1
{
    @display("bgb=165,171");
    submodules:
        t1: Txc1 {
            @display("p=33,117");
        }
        t2: Txc1 {
            @display("p=33,42");
        }
        t3: Txc1 {
            @display("p=123,42");
        }
        t4: Txc1 {
            @display("p=123,117");
        }

    connections:
        t1.out --> {  delay = 100ms; } --> t2.in;
        t2.out --> {  delay = 100ms; } --> t3.in;
        t3.out --> {  delay = 100ms; } --> t4.in;
        t4.out --> {  delay = 100ms; } --> t1.in;
        
}

.cc 代码

#include <string.h>
#include <omnetpp.h>

using namespace omnetpp;

int count;
/**
 * Derive the Txc1 class from cSimpleModule. In the Tictoc1 network,
 * both the `tic' and `toc' modules are Txc1 objects, created by OMNeT++
 * at the beginning of the simulation.
 */
class Txc1 : public cSimpleModule
{
  protected:
    // The following redefined virtual function holds the algorithm.
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;
  private:
    int t1_count;
    int t2_count;
    int t3_count;
    int t4_count;
};

// The module class needs to be registered with OMNeT++
Define_Module(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("t1", 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("t1cMsg");
        send(msg, "out");
    }
    t1_count = 0;
    t2_count = 0;
    t3_count = 0;
    t4_count = 0;
    count = 0;

}

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.
    count++;
    if(strcmp("t1", getName()) == 0)
    {
        t1_count++;
        EV << "t1 get Message " <<t1_count<< "\n";
    }
    if(strcmp("t2", getName()) == 0)
    {
        t2_count++;
        EV << "t2 get Message " << t2_count<<"\n";
    }
    if(strcmp("t3", getName()) == 0)
    {
        t3_count++;
        EV << "t3 get Message " << t3_count<<"\n";
    }
    if(strcmp("t4", getName()) == 0)
    {
        t4_count++;
        EV << "t4 get Message " << t4_count<<"\n";
    }

    if(count == 100)
    {
        EV << "sum count: " << count<<"\n";
        EV << "the end " <<"\n";
        delete msg;
    }
    else{
        EV << "sum count: " << count<<"\n";
        send(msg, "out"); // send out the message
    }


}

.ini 代码

[General]
network = Tictoc1

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值