基于第一个创建的 工程
.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