0

I don't know whether I don't get the logic behind adding a function to eclipse/omnet++: Tried to simply add a new function "init_func" to the VeinsInetSampleApplication.h Header file

#pragma once

#include "veins_inet.h"

#include "VeinsInetApplicationBase.h"

class VEINS_INET_API VeinsInetSampleApplication : public veins::VeinsInetApplicationBase {
protected:
bool haveForwarded = false;

protected:
virtual bool startApplication() override;
virtual bool stopApplication() override;
virtual void processPacket(std::shared_ptr<inet::Packet> pk) override;
// ADAPTION 2022/05/28
virtual void init_func();


public:
VeinsInetSampleApplication();
~VeinsInetSampleApplication();


};

However when I simply copy the initialization code of the startApplication() function into "init_func()" in VeinsInetSampleApplication.cc "init_func()" is simply ignored by my simulation.

Here's the code snippet which basically shall send a message to all my simulation nodes at time t=5 sec. The only difference to the startApplication() function is that the message sending is triggered at t=5sec. and not at t=10sec. The startApplication() function does its job correctly and even if i comment it out and only make my init_func() function run it is also ignored. (I tried this in order to rule out any issues with the "this" pointer etc. which would suggest there is a "bad interaction" between both functions). So it really seems like my init_func() is not registered. But I don't know. Does anybody have an idea why "init_func()" might be ignored here by Omnet++?

Code of init_func():

void VeinsInetSampleApplication::init_func()
{

// host[0] should stop at t=5s, change in timerManger.create(...)
if (getParentModule()->getIndex() == 0) {
    auto callback = [this]() {
        getParentModule()->getDisplayString().setTagArg("i", 1, "red");

        traciVehicle->setSpeed(0);

        auto payload = makeShared<VeinsInetSampleMessage>();
        timestampPayload(payload);
        payload->setChunkLength(B(100));
        payload->setRoadId(traciVehicle->getRoadId().c_str());

        auto packet = createPacket("accident");
        packet->insertAtBack(payload);
        sendPacket(std::move(packet));
    };


    timerManager.create(veins::TimerSpecification(callback).oneshotAt(SimTime(5, SIMTIME_S)));
}


}

(I also changed return type of "init_func" to bool to further increase similarity. But that of course was also not successful)

Best regards, Lukas

1 Answers1

0

init_func() is ignored because it is not called neither by initialize() nor by any other method. You decided to create a new method, so you have to add calling of that method somewhere in the code.

Jerzy D.
  • 6,707
  • 2
  • 16
  • 22
  • I know, but it's a bit more complicated as I don't really have access to the main func. https://stackoverflow.com/questions/54736201/the-main-function-in-omnet – user18944383 May 29 '22 at 10:23
  • But you do not need an access to `main`. API of OMNeT++ provides convenient method `initialize()` in which one can add own code that is run when a module is created. – Jerzy D. May 29 '22 at 15:24
  • Indeed, for VeinsInetApplicationBase that would be: virtual void initialize(int stage) override; – user18944383 May 29 '22 at 16:00
  • But the problem is that within my function I want to set up a TCP client (ONCE) and then read data from an external application (external TCP server). Based on this bitbucket code: https://bitbucket.org/sloankelly/youtube-source-repository/src/1d500703116a4e6ab79c03994e85eff20359cb15/cpp/networking/BarebonesClient/Barebones_Client/main.cpp?at=master when I create my "socket" object within the initialize function it is not known in my main function. Thus I thought a could write my own function which first initializes it and then runs in an endless loop like in the bitbucket example. – user18944383 May 29 '22 at 16:22
  • In C++ one can add a variable into a class declaration. Then this variable is visible in every method of this class, for example in `initialize()`. – Jerzy D. May 29 '22 at 16:28