OMNET++中实现模块B接收数据包后立即转发的技术问询
Hey there! Let's break down how to get your Module B to forward each packet immediately after receiving it, instead of waiting for all 5 packets from Module A to arrive first.
Why It's Happening Right Now
Looking at your code and simulation logs, here's the key issue: Your Module A (QKDProcessor) is sending all 5 packets at the exact same simulation time (t=0). Even though Module B (PolarizationFilter) is coded to forward packets as soon as it gets them, all those packets hit B at once—making it look like B is waiting to collect all of them before forwarding. The logs confirm this too, since every event is timestamped at t=0.
The Fix: Add Small Delays Between Packet Sends
To make Module B process and forward packets one by one, we just need to adjust Module A to send each packet with a tiny delay between them. This way, they arrive at B sequentially, and B can forward each immediately as it comes in.
Modified Module A Code
Here's the updated handleMessage method for your QKDProcessor class, with the delay added (plus a small memory leak fix):
void QKDProcessor::handleMessage(cMessage *msg) { if(strcmp("QuantumStatesProcessorBob", getName()) == 0) { delete msg; if(counter == 0) { cMessage *keyCheck = new cMessage("T"); send(keyCheck,"out",0); } counter++; } else { std::string key = msg->getName(); delete msg; char *stat = new char[key.length()+1]; strcpy(stat, key.c_str()); for(int i=0; i<key.length(); i++) { // Selecting random gates double x = rand()/static_cast<double>(RAND_MAX+1); randomGate = 0 + static_cast<int>( x * (4 - 0) ); // Create individual message for each bit. std::string j = std::string(1,stat[i]); cMessage *quantumState = new cMessage(j.c_str()); // Add a small delay between each packet send (0.1 time units here) sendDelayed(quantumState, simtime_t(i)*0.1, "out", randomGate); } delete[] stat; // Free the allocated char array to avoid memory leaks } }
Key Changes Explained
- Replaced
send()withsendDelayed(): This method sends the message after a specified delay. We usesimtime_t(i)*0.1so each subsequent packet is sent 0.1 time units later than the previous one—you can tweak this delay value to fit your simulation's timing needs. - Added
delete[] stat;: Your original code allocated a char array but didn't free it, which would cause memory leaks over time. This line fixes that.
Why Module B Doesn't Need Changes
Your PolarizationFilter class is already doing exactly what you want—no tweaks needed here:
void PolarizationFilter::handleMessage(cMessage *msg) { send(msg,"polarizaedLight",0); }
As soon as it receives a message, it forwards it immediately. The only problem was the packets arriving all at once, which we've fixed with the staggered delays in Module A.
What to Expect in the Simulation Log
After making this change, your simulation logs will show events at different timestamps (t=0, t=0.1, t=0.2, etc.). You'll see Module B receive each packet at those staggered times and forward them right away, instead of all events stacking up at t=0.
内容的提问来源于stack exchange,提问作者Faisal Saleem

