I want to implement a timer-based message scheme in VEINs/OMNeT++. Here is a scenario: one node sends message to many nodes (let's say 5 nodes). Each node after receiving message sets its timer to broadcast message to other nodes in a network basing on its distance from sender node, such that the furthest node, set shortest timer. And when a node receives message from other nodes before its timer expired, it cancels the timer. But if the timer expires and it has not received any message from other nodes it broadcast the message.
I tried followed explanation in this link How to implement timers in Omnet++?
I have declared a timer message in the initialize()
function
MyApp::Initialize(int stage)
{
RstValueEvt = new cMessage("reset value evt");
}
Then onWSM function for receiving message checks if a message is received again, I check the timer event, if it is scheduled I cancel the timer using:
MyApp::onWSM(BaseFrame1609* frame)
infoMsg* wsm = check _and_cast<infoMsg>(frame)
if(wsm.getrecipient==myId)
{
if(RstValueEvt->isScheduled())
{ cancelEvent(RstValueEvt); }
else{scheduleAt(simTime()+timer, RstValueEvt);
//creating copy of the message that I need to rebroadcast
cMessage* copyMessage = (cMessage *)infoMsg.dup;
}
}
My issue, is how to make this node broadcast the copy of message(infoMsg) to all nodes in the network when timer expires, that is how to handle this message in handleselfmsg fcn and onWSM fcn?
I suggest the following way to achieve your goal:
declare in your class a message for storing received broadcast message, e.g.
cMessage * toBroadcastMsg;
in constructor set
toBroadcastMsg = nullptr;
create an instance of selfmessage (timer):
MyApp::initialize() {
// ...
RstValueEvt = new cMessage("reset value evt");
// ... }
check whether your selfmessage (timer) expires:
MyApp::handleSelfMsg(cMessage* msg) {
// ...
if (RstValueEvt == msg && toBroadcastMsg != nullptr) {
// (2) send a message from toBroadcastMsg
// ...
toBroadcastMsg = nullptr;
}
schedule the timer after receiving a broadcast message as well as store a duplicate of received broadcast message:
MyApp::onWSM(BaseFrame1609_4* wsm) {
if(wsm.getrecipient == myId) {
if(RstValueEvt->isScheduled()) {
cancelEvent(RstValueEvt);
} else {
scheduleAt(simTime() + timer, RstValueEvt);
// (1) remember copy of the message for future use
toBroadcastMsg = wsm->dup;
}
}