3,882
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
#include <myMessage_m.h>
class Txc10 : public cSimpleModule
{
protected:
virtual void forwardMessage(myMessage *msg);
virtual void initialize();
virtual void handleMessage(myMessage *msg);
};
Define_Module(Txc10);
void Txc10::initialize()
{
if (getIndex()==0)
{
// Boot the process scheduling the initial message as a self-message.
char msgname[20];
sprintf(msgname, "user-%d", getIndex());
myMessage *msg = new myMessage(msgname);
scheduleAt(0.0, msg);
}
}
void Txc10::handleMessage(myMessage *msg)
{
myMessage *ttmsg = check_and_cast<myMessage *>(msg);
if (getIndex()==3)
{
// Message arrived.
EV << "消息 " << ttmsg << " 到达.\n";
delete ttmsg;
}
else
{
// We need to forward the message.
forwardMessage(ttmsg);
}
}
void Txc10::forwardMessage(myMessage *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");
int k = intuniform(0,n-1);
EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
send(msg, "out", k);
}