0

在我们的模拟中,我们向 cMessage 类添加了两个受保护的字段:

/* sequence number for log files */
long seqNo = 0;

/* timestamp at sending message */
simtime_t sendingTime;

我们添加以下公共方法

public:

  void setSeqNo(long n) {
    this->seqNo = n;
  }

  long getSeqNo() {
    return this->seqNo;
  }

  void setSentTime(simtime_t t) {
    this->sendingTime = t;
  }

  simtime_t getSentTime() {
    return this->sendingTime;
  }

现在,当服务器模拟应用程序运行时,在它执行每条消息 seding 之前:

pkt->setSeqNo(numPkSent);
pkt->setSentTime(simTime());
fprintf(this->analyticsCorrespondentNode, "PKT %u SENT AT TIME %f TO NODE %s \n", numPkSent, pkt->getSentTime().dbl(), d->clientAddr.get4().str().c_str());

另一方面,当模拟应用程序客户端接收到消息时,如果执行:

double recvTime = simTime().dbl();
fprintf(this->analyticsMobileNode, "RECEIVED PKT num. %d SENT AT TIME: %f RECEIVED AT TIME %f TRANSMISSION TIME ELAPSED %f \n", msg->getSeqNo(), msg->getSentTime().dbl(), recvTime, recvTime - msg->getSentTime().dbl());

问题是客户端正确写入了 SeqNo,因为它在发送之前由服务器设置。相反,方法

msg->getSentTime.dbl()

当服务器在服务器日志文件中正确设置时,总是在客户端日志文件中返回 0。我不明白为什么,可能在客户端应用程序中 cMessage 到 cPacket 之间的转换发生了一些奇怪的事情......你知道吗?

4

2 回答 2

2

In order to add own fields to a packet definition you should only prepare the definition in *.msg file. For example file FooPacket.msg:

packet FooPacket {
   long       seqNo;
   simtime_t sendingTime;
   // other fields... 
 }

Then, in your source file *.cc add:
#include "FooPacket_m.h"
The class FooPacket which derives from cPacket as well as all setter and getter methods will be generated automatically during the compilation - you will see the following files: FooPacket_m.h and FooPacket_m.cc.
When your client receives a message, you should check whether the type is the same as you expected and then cast it to FooPacket type. For example this way:

void handleMessage(cMessage *msg) {
   if (dynamic_cast<FooPacket* >(msg)) {
      FooPacket *pkt = check_and_cast<FooPacket* >(msg);
      simtime_t t = pkt->getSendingTime();
   }
   // ...
}
于 2015-07-16T11:52:50.250 回答
1

它可能是从 cMessage 到 cPacket 的转换。你试过这个吗?

Packet pk = check_and_cast<Packet *>(msg);
pk->getSentTime.dbl();

simtime_t double您也可以尝试检查某处double是否有问题,尝试sentTime参数

于 2015-07-16T11:09:02.227 回答