如何在 Omnet 的 UDPBasicApp 中检查转发的数据包
How to check forwarded Packets in UDPBasicApp in Omnet
如何修改 UDPBasicApp 以在收到的消息中查找重复项?
我对 class UDPBasicApp.cc 进行了这些更改,以添加一个额外的步骤来检查收到的 udp 数据包,如下所示,但我在 .sca/.vec 中看不到任何效果,甚至没有显示气泡。
错误可能在哪里?
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);
}
}
由于我对参与您的整个系统的实体没有足够的上下文,我将提供以下想法:
您可以通过在您的应用程序中添加以下行来为您的应用程序的每条消息添加一个唯一的 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 */
对您来说棘手的事情是如何定义一条消息是否已被查看两次(或更多次)。
如何修改 UDPBasicApp 以在收到的消息中查找重复项? 我对 class UDPBasicApp.cc 进行了这些更改,以添加一个额外的步骤来检查收到的 udp 数据包,如下所示,但我在 .sca/.vec 中看不到任何效果,甚至没有显示气泡。 错误可能在哪里?
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);
}
}
由于我对参与您的整个系统的实体没有足够的上下文,我将提供以下想法:
您可以通过在您的应用程序中添加以下行来为您的应用程序的每条消息添加一个唯一的 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 */
对您来说棘手的事情是如何定义一条消息是否已被查看两次(或更多次)。