CSE247 on YouTube

Simulation of Two Node Network using the Network Simulator - OMNeT++




Objective: Simulation of Two Node Network using the Network Simulator - OMNeT++

Tasks 1:

1.       Start OMNeT++ IDE by “omnetpp” command
2.       Create your workspace (name “workspace”) in your home directory
3.       Switch to your workspace (don’t install “INET framework” and “sample”)
4.       Create a new “OMNeT++ project” with the name “lab1”


Task 2: Network Simulation

1.       Network Design:
a.       Create a computing node (name “node”)
b.       Create a network (name “twoNode”) with two instances of computing nodes

[Node1]    <-->   [Node2]

c.       Create message (type “packet” and name “N_PDU”) with four fields: Id, Type of message (Data or Ack), Source Address and Destination Address.



2. Behavioural Design: (Data Transfer)

a. Node1 sends a “Data” messages to Node2. Node2 receives this message and sends back the “Ack” message to the Node1.
                   example:

[Node1]                      <-->      [Node2]

                                                     t1         “Data” --> (send)
                                                     t2          “Data” (receive)
                                                     t3          <---“Ack” (send)
                                                     t4         “Ack” (receive)

b.       Node1 sends 10 “Data” messages with id 1 to 10 to Node2. Node2 receives each “Data” message and send back “Ack” message with the same id as that of received “Data” message id to the Node1. After receiving the “Ack” message Node1 sends the next message.



Solution:


simple Node
{
    parameters:
        int id;
    gates:
        input gIn;
        output gOut;
}
network TwoNode
{
    submodules:
        N1: Node {
            id = 1;
        }
        N2: Node {
            id = 2;
            @display("p=120,110");
        }
    connections:
        N1.gIn <-- N2.gOut;
        N1.gOut --> N2.gIn;
}
packet N_PDU {
    int pID;
    string pType;
    int src;
    int dest;  
}

#ifndef __FIRST_CN_LAB_NODE_H_
#define __FIRST_CN_LAB_NODE_H_

#include <omnetpp.h>
#include <N_PDU_m.h>
using namespace omnetpp;

class Node : public cSimpleModule
{
  protected:
            int address;
            int S;
            int D;
            cGate *in;
            cGate *out;
            int counter;
            int id_num;
    virtual void initialize();
    virtual void handleMessage(cMessage *msg);
};

#endif
#include "Node.h"

Define_Module(Node);

void Node::initialize()
{
        address = par("id");
        in  = gate("gIn");
        out = gate("gOut");
        counter = 0;
        id_num = 0;
        if(address == 1)
        {
            cMessage* event = new cMessage();
            scheduleAt(0 , event);
        }
}

void Node::handleMessage(cMessage *msg)
{

    if(msg -> isSelfMessage())
        {
            N_PDU *p = new N_PDU();
            p -> setPID(id_num++);
            p -> setPType("data");
            p -> setSrc(1);
            p -> setDest(2);
            send(p , out);
        }

    else
        {
            N_PDU *A = check_and_cast<N_PDU*>(msg);

            if(strcmp(A -> getPType() , "data") == 0)
            {
               N_PDU *p1 = new N_PDU();
               p1 -> setPID(id_num++);
               p1 -> setPType("ack");
               p1 -> setSrc(2);
               p1 -> setDest(1);
               send(p1 , out);
            }
            else
            {
                counter++;

                if(counter >= 10)
                {
                    return;
                }
                N_PDU *p2 = new N_PDU();
                p2 -> setPID(id_num++);
                p2 -> setPType("data");
                p2 -> setSrc(1);
                p2 -> setDest(2);
                send(p2 , out);
            }

        }
}

No comments:

Powered by Blogger.