singleton in omnet mixim

170 views
Skip to first unread message

Humberto

unread,
Feb 20, 2013, 7:13:09 AM2/20/13
to omn...@googlegroups.com
This post comes from the discussion in this other one ... https://groups.google.com/d/msg/omnetpp/iyO5H4OrxBU/7VkEL4gpNmIJ

I finally created a Singleton class that inherits from cSimpleModule

In order to make it work I had to include its .ned it into my SNetwork.ned description. I did it like this

network SNetwork extends BaseNetwork
{
    parameters
:
       
int numNodes; // total number of hosts in the network

    submodules
:
        node
[numNodes]: SNode {
            parameters
:
               
@display("p=170,50;i=device/wifilaptop");
       
}
       
GlobalMetric: SingletonGlobalMetrics {
           
@display("p=50,40;i=misc/globe;is=b");
       
}
    connections allowunconnected
:
   
// all connections and gates are to be generated dynamically
}

I have to point out that, in order to get success with the Singleton pattern. All variables must be initialized at the Constructor, because this is how the single instance will be called.

The use of the inheritance from cSimpleModule helps to get a call to  the destructor once the simulation is over.
As Rudolf pointed out, the destructor is always being called whether the simulation has been successful or not.
For that it requires some extra code to filter that situation, nothing special.

Still I am not fully sure about what I did. For this I Have the following question.
Do we have any other way to subscribe my Singleton-SimpleModule to the simulation without edition the Network.ned?



Rudolf Hornig

unread,
Feb 20, 2013, 8:11:00 AM2/20/13
to omn...@googlegroups.com


On Wednesday, 20 February 2013 13:13:09 UTC+1, Humberto wrote:
This post comes from the discussion in this other one ... https://groups.google.com/d/msg/omnetpp/iyO5H4OrxBU/7VkEL4gpNmIJ

I finally created a Singleton class that inherits from cSimpleModule

In order to make it work I had to include its .ned it into my SNetwork.ned description. I did it like this

network SNetwork extends BaseNetwork
{
    parameters
:
       
int numNodes; // total number of hosts in the network

    submodules
:
        node
[numNodes]: SNode {
            parameters
:
               
@display("p=170,50;i=device/wifilaptop");
       
}
       
GlobalMetric: SingletonGlobalMetrics {
           
@display("p=50,40;i=misc/globe;is=b");
       
}
    connections allowunconnected
:
   
// all connections and gates are to be generated dynamically
}

I have to point out that, in order to get success with the Singleton pattern. All variables must be initialized at the Constructor, because this is how the single instance will be called.
No, you should be able to initialize all variables in the initialize() method. You should nowhere use 'static' variables. Just create an ordinary simple module, place it in the top-level network with a predefined name. From your submodules look up the statistic module using the name, and you can call in to pass on the statistics. In the strict sense this is not a "singleton" pattern. It's just that there is a single global statistic module in the network.

The use of the inheritance from cSimpleModule helps to get a call to  the destructor once the simulation is over.
As Rudolf pointed out, the destructor is always being called whether the simulation has been successful or not.
For that it requires some extra code to filter that situation, nothing special.

Still I am not fully sure about what I did. For this I Have the following question.
Do we have any other way to subscribe my Singleton-SimpleModule to the simulation without edition the Network.ned?
This is the way to do. Placing a separate module at the top level ensures that the module will be recreated each time the network is rebuilt.

Rudolf 
Message has been deleted

Humberto

unread,
Feb 20, 2013, 9:58:09 AM2/20/13
to omn...@googlegroups.com

    cModule *mod2 = simulation.getSystemModule()->getSubmodule("GlobalMetric",0);
    EV
<< mod2->getName() << endl;
    mod2
->emit()
    emit
(BaseLayer::catPacketSignal, &app_packet);


I think I found it.
But is there any other way to pass information between modules without using the signals?
I want to skip using new cMessages or several signals for sending my struct_metrics.
Could it be done by connecting gates? would it be dangerous?

Rudolf Hornig

unread,
Feb 20, 2013, 11:22:40 AM2/20/13
to omn...@googlegroups.com
No need to use signals and listeners in this case as the modules would be strictly coupled anyway. Just cast the module pointer using check_and_cast<SingletonGlobalMetrics*> and and call a function on it like updateStatistics(struct_metrics str);

Humberto

unread,
Feb 21, 2013, 8:36:51 AM2/21/13
to omn...@googlegroups.com
Thanks Rudolf for the advice.
Sometimes I have the feeling that I should literally have studied the omnet user manual for all this :P
For those that might have the same or similar problem, please refer to omnet manual point 4.12 "Direct Method call between modules"

With this solution I got rid off the static pointer that was giving my a headache with the destructor and the memory use.
Now seems to be well done, because I have already check that there exist no collisions between the GlobalMetric Modules when I run several simulations in parallel with different parameters.



Reply all
Reply to author
Forward
0 new messages