大家好,又见面了,我是你们的朋友全栈君。
tictoc12.ned文件
//input:指定当前门是输入门,只能和输出门连接,只能接受消息
//output:当前门是输出门,只能和输入门连接,只能发送消息
//inout:既是输入门又是输出门,既能发送消息也能接受消息
simple Txc12
{
parameters:
@display("i=block/routing");
gates:
inout gate[]; // declare two way connections声明双向连接
}
// using two way connections to further simplify the network definition
network Tictoc12
{
types:
channel Channel extends ned.DelayChannel {
delay = 100ms;
}
submodules:
tic[6]: Txc12;
connections:
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++;
}
Txc12.cc文件
//输入输出门向量,随机消息发送
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
/** * Let's make it more interesting by using several (n) `tic' modules, * and connecting every module to every other. For now, let's keep it * simple what they do: module 0 generates a message, and the others * keep tossing it around in random directions until it arrives at * module 2. */
class Txc12 : public cSimpleModule
{
protected:
virtual void forwardMessage(cMessage *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Txc12);
void Txc12::initialize()
{
if (getIndex() == 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 Txc12::handleMessage(cMessage *msg)
{
if (getIndex() == 3) {
// Message arrived.
EV << "Message " << msg << " arrived.\n";
delete msg;
}
else {
// We need to forward the message.
forwardMessage(msg);
}
}
void Txc12::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 `gate[]'.我们画了一个介于0和“gate[]”大小之间的随机数。
int n = gateSize("gate");
int k = intuniform(0, n-1);
EV << "Forwarding message " << msg << " on gate[" << k << "]\n";
// $o and $i suffix is used to identify the input/output part of a two way gate。$o和$i后缀用于标识双向门的输入/输出部分
//inout门发送消息,门的名称+“$o”表示输出门,门的名称+“$i”表示输入门
send(msg, "gate$o", k);
}
omnetpp.ini
[Config Tictoc12]
network = Tictoc12
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/234383.html原文链接:https://javaforall.cn