Hello I am new to omnet++. I am trying to implement a simple broadcast service in omnet++ without using the inet. I have 6 nodes in ring topology. node[0] is the source and node[3] is the destination. So the idea is node[0] should create message and broadcast them through its gates till it reaches the desitination and delete it once it has reached.
I have attached my code below. I am not sure if there is a logical error here because i can compile the code. Also i am not sure if i have correctly used msg->getArrivalGateId() in my forward function, ideally i would need the getIndex() function. Please let me know how do i fix this. Any feedback is appreciated.
void broadcast::initialize()
{
if (getIndex() == 0) {
MyMessage *msg = generateMessage();
scheduleAt(0.0, msg);
}
}
MyMessage *broadcast::generateMessage()
{
// Produce source and destination addresses.
int source = getIndex(); // our module index
//int n = getVectorSize(); // module vector size
int destination = (3);
int hopcount = 3;
char msgname[20];
sprintf(msgname, "Hello from-%d-to-%d, ", source, destination);
// Create message object and set source and destination field.
MyMessage *msg = new MyMessage(msgname);
msg->setSource(source);
msg->setDestination(destination);
msg->setHopcount(hopcount);
return msg;
}
void broadcast::handleMessage(cMessage *msg)
{
MyMessage *bmsg = check_and_cast<MyMessage *>(msg);
if (bmsg->getDestination() == getIndex()) {
delete bmsg;
// Generate another one.
EV << "Generating another message: ";
MyMessage *bmsg = generateMessage();
EV << bmsg << endl;
forwardMessage(bmsg);
}
else {
forwardMessage(bmsg);
}
}
void broadcast::forwardMessage(MyMessage *msg)
{
// Increment hop count.
// msg->setHopcount(msg->getHopcount()+1);
int size = gateSize("gate");
int baseId = gateBaseId("gate$o");
for (int i = 0; i < size; i++){
if(i != msg->getArrivalGateId()) {
EV << "Forwarding message " << msg ;
send(msg, baseId+i);
}
}
}
** Event #1 t=0 routing.node[0] (broadcast, id=2) on selfmsg Hello from-0-to-3, (MyMessage, id=0)
send()/sendDelayed(): Cannot send message (MyMessage)Hello from-0-to-3, , it is currently in scheduled-events, being underway between two modules -- in module (broadcast) routing.node[0] (id=2), at t=0s, event #1 errorsnap