2016-02-09 28 views
0

如何修改UDPBasicApp以在收到的消息中查找重复项? 我对UDPBasicApp.cc类进行了这些更改,以添加一个额外的步骤来检查接收的udp数据包,如下所示,但是我在.sca/.vec中看不到任何效果,甚至不显示气泡。 错误在哪里?如何在Omnet中检查UDPBasicApp中的转发数据包

void UDPBasicApp::handleMessageWhenUp(cMessage *msg) 
{ 

if (msg->isSelfMessage()) { 
    ASSERT(msg == selfMsg); 
    switch (selfMsg->getKind()) { 
     case START: 
      processStart(); 
      break; 

     case SEND: 
      processSend(); 
      break; 

     case STOP: 
      processStop(); 
      break; 

     default: 
      throw cRuntimeError("Invalid kind %d in self message", (int)selfMsg->getKind()); 
    } 
} 

else if (msg->getKind() == UDP_I_DATA) { 
    // process incoming packet 
    //-----------------------------------------------------Added step 
        //std::string currentMsg= "" + msg->getTreeId(); 
        std::string currentPacket= PK(msg)->getName(); 
         if(BF->CheckBloom(currentPacket) == 1) { 
         numReplayed++; 
         getParentModule()->bubble("Replayed!!"); 
         EV<<"----------------------WSNode "<<getParentModule()->getIndex() <<": REPLAYED! Dropping Packet\n"; 
          delete msg; 
          return; 
         } 
         else 
          { 
          BF->AddToBloom(currentPacket); 
          numLegit++; 
          getParentModule()->bubble("Legit."); 
          EV<<"----------------------WSNode "<<getParentModule()->getIndex() <<":OK. Pass.\n"; 
         } 
    //----------------------------------------------------------------------------- 
    processPacket(PK(msg)); 
} 

else if (msg->getKind() == UDP_I_ERROR) { 
    EV_WARN << "Ignoring UDP error report\n"; 
    delete msg; 
} 
else { 
    throw cRuntimeError("Unrecognized message (%s)%s", msg->getClassName(), msg->getName()); 
} 
if (hasGUI()) { 
    char buf[40]; 
    sprintf(buf, "rcvd: %d pks\nsent: %d pks", numReceived, numSent); 
    getDisplayString().setTagArg("t", 0, buf); 
} 
} 
+0

您能否显示BF'对象的声明和定义? –

+0

你想检查一条消息是否已经收到两次? – user4786271

+0

是的,请注意“转发”而不是“收到” –

回答

0

因为我没有关于参与你的整个系统中的实体足够的情况下,我将提供以下思路:

你可以一个唯一的ID通过将添加到您的应用程序的每个消息下面一行到你的应用程序*.msg

int messageID = simulation.getUniqueNumber(); 

现在在接收端,你可以有一个std::map<int, int> myMap您存储的<id,number-of-occurences>

每次收到邮件时添加的消息给std::map并增加number-of-occurences

if(this->myMap.count(myMessage->getUniqueID) == 0) /* check whether this ID exists in the map */ 
{ 
    this->myMap.insert(std::make_pair(myMessage->getUniqueID(), 1)); /* add this id to the map and set the counter to 1 */ 
} 
else 
{ 
    this->myMap.at(myMessage->getUniqueID())++; /* add this id to the map and increment the counter */ 

} 

这将允许你跟踪相同的消息是否已经被转发了两次,简单地做:

if(this->myMap.at(myMessage->getUniqueID()) != 1) /* the counter is not 1, message has been "seen" more than once */ 

对你而言,棘手的是你如何定义一条消息是否被两次(或多次)看到。

相关问题