Tictoc tutorial 13: Exercise

733 views
Skip to first unread message

Mohd Adib Sarijari

unread,
Apr 23, 2012, 12:20:23 PM4/23/12
to omn...@googlegroups.com
Does any body have solution for this exercise?

anybody have idea?

Carlo

unread,
Apr 24, 2012, 5:08:55 AM4/24/12
to omn...@googlegroups.com
Hello Mohd :)
i solved in this way..in the initialize()

void Txc15::initialize()
{
 
    int     n = par("pkt");     //number of packets that node 0 will transmit
    double  x;                  //this will contain the time when the next packet will be transmitted
 
    hopCountStats.setName("hopCountStats");
    hopCountStats.setRangeAutoUpper(0, 10, 1.5);
    hopCountVector.setName("HopCount");

    // Module 0 sends the first message
   if (getIndex()==0)                  //   NOTE: if you comment this line and the other two "{" and "}" every node will transmit packets randomly
   {
        TicTocMsg15 *msg[n];                                          // a vector of messages

        // Multiple messages from node 0
        for (i=0;i<=n-1;i++) {
                msg[i] = generateMessage();
                x = exponential(2);             //here i extract the next instant when the packet will be scheduled
                EV << "pkt " << msg[i] << " will be transmitted in " << x << " seconds!\n";
                scheduleAt(0.0+x, msg[i]);

     }
    }
}


Let me know if you like this solution or you have a better one :)

Mohd Adib Sarijari

unread,
Apr 24, 2012, 5:23:55 AM4/24/12
to omn...@googlegroups.com
Great Carlo,

thanks for the sharing. it is a good solution. appreciate it much.

regards,
Adib

--
Sent from the OMNeT++ mailing list. To configure your membership,
visit http://groups.google.com/group/omnetpp

Zulhizam Sanip

unread,
Jan 22, 2013, 11:20:34 AM1/22/13
to omn...@googlegroups.com
My solution...

Txc13::Txc13() {
    event = NULL;
    period = 0;
}


Txc13::~Txc13()
{
    cancelAndDelete(event);
}

void Txc13::initialize()
{
    event = new cMessage("event");   // Create event for scheduling
    period = par("periodical");            // get the random periodical time
    scheduleAt(simTime()+period,event);  // schedule the self event message

    if (getIndex()==0)
    {
        // Boot the process scheduling the initial message as a self-message.

        cMessage *msg = generateMessage();
        scheduleAt(0.0, msg);
    }
}

TicTocMsg13 *Txc13::generateMessage() {

    int src = getIndex(); // module index
    int n = size();      // module vector size
    int dest = intuniform(0,n-2);
    if (dest>=src) dest++;

    char msgname[20];
    sprintf(msgname, "txc-%d", getIndex());
    EV << "Message " << msgname << " from " << src << " to " << dest << endl;
    TicTocMsg13 *msg = new TicTocMsg13(msgname);
    msg->setSource(src);
    msg->setDestination(dest);
    return msg;
}


void Txc13::handleMessage(cMessage *msg)
{
    if (msg == event) {                                                 // if the self event message arrived
        EV << "Generating extra message...";
        TicTocMsg13 *extramsg = generateMessage();   // generate a new message to forward 
        forwardMessage(extramsg);                           
        scheduleAt(simTime()+period,event);                 // schedule another event message so there are multiple
                                                                             //  messages in the network
    } else {
        TicTocMsg13 *ttmsg = check_and_cast<TicTocMsg13 *>(msg);
        if (ttmsg->getDestination() == getIndex())
        {
            // Message arrived.
            EV << "Message " << ttmsg << " arrived after " << ttmsg->getHopCount() << " hops.\n";
            bubble("ARRIVED, starting new one!");
            delete ttmsg;
        } else
        {
            // We need to forward the message.
            forwardMessage(ttmsg);
        }
    }
}

void Txc13::forwardMessage(TicTocMsg13 *msg)
{
    // In this example, we just pick a random gate to send it on.
    // We draw a random number between 0 and the size of gate `out[]'.
    int n = gateSize("gate");
    int k = intuniform(0,n-1);
    cGate * gate = msg->getArrivalGate();
    // Increment hop count.
    msg->setHopCount(msg->getHopCount()+1);

    if (gate != NULL && n > 1 ) {
        EV << "Found the same port [" << k << "].\nReassigning port...\n";
        int gate_ind = gate->getIndex();
        while (gate_ind == k) {
            k = intuniform(0,n-1);
        }
    }
    EV << "Forwarding message " << msg << " on port out[" << k << "]\n";
    send(msg, "gate$o", k);

}

Reema Sharma

unread,
Feb 3, 2013, 7:29:12 AM2/3/13
to omn...@googlegroups.com
Sir , i am a beginer in Omnet++ and need your expert guidance. I started with tictoc example, but when i am using "make " command to create executable file, i am getting an error that omnetpp.h is not found (No such file or directory) . But in same directory if we run the sample tictoc progam it is generating the tictoc1.exe. and sample program is running successfully.
Kindly help.

Mohammadreza sahebi

unread,
May 6, 2013, 10:54:56 PM5/6/13
to omn...@googlegroups.com
Thank you so much about your solution;

tuchae

unread,
Jun 15, 2013, 7:33:48 AM6/15/13
to omn...@googlegroups.com
I try your code but it has some error when run program (pop up message) , it told me that hasn't parameter "periodical" of par() function.

Fady Samann

unread,
Oct 8, 2021, 5:50:47 AM10/8/21
to OMNeT++ Users
Hello ALL,
The question of the exercise was "Change the module class so that instead, it generates messages periodically. ". 
I think that the initialize() and handleMessage() methods are not useful in this case, because of in the first one the code will be implemented once and in the second method the code will only implemented when a message is arrived (self-message or network message).
Even though, the process of periodic messages triggered by self-messaging, the later must be triggered by the initialize() or/and handleMessage() methods.
So, anyone know how to do the periodic sending of messages without relying on these methods, or did I misunderstood the question of the exercise??   
Reply all
Reply to author
Forward
0 new messages