tictoc3总结:增加了counter,相当于计数器
一、cc文件
在这个类中,我们添加一个计数器,并在十次交换后删除消息。
private://私有的
int counter;//在Txc3中多了一个私有的counter计数器
WATCH(counter);//观察这个counter的值
void Txc3::handleMessage(cMessage *msg)
{
// Increment counter and check value.增加计数器和检查值。
counter--;//每处理一次消息,counter就减一
if (counter == 0) {
// If counter is zero, delete message. If you run the model, you'll
// find that the simulation will stop at this point with the message
// "no more events".
EV << getName() << "'s counter reached zero, deleting message\n";
delete msg;//删除消息(新加)
}
else {
EV << getName() << "'s counter is " << counter << ", sending back message\n";
send(msg, "out");
}
}
tictoc4总结:增加了两个参数。default,par的使用
一、ned文件
simple Txc4
{
parameters:
//增加了这两个参数
bool sendMsgOnInit = default(false);
// 模块是否应该在初始化时发出消息,默认是false
int limit = default(2); // 初始化的值为2
@display("i=block/routing");
gates:
input in;
output out;
}
default是默认的初始值
在ini的配置文件中也可以初始值的配置
这个默认的值,在后续的实例化代码中也可以修改
network Tictoc4
{
submodules:
tic: Txc4 {
parameters:
sendMsgOnInit = true;//这里谁等于ture就是谁先发送
//这里对初始化之后的值做了修改
@display("i=,cyan");
}
toc: Txc4 {
parameters:
sendMsgOnInit = false;
@display("i=,gold");
}
connections:
tic.out --> { delay = 100ms; } --> toc.in;
tic.in <-- { delay = 100ms; } <-- toc.out;
}
二.cc文件
void Txc4::initialize()
{
// Initialize the counter with the "limit" module parameter, declared
// in the NED file (tictoc4.ned).
counter = par("limit");//这里相当于把之前的10计数器换成了一个参数,
//limit前边自己定义了,在ini文件中还有一个参数,两个们一块递减
// 我们不再依赖于模块的名称来决定是否发送初始消息
if (par("sendMsgOnInit").boolValue() == true) {
//这里谁等于ture就是谁先发送
//par("sendMsgOnInit").boolValue() 获取了这个变量的布尔类型值
EV << "Sending initial message\n";
cMessage *msg = new cMessage("tictocMsg");
send(msg, "out");
}
}
使用par函数读取参数
这里仿真之后打印出的结果为
为什么是4132
应该是这个tic和toc分别做counter--,toc是从5开始减,tic从2也就是1开始减。
原因是在Txc4这个模块中已经定义了limit是2,但是在在ini文件中又定义了toc的limit为5,toc是继承了Tcx4这个模块,所以相当于重新设置了这个默认参数为5.
tictoc5总结:和4功能相同,只是换了继承的定义方式
simple Toc5 extends Txc5
{
parameters:
@display("i=,gold");
sendMsgOnInit = false; // Toc modules should NOT send a message on init
}
只是在ned文件中把原来写在network中的代码拿出来写了一个模块。
tictoc6:总结:引入了自己给自己发消息的功能,定时scheduleAt的使用
一、ned文件
与之前的都相同。
二、cc文件
class Txc6 : public cSimpleModule
{
private:
cMessage *event = nullptr;
cMessage *tictocMsg = nullptr;
//私有属性,定义了两个cMessage的指针变量
//将指针设置为nullptr,这样析构函数就不会崩溃
//nullptr是c++中空指针类型的关键字
public:
Txc6();
virtual ~Txc6();(新加:析构函数)
protected://固定的写法
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
};
Txc6::~Txc6()//重写析构函数
{
cancelAndDelete(event);//把自消息event先取消再删除
delete tictocMsg;//直接删除
}
void Txc6::initialize()
{
event = new cMessage("event");
tictocMsg = nullptr;
if (strcmp("tic", getName()) == 0)
{
EV << "Scheduling first send to t=5.0s\n";
tictocMsg = new cMessage("tictocMsg");
scheduleAt(5.0, event);//(两个参数,一个是时间,一个是消息)
//相当于定时器,schedule就是说自己给自己发5秒的信息
}
}
void Txc6::handleMessage(cMessage *msg)
{
if (msg == event) {
//收到了自己给自己发的消息,如果收到则说明5s的延迟已经计算完毕
EV << "Wait period is over, sending back message\n";
send(tictocMsg, "out");
tictocMsg = nullptr;
}
else {
EV << "Message arrived, starting to wait 1 sec...\n";
tictocMsg = msg;
scheduleAt(simTime()+1.0, event);//再次定时一秒钟
}
}
tictoc7总结:将延时时间定义为随机数
一、ned文件
simple Txc7
{
parameters:
volatile double delayTime @unit(s);
// delay before sending back message发送回消息前的延迟
//这里定义了delayTime,并且指明了单位为秒
//volatile可变的,@unit(s)属性修饰指明当前变量的单位,此处s表示单位为秒
@display("i=block/routing");
gates:
input in;
output out;
}
二、cc文件
cc文件中大部分和6是一样的。
在handleMessage中新加了一个随机数的代码
if (msg == event) {
EV << "Wait period is over, sending back message\n";
send(tictocMsg, "out");
tictocMsg = nullptr;
}
else {
if (uniform(0, 1) < 0.1) {
//有0.1的概率发送丢包(新加)
EV << "\"Losing\" message\n";
delete msg;
}
else {
simtime_t delay = par("delayTime");
//使用par函数读取在简单模块中定义的参数delayTime
//在ini文件中有定义delaytime
EV << "Message arrived, starting to wait " << delay << " secs...\n";
tictocMsg = msg;
scheduleAt(simTime()+delay, event);
}