0% found this document useful (0 votes)
2 views

L1

The document outlines the TicToc project in OMNeT++, detailing course prerequisites, the concept of discrete event simulation (DES), and the setup process for the project. It explains the use of OMNeT++ as a simulation framework, including key files and methods for implementing module behavior. Additionally, it covers enhancements to the TicToc simulation, such as adding icons, logging, state variables, and parameters.

Uploaded by

ozcan8479
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

L1

The document outlines the TicToc project in OMNeT++, detailing course prerequisites, the concept of discrete event simulation (DES), and the setup process for the project. It explains the use of OMNeT++ as a simulation framework, including key files and methods for implementing module behavior. Additionally, it covers enhancements to the TicToc simulation, such as adding icons, logging, state variables, and parameters.

Uploaded by

ozcan8479
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

1

TicToc Project in
OMNeT++

DR. MOHAMMED AL-HUBAISHI

Dr. Mohammed Al-Hubaishi


2
Course Requirements

Students must complete courses such as


C++, OOP, and Computer Networks
before enrolling in this elective course.
3
Discrete event simulator (DES)

A discrete event simulator (DES) is a powerful


tool for modeling and analyzing systems
where changes happen at specific points in
time, rather than continuously.

Dr. Mohammed Al-Hubaishi


4
Example of DES :

1.Computer Network:
Computer networks are complex systems connecting
numerous devices like computers, servers, and other
equipment through various communication channels.
► Events: Data packet arrival, router forwarding, link
failure constantly occur, dictating the flow of
information.
► Simulation: You can evaluate network
performance, optimize routing protocols, and test
new technologies.

https://en.wikipedia.org/wiki/Computer_network_diagram

Dr. Mohammed Al-Hubaishi


5
Example of DES :

2.Traffic Network:
► Events: Vehicle arrival, lane change, traffic
light change.
► Simulation: You can test different traffic
management strategies, predict
congestion, and evaluate infrastructure
improvements.

Dr. Mohammed Al-Hubaishi


6
OMNeT++

OMNeT++ is a discrete event simulation framework that


provides libraries, tools, and APIs for building simulations of
various systems. It allows developers to model complex
systems using components called modules, which interact
with each other through message passing.
► Let's consider a simple example of a network simulation
where we model the behavior of nodes sending packets
to each other over a network.
1. tictoc tutorial omnet++
2. https://docs.omnetpp.org/tutorials/tictoc/

Dr. Mohammed Al-Hubaishi


7
Setting Up the TicToc Project

Steps to set up the project:


► Create a new OMNeT++ project.
► Add necessary files: .ned, .cc, and .ini.
Key files:
1. tictoc.ned: Network and module definitions.
2. tictoc.cc: Implementation of module behavior.
3. omnetpp.ini: Configuration file for the simulation.

Dr. Mohammed Al-Hubaishi


8
1.3 Adding the NED file

tictoc1.ned

OMNeT++ uses NED files to define


components and to assemble them into
larger units like networks. To add the file
to the project, right-click the project
directory in the Project Explorer panel on
the left, and choose New -> Network
Dr. Mohammed Al-Hubaishi
Description File (NED)
9
1.4 Adding the C++ files

Dr. Mohammed Al-Hubaishi


10
MCQ Question 1

What is the correct code to be in the handleMessage function?


a) Define_Module(Txc1);
b) tic: Txc1;
c) output out;
d) send(msg, "out");
11
Question 2

What is the missing code at line 74?


12
1.5 Adding omnetpp.ini

Dr. Mohammed Al-Hubaishi


13
Omnetpp.ini

Dr. Mohammed Al-Hubaishi


14
Event Log

Dr. Mohammed Al-Hubaishi


15
Scenarios

Dr. Mohammed Al-Hubaishi


16
result-recording

Dr. Mohammed Al-Hubaishi


17
Sections

Dr. Mohammed Al-Hubaishi


18
Implementing the Module
Behavior

► Modules are implemented in C++.


► Key methods:
► initialize(): Called at the start of the simulation.
► handleMessage(): Handles incoming messages.

Dr. Mohammed Al-Hubaishi


19
Module Lifecycle Functions

► initialize() // Called during module initialization


► handleMessage (cMessage *msg) // Handles messages
► activity () // module activity function
► finish() // Called when simulation ends

https://doc.omnetpp.org/omnetpp/api/classomnetpp_1_1cSimpleModule.html#a517a5ef252044
90292b84fa5bdd12288

Dr. Mohammed Al-Hubaishi


20
activity()

The activity() function normally contains an infinite loop, with at least a wait() or receiv
The most important functions that can be used in activity() are :
► receive() -- to receive messages (events)
► wait() -- to suspend execution for some time (model time)
► send() family of functions -- to send messages to other modules
► scheduleAt() -- to schedule an event (the module "sends a message to itself")
► cancelEvent() -- to delete an event scheduled with scheduleAt()
► end() -- to finish execution of this module (same as exiting the activity() function)
https://doc.omnetpp.org/omnetpp/manual/#sec:simple-modules:activity

Dr. Mohammed Al-Hubaishi


activity() and handleMessage() 21
Functions

OMNeT++ is a discrete event simulation framework. Modules in OMNeT++ use


specific functions to define their behavior. Two primary methods for
implementing module behavior are activity() and handleMessage().

1. activity(): Conoutine-based, sequential programming style, runs in its own


thread.

2. handleMessage(): Event-driven, runs in the simulation's main thread,


preferred for modern usage.

Dr. Mohammed Al-Hubaishi


activity() and handleMessage() 22
Functions
Aspect activity() handleMessage()

Programming Paradigm Coroutine-based (sequential) Event-driven

Runs in simulation's main


Threading Runs in separate thread
thread
Uses receive() to explicitly get Automatically called when
Message Reception
messages messages arrive
Less efficient due to thread
Memory Efficiency More efficient
overhead
Single continuous function with Individual message handling
Code Structure
infinite loop function
Blocks until message arrives using Processes one message per
Message Handling Style
receive() function call
Current Usage Status Legacy approach, less common Modern preferred approach
Implementation Requires understanding of
More intuitive for beginners
Complexity event-driven programming
Higher (requires separate thread Lower (shares simulation
Resource Usage
stack) thread)
Control Flow Direct sequential control Callback-based control
Lower due to thread context Better due to single-threaded
Performance
switching operation
Can be more complex due to Easier to debug in single
Debugging
multiple threads thread

Dr. Mohammed Al-Hubaishi


Message Management 23
Functions:

- scheduleAt (simtime_t t, cMessage *msg)


// Schedule self-message
- cancelEvent (cMessage *msg)
// Cancel scheduled message
- send (cMessage *msg, const char *gateName)
// Cancel scheduled message

https://doc.omnetpp.org/omnetpp/api/classomnet
pp_1_1cSimpleModule.html#a97c57271ca2fc95225
ed8514750cac27

Dr. Mohammed Al-Hubaishi


24
Parameter and Statistics
Functions:

► - par (const char *parname) // Access module


parameters
► - recordScalar (const char *name, double value) //
Record statistics
► - emit (simsignal_t signal, type value) // Emit signals for
statistics

Dr. Mohammed Al-Hubaishi


25
Gate Management:

► - gate (const char *gateName) // Access a gate


► - gateSize (const char *gateName) // Get vector gate size
► - isGateVector (const char *gateName) // Check if gate is
a vector
► - findGate (const char * gatename, int index = -1 )
► - getFullPath ()

https://doc.omnetpp.org/omnetpp/api/classomnetpp_1_1cModule.html#a527d7dc09
12da702a9bdaaf2bf3c067a

Dr. Mohammed Al-Hubaishi


26
Module Management:

► - getParentModule() // Get parent module


► - getSubmodule (const char *name) // Get submodule
► - getFullName() // Returns the full name of the module

Dr. Mohammed Al-Hubaishi


https://doc.omnetpp.org/omnetpp/api/classomnetpp_1_1c
Module.html#a6273eb514331e0e44130844d637b8b4c
27
Simulation Control:

► - simTime() // Get current simulation time


► - getEnvir() // Access simulation environment
► - endSimulation() // End simulation

Dr. Mohammed Al-Hubaishi


28
Random Number Generation:

► - uniform (double a, double b) // Generate uniform


random number
► - exponential (double mean) // Generate exponential
random number
► - normal (double mean, double stddev) // Generate
normal distribution
https://doc.omnetpp.org/omnetpp/api/classomnet
pp_1_1cComponent.html#a590fc7b9500267a06fe0f
42acd20b45f

Dr. Mohammed Al-Hubaishi


29
Network Configuration:

► - getModuleByPath (const char *path) // Find module


by path
► - getSystemModule () // Get root module
► - getFullPath() // Get full path of module

https://doc.omnetpp.org/omnetpp/api/classomnet
pp_1_1cSimulation.html#ad21862849b0933a7550b5
5aa90d8620c

Dr. Mohammed Al-Hubaishi


30
Logging and Debug:

► - EV << "message" // Event log output


► - bubble (const char *text) // Display bubble message in GUI
► - error (const char *format,...) // Error handling
► - EV_WARN << “warning message" <<endl;

https://doc.omnetpp.org/omnetpp/api/group__Log
ging.html#ga650ef3eff8a2900bef69dae29c05d2dd

Dr. Mohammed Al-Hubaishi


cMessage and cPacket in 31
OMNeT++

1. cMessage:
► Base class for messages in OMNeT++
► Used for simple messages, control signals, and timers
2. cPacket (inherits from cMessage):
► Represents network packets/frames

Dr. Mohammed Al-Hubaishi


cMessage and cPacket in 32
OMNeT++
Aspect cMessage cPacket
Class Type Base class Derived from cMessage
Primary Use Control messages, timers, signals Network packets, frames, protocol data units
Size Properties No size information Has byte length and bit length

Encapsulation Cannot encapsulate other messages Can encapsulate/decapsulate other packets

Error Handling Basic error flags Additional bit error handling


Creation Example new cMessage("Timer") new cPacket("DataPacket", TYPE, byteLength)
Memory Overhead Lower (minimal properties) Higher (additional network properties)
Typical Methods setKind(), getKind(), setSchedulingPriority() setByteLength(), encapsulate(), decapsulate(), hasBitError()
Duration Support No duration property Has transmission duration
Protocol Support Basic protocol info Full protocol stack support
- Timer events<br>- Control signals<br>- - IP packets<br>- Ethernet frames<br>- Application
Common Uses
Simple notifications data<br>- Protocol headers
Performance Lighter weight Slightly more overhead
Inheritance Base class for all messages Derived from cMessage
Extra Features Basic message features Network simulation features
Storage Only stores basic message data Can store protocol-specific data
Dr. Mohammed Al-Hubaishi
33
cModule Class Reference

► cModule can be used directly for compound modules.


Simple module classes need to be subclassed
from cSimpleModule, a class that adds more functionality
to cModule.
► For navigating the module tree,
see: getParentModule(), getSubmodule(), cModule::Submo
duleIterator, cModule::getModuleByPath(), cSimulation::get
ModuleByPath().

Dr. Mohammed Al-Hubaishi


34
Enhancing the 2-node TicToc (Part 3)

► OMNeT++ Tutorial: Enhancements to the basic TicToc simulation


► Overview of GUI improvements and protocol refinements
► Focus on adding icons, logging, state variables, parameters,
and more

https://docs.omnetpp.org/tutorials/tictoc/part3/

Dr. Mohammed Al-Hubaishi


35
Table of Contents

• 3.1 Adding Icons


• 3.2 Adding Logging
• 3.3 Adding State Variables
• 3.4 Adding Parameters
• 3.5 Using NED Inheritance
• 3.6 Modeling Processing Delay
• 3.7 Random Numbers and Parameters
• 3.8 Timeout & Cancelling Timers
• 3.9 Retransmitting the Same Message

Dr. Mohammed Al-Hubaishi


36
3.1 Adding Icons

► Improve model aesthetics in the GUI


► Assign the 'block/routing' icon from images/block/routing.png
► Set colors via display strings: cyan for tic and yellow for toc
► Changes made in the NED file using the i= tag

Dr. Mohammed Al-Hubaishi


Adding Icons and Colors 37

Routing Icon Cyan for Tic Yellow for Toc

• Use • Color the Tic • Color the Toc


"block/routing" node cyan. node yellow.
icon for both
nodes.

Dr. Mohammed Al-Hubaishi


38
Visualizing Icons

► Icons enhance identification of simulation modules


► Custom display strings in NED help differentiate modules
► Result: A visually pleasing simulation with color-coded icons

Dr. Mohammed Al-Hubaishi


39
3.2 Adding Logging

► Modify C++ code (Txc1 module) to add logging statements


► Use OMNeT++’s EV macro for output logging
► Logs are displayed in the simulation runtime log window
► Option to open separate log windows for individual modules

Dr. Mohammed Al-Hubaishi


40
Implementing Logging

EV Logging Message Tracking Component Logs

•Use EV << for simple •Log when sending •Open separate log
logging in C++ code. initial message and windows for Tic and
receiving/resending Toc in GUI.
messages.

Dr. Mohammed Al-Hubaishi


41
Adding logging

We also modify the C++ code. We


add log statements to Txc1 so that
it prints what it is doing. OMNeT++
provides a sophisticated logging
facility with log levels, log
channels, filtering, etc. that are
useful for large and complex
models, but in this model we'll use
its simplest form EV:

Dr. Mohammed Al-Hubaishi


42
3.3 Adding State Variables

► Introduce a counter as a class member to track message


exchanges
► Initialize counter (e.g., set to 10) in the initialize() method
► Decrement counter in handleMessage() at every message arrival
► Terminate simulation once counter reaches zero
► Inspector view displays current counter value

Dr. Mohammed Al-Hubaishi


43
Adding State Variables

Counter Variable WATCH Macro


► Add a counter as a class ► Use WATCH(counter) to make
member. Set to 10 in the counter visible in the GUI.
initialize(). Decrement in
handleMessage().

Dr. Mohammed Al-Hubaishi


44
3.4 Adding Parameters

► Replace hardcoded 'magic' numbers with configurable parameters


► Use parameters to determine counter value and decide initial
message sending
► Declare parameters in the NED file (supports numeric, bool, string,
xml, etc.)
► Parameters can also be set via omnetpp.ini (note: NED file
assignments override ini settings)

Dr. Mohammed Al-Hubaishi


45
Adding Parameters (cont’d)

Dr. Mohammed Al-Hubaishi


46
Implementing Parameters Details

► Use default values with the default(...) syntax in NED


► Examples of wildcard assignments (e.g., Tictoc4.t*c.limit=5, **.limit=5)
► View parameters via the Object Tree or Module Inspector in the
runtime environment
NED File Declaration C++ Usage Configuration

• Declare parameters • Read parameters in • Set values in NED file


in NED file. Use bool, initialize() using or omnetpp.ini. NED
int, etc. par("paramName"). takes precedence.

Dr. Mohammed Al-Hubaishi


47
3.5 Using NED Inheritance

► Factor out common elements between tic and toc modules


► Create a base simple module and derive Tic and Toc by overriding
parameters and display strings
► Inheritance reduces redundancy in network definitions
► C++ behavior is inherited from the base module (Txc5)

Dr. Mohammed Al-Hubaishi


48
Using NED Inheritance (cont’d)

• Define Txc5 • Create Tic5 • Use Tic5 and


as base and Toc5 Toc5 in
module. extending network.
Txc5.

Base Derived Network


Module Modules Definition

Dr. Mohammed Al-Hubaishi


49
Using Inheritance

https://docs.omnetpp.org/tutorials/tictoc/part3/
Dr. Mohammed Al-Hubaishi
50
3.6 Modeling Processing Delay

► we'll add some timing: tic and toc will hold the message for 1
simulated second before sending it back
► Introduce a delay (e.g., 1 simulated second) before replying to
messages
► Implement delay using self-messages (scheduleAt function)
► Distinguish between arrival of a new message and a returning
self-message in handleMessage()
► Simulates processing delays typical in real systems

Dr. Mohammed Al-Hubaishi


Modeling Processing Delay (code) 51
52
Modeling Processing Delay

Dr. Mohammed Al-Hubaishi


Modeling Processing Delay 53

Self-Messages

•Use self-messages for timing. Add


event and tictocMsg pointers.

Scheduling

•Use scheduleAt() to schedule self-


messages.

Handling

•Differentiate between new messages


and self-messages in handleMessage().

Dr. Mohammed Al-Hubaishi


Modeling Processing Delay 54
55
3.7 Random Numbers and Parameters

► Replace fixed delay with a random delay (configurable via parameters)


► Read random delay values from module parameters in handleMessage()
► Set probability of losing messages in omnetpp.ini
► Experiment with different random seeds and distributions

• Change fixed delay to


Random Delay random value.

Parameter • Read delayTime parameter


Usage in handleMessage().

• Set distribution in
Configuration omnetpp.ini.
Dr. Mohammed Al-Hubaishi
56
Random Numbers and
Parameters codes

Dr. Mohammed Al-Hubaishi


57
3.8 Timeout & Cancelling Timers

► Model a stop-and-wait protocol where tic starts a timer after sending a message
► Use self-messages as timers; if timeout occurs, resend the message
► Cancel the timer upon receipt of a reply using cancelEvent()
► Visual feedback with bubble() calls to indicate events (e.g., message drop)

• Start timer when sending message.


Timer Setup

Message
• Simulate message loss in Toc.
Loss

• Resend on timeout. Cancel timer on


Timeout
Handling
reply.
Dr. Mohammed Al-Hubaishi
58
Implementing Timeouts

Dr. Mohammed Al-Hubaishi


59
class diagram
60
Toc8: Massage lost

the bubble() call in the code, toc will display a


callout whenever it drops the message.
tic will start a timer whenever it sends the message.
When the timer expires, we'll assume the message
was lost and send another one. If toc's reply arrives,
the timer has to be cancelled. The timer will be
(what else?) a self-message.

Dr. Mohammed Al-Hubaishi


61
Tic8: Timeouts

Cancelling the timer will be done with the


cancelEvent() call.

Dr. Mohammed Al-Hubaishi


62
3.9 Retransmitting the Same Message

► Improve retransmission logic by saving a copy of the original message


► Instead of creating a new message, send copies so that the original remains intact
► Use helper functions like generateNewMessage() and sendCopyOf() to manage
retransmissions
► Avoid issues with invalid pointers once the original is deleted

Message Copy • Keep original message, send copies.

• Include sequence numbers in


Sequence Numbers message names.

• Use separate functions for message


Code Organization generation and sending.

Dr. Mohammed Al-Hubaishi


tictoc 9: Retransmitting Message 63
64
Tic9::initialize()

Dr. Mohammed Al-Hubaishi


65
Tic9::handleMessage()

Dr. Mohammed Al-Hubaishi


66
Simulation Outputs &
Visualization

► GUI displays log windows, inspector details, and module icons


► State variables and parameters are live-inspected during simulation
► Output logs show detailed message exchange events and timing details
► Visualization enhances debugging and understanding of simulation flow

Dr. Mohammed Al-Hubaishi


67
Summary & Next Steps

► Recap: Icons, Logging, State Variables, Parameters, NED Inheritance


► Additional enhancements: processing delay, randomization, timeout
handling, retransmission logic
► Key for building more realistic network simulations in OMNeT++
► Next steps: Explore turning the model into a full network and further
statistical analysis

Dr. Mohammed Al-Hubaishi


References
68

► https://omnetpp.org/
► https://docs.omnetpp.org/tutorials/tictoc/part3/
► https://doc.omnetpp.org/omnetpp/manual/
► https://doc.omnetpp.org/omnetpp/api/classomnetpp_1_1cModule.html

Dr. Mohammed Al-Hubaishi


69
References Books

Dr. Mohammed Al-Hubaishi

You might also like