omnetpypy.front_end

The front end of the omnetpypy package contains all the tools necessary to users to create their own simulations.

class omnetpypy.front_end.__init__.Channel(name, identifier=None, delay=None, loss_prob=None)[source]

Bases: SimulatedEntity

Channels are used to apply operations on messages travelling between connected ports. They can apply noise, delays, losses, and other custom operations to the messages.

Channels always have two ports, named “A” and “B”. The channel forwards messages from port “A” to port “B”, and vice versa, after applying the specified delay and loss probability.

Subclasses can override the methods process_message() to apply more complex and asymmetric operations on the messages passing through the channel.

Parameters:
namestr

The name of the channel. This name should be unique within the simulation.

identifierint or None, optional

The identifier of the channel. This identifier should be unique within the simulation. If None, the identifier will be automatically generated.

delayfloat or None, optional

The delay to be applied to the messages passing through the channel. If None, no delay is applied.

loss_probfloat or None, optional

The probability of loss to be applied to the messages passing through the channel. If None, no loss is applied.

Attributes:
delayfloat or None

The delay to be applied to the messages passing through the channel. If None, no delay is applied.

loss_probfloat or None

The probability of loss to be applied to the messages passing through the channel. If None, no loss is applied.

apply_loss(message, port_name)[source]

Apply loss to the message, based on an arbitrary loss probability distribution.

Parameters:
messageMessage

The message to be delayed.

port_namestr

The port from which the message was received.

Returns:
bool

True if the message should be lost, False otherwise.

generate_delay(message, port_name)[source]

Generate a delay for the message, based on an arbitrary delay distribution.

Parameters:
messageMessage

The message to be delayed.

port_namestr

The port from which the message was received.

Returns:
float or None

The delay to be applied to the message. If None or zero, the message will be sent immediately.

handle_message(message, port_name)[source]

Handle a message received from a port. First, it processes the message by calling the method process_message(), then it applies the optional delay and loss probability, and finally it sends the message out of the other port (if not lost).

Parameters:
messageMessage

The message to be handled.

port_namestr

The port from which the message was received.

process_message(message, port_name)[source]

Process a message received from a port. By default, this method does nothing and returns the message as is. Subclasses can override this method to apply more complex operations on the messages passing through the channel.

Parameters:
messageMessage()

The message to be processed.

port_namestr

The port from which the message was received.

Returns:
Message() or None

The message to be sent to the connected port, or None if the message should be dropped.

class omnetpypy.front_end.__init__.CompoundModule(name, identifier, port_names, **kwargs)[source]

Bases: SimulatedEntity

This class represents a compound module in the simulation model. Compound modules are non-atomic modules that contain other modules and channels. They do not come with a behavior, but they can be used to organize the simulation model with a hierarchical structure. Their behavior is intrinsically defined by the behavior of their submodules and how they are connected.

See SimulatedEntity for inherited attributes. All kwargs passed to the constructor are also stored as attributes of the module.

Parameters:
namestr

The name of the module. This name should be unique within the simulation.

identifierint

The identifier of the module. This identifier should be unique within the simulation.

port_nameslist of str

The names of the ports of the module.

**kwargs

Additional, arbitrary attributes of the module, passed as keyword arguments.

Attributes:
sub_modulesdict of SimulatedEntity

The sub-entities of the module, indexed by their names.

add_sub_module(module)[source]

Add a submodule to this compound module.

Parameters:
moduleSimulatedEntity

The submodule to be added.

Raises:
Exception

If a submodule with the same name already exists.

cancel_scheduled(message)[source]

Cancel a scheduled self message for this entity. Calls internally the method cancel_scheduled().

Parameters:
messageMessage

The message to be cancelled.

connect(local_port, remote_entity, remote_port, channel=None)[source]

Connect a port of this module to a port of another remote entity.

Parameters:
local_portstr

The name of the local port to be connected.

remote_entitySimulatedEntity

The remote entity to which the port will be connected.

remote_portstr

The name of the remote port to be connected.

channelChannel or None, optional

The channel through which the connection will be made, if any. If set, the local port is connected to the “A” port of the channel, and the “B” port of the channel is connected to the remote port.

See also

connect()
forward_input(port_container, port_submodule)[source]

Forward the input of a port of this module to a port of a submodule.

Parameters:
port_containerstr

The name of the port of this module that will forward the input.

port_submodulePort

The port of the submodule that will receive the input.

See also

forward_input()
forward_output(port_submodule, port_container)[source]

Forward the output of a port of a submodule to a port of this module.

Parameters:
port_submodulestr

The name of the port of the submodule that will forward the output.

port_containerPort

The port of this module that will receive the output.

See also

forward_output()
get_sub_module(name)[source]

Get a submodule by its name.

Parameters:
namestr

The name of the submodule to be retrieved.

Returns:
SimulatedEntity

The submodule with the given name.

handle_message(message, port_name)[source]

Process a message received as input to a port.

Parameters:
messageMessage

The message to be processed.

port_namestr or None

The name of the port on which the message was received. If None, the message was sent by the entity itself (self message).

is_scheduled(message)[source]

Check if a message is scheduled as a self message for this entity. Calls internally the method is_scheduled().

Parameters:
messageMessage

The message to be checked.

schedule_message(message, at=None, delay=None)[source]

Schedule a self message to be received by this entity. Calls internally the method schedule_self_message().

Parameters:
messageMessage

The message to be processed.

atfloat or None, optional

The simulation time at which the message should be processed. If None, the delay parameter will be used.

delayfloat or None, optional

The time delay from the current simulation time at which the message should be processed. If None, the at parameter will be used.

send(message, port_name)[source]

Send a message out on a specified port. Calls internally the method tx_output().

Parameters:
messageMessage

The message to be sent.

port_namestr

The name of the port on which the message should be sent.

class omnetpypy.front_end.__init__.Message(fields, **meta)[source]

Bases: object

This class is a wrapper for messages exchanged between entities in a simulation.

Parameters:
fieldslist

The fields of the message.

metadict, optional

Additional metadata to be stored with the message. A typical use case is to store a “header”.

Attributes:
fieldslist

The fields of the message.

metadict

Additional, editable metadata stored with the message.

class omnetpypy.front_end.__init__.SimpleModule(name, identifier, port_names)[source]

Bases: SimulatedEntity

This class is an abstract class that represents a simple module in the simulation. Simple modules are the basic building blocks of the simulation model. They can send and receive messages through their ports, and they can be connected to other modules through channels connecting their ports. The behavior of a simple module is defined by how it handles incoming messages. The user should subclass this class to define custom simulation modules.

Simple modules are also in charge of recording metrics samples. The user can call the method emit_metric() at any time to record a metric sample.

See SimulatedEntity for inherited attributes.

Parameters:
namestr

The name of the module. This name should be unique within the simulation.

identifierint

The identifier of the module. This identifier should be unique within the simulation.

port_nameslist of str

The names of the ports of the module.

connect(local_port, remote_entity, remote_port, channel=None)[source]

Connect a port of this module to a port of another remote entity. The output of a port will be fed as input to the other port (up to the intermediate actions of a channel, if any).

Parameters:
local_portstr

The name of the local port to be connected.

remote_entitySimulatedEntity

The remote entity to which the port will be connected.

remote_portstr

The name of the remote port to be connected.

channelChannel or None, optional

The channel through which the connection will be made, if any. If set, the local port is connected to the “A” port of the channel, and the “B” port of the channel is connected to the remote port.

emit_metric(name, value)[source]

Record a metric sample in the simulation context. The metric name must be defined in the main configuration file.

Parameters:
namestr

The name of the metric. The metric name must be defined in the simulation configuration.

valueAny

The value of the metric sample

abstract handle_message(message, port_name)[source]

Handle a message received as input to a port. This method must be implemented by every subclass to define the behavior of the custom module.

Parameters:
messageMessage

The message to be processed.

port_namestr or None

The name of the port on which the message was received. If None, the message is a self message scheduled by this module.