Creating an example project

To create an empty project, perform the following steps:

  1. Open the OMNeT++ IDE.
  2. Navigate to File | New | OMNeT++ Project... or click the down-facing triangle on the New icon and select OMNeT++ Project....
  3. Enter a name that you would like to give your new project and click on Next >.
  4. Select the Tictoc example file in the Examples folder, as shown in the following screenshot:
    Creating an example project
  5. Finally, click on Finish to create the Tictoc example project.

Let's first have a look at what we have understood already. In the newly created project, navigate to the simulations folder in the Project Explorer window of the OMNeT++ IDE and double-click on Tictoc.ned to open it.

You should see something similar to what we have discussed previously in this book—this is the topology of a network called Tictoc . This network contains two modules, tic and toc. The source code that you can see by clicking on the Source tab next to the Design tab shows that this network topology is very basic:

package example.simulations;

import example.Txc;

//
// Two instances (tic and toc) of Txc connected.
//
network Tictoc
{
    submodules:
        tic: Txc;
        toc: Txc;
    connections:
        tic.out --> {delay = 100ms;} --> toc.in;
        tic.in <-- {delay = 100ms;} <-- toc.out;
}

We can see that tic and toc are submodules of Txc. The out gate of tic is connected to the in gate of toc and vice versa.

Creating an example project

In the src folder of this project, double-click on Txc.ned and click on the Source tab. The following is a code snippet of the source:

package example;

//
// Immediately sends out any message it receives. It can optionally generate
// a message at the beginning of the simulation, to bootstrap the process.
//
simple Txc
{
    parameters:
        bool sendInitialMessage = default(false);
    gates:
        input in;
        output out;
}

This module contains one parameter, a Boolean value called sendInitialMessage, which has been given the default value false. The module contains one input gate called in, and one output gate called out. We can see these gates used in the Tictoc network topology as shown in the following line of code:

tic.out --> {delay = 100ms;} --> toc.in;

This example project, however, does not use a library like INET. In fact, this example has its own source code that defines the Txc module. In the Project Explorer, navigate to the src folder under the example project and double-click on the Txc.cc C++ source file. You will see the following source code:

#include "Txc.h"

namespace example {

Define_Module(Txc);

void Txc::initialize()
{
    if (par("sendInitialMessage").boolValue())
    {
        cMessage *msg = new cMessage("tictocMsg");
        send(msg, ""out"");
    }
}
void Txc::handleMessage(cMessage *msg)
{
    // just send back the message we received
    send(msg, "out"");
}

}; // namespace

You can see that an OMNeT++ module is defined:

Define_Module(Txc);

This module has two methods: initialize and handleMessage(cMessage *msg). The initialize method does a check on the sendInitialMessage parameter to see if it equals true. This parameter was defined in the Txc module's NED file as follows:

parameters:
        bool sendInitialMessage = default(false);

So we can see that by default, the value of sendInitialMessage is set to false. The omnetpp.ini configuration file can be used to set the value of sendInitialMessage to true. This is shown in the following code snippet:

[General]
network = Tictoc
cpu-time-limit = 60s
#debug-on-errors = true

**.tic.sendInitialMessage = true

This tells us that the submodule tic of the Txc module will send the initial message when the simulation is started.

In the initialize method, if the sendInitialMessage variable is equal to true, the following message is sent from the out gate:

if (par(""sendInitialMessage").boolValue())
    {
        cMessage *msg = new cMessage(""tictocMsg");
        send(msg, "out"");
    }

The network topology for this simulation tells us that the message is sent between the two submodules tic and out. A message is sent from the out gate of tic to the in gate of toc. But what happens when toc receives this message? Like in every defined OMNeT++ module, there must be a handleMessage method. This method gets called within the module once it has received a message during the simulation runtime as follows:

void Txc::handleMessage(cMessage *msg)
{
    // just send back the message we received
    send(msg, "out");
}

This method takes the message that it received and sends it as a message from the module's out gate as the network topology tells us:

tic.out --> {delay = 100ms;} --> toc.in;
tic.in <-- {delay = 100ms;} <-- toc.out;

We can see that the tic and the toc node will constantly send a message back and forth as both the modules' out gates are connected to the other node's in gate, thus causing an infinite send and receive between the two nodes as shown in the following screenshot:

Creating an example project

Let's now change the messages that go back and forth in order to say something else. Let's make tic say "Hello" and then toc reply with "Hello back!".

Let's first modify the initialize() method of Txc.cc to:

void Txc::initialize()
{
    if (par("sendInitialMessage").boolValue())
    {
        cMessage *msg = new cMessage("Hello");
        send(msg, "out");
    }
}

All I've done is changed the variable msg to store the string Hello instead of tictocMsg. That was simple enough, and now let's change the handleMessage() method to the following:

void Txc::handleMessage(cMessage *msg)
{
    if (strcmp("tic", getName()) == 0) {
        msg = new cMessage("Hello");
    } else {
        msg = new cMessage("Hello back!");
    }
    send(msg, "out");
}

The If statement checks to see if the message id value sent from a node is even or odd. If the id value is even, the message sent out is Hello, otherwise it's Hello back!. Let's look at a snippet from the output of the simulation log:

** Event #2  T=0.2  Tictoc.tic (Txc, id=2), on 'Hello' (cMessage, id=1)
** Event #3  T=0.3  Tictoc.toc (Txc, id=3), on '{Hello back!}' (cMessage, id=2)
** Event #4  T=0.4  Tictoc.tic (Txc, id=2), on 'Hello' (cMessage, id=3)
** Event #5  T=0.5  Tictoc.toc (Txc, id=3), on '{Hello back!}' (cMessage, id=4)
** Event #6  T=0.6  Tictoc.tic (Txc, id=2), on 'Hello' (cMessage, id=5)
** Event #7  T=0.7  Tictoc.toc (Txc, id=3), on '{Hello back!}' (cMessage, id=6)
** Event #8  T=0.8  Tictoc.tic (Txc, id=2), on 'Hello' (cMessage, id=7)
** Event #9  T=0.9  Tictoc.toc (Txc, id=3), on '{Hello back!}' (cMessage, id=8)
** Event #10  T=1  Tictoc.tic (Txc, id=2), on 'Hello' (cMessage, id=9)

This shows us that the modification to the Txc node class was successful. This also shows us how we can use the simulation output log as a way to debug our network simulation. The simulation log appears when you start your network simulation environment. The following is what it looks like:

Creating an example project
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.125.51