I am new to OMNeT++ and I do not have much experience in C++.
In my OMNeT++ scenario I have two nodes (coming from SUMO scenario). I want to send a node_indicator
from Python (in this case it is 0), then send some message from node 0 to node 1 and return that message back to Python. I am trying to achieve this with the following code:
Define_Module(veins::TraCIDemo11p);
void TraCIDemo11p::initialize(int stage)
{
DemoBaseApplLayer::initialize(stage);
if (stage == 0) {
sentMessage = false;
lastDroveAt = simTime();
currentSubscribedServiceId = -1;
}
this->communicate_with_python();
}
void TraCIDemo11p::communicate_with_python() {
// Prepare our context and socket
zmq::context_t context(1);
zmq::socket_t socket(context, ZMQ_REP);
socket.bind("tcp://*:5555");
zmq::message_t request;
// Wait for next request from client
socket.recv(request);
std::string requestMessage = std::string(static_cast<char *>(request.data()), request.size());
// Print out received message
EV << "Received from Python: " + requestMessage << std::endl; //requestMessage = 0
std::string msgToClient(" ");
this->message_exchange(stoi(requestMessage));
// Send reply back to client
msgToClient = message_to_python;
EV << "Returning Message back to Python" << "\n";
socket.send(zmq::message_t(msgToClient.data(), msgToClient.size()), zmq::send_flags::none);
}
void TraCIDemo11p::message_exchange(int node_indicator) {
// Send the message from node 0 to node 1
if (findHost()->getIndex()==node_indicator) {
sentMessage=true;
TraCIDemo11pMessage* wsm = new TraCIDemo11pMessage();
populateWSM(wsm);
std::string message = "Message from node 0 to node 1";
wsm->setDemoData(message.c_str());
EV << "Sending Now!" << "\n";
sendDown(wsm);
}
}
void TraCIDemo11p::onWSM(BaseFrame1609_4* frame)
{
TraCIDemo11pMessage* wsm = check_and_cast<TraCIDemo11pMessage*>(frame);
EV << "Message from node 0 received! " << wsm->getDemoData() << " on node " << findHost()->getFullName() << "\n";
if (strcmp("0", python_request.c_str()) == 0) {
message_to_python = wsm->getDemoData();
EV << "Assigned Message to Python!" << "\n";
}
It does not work. In Python only '' gets returned. My problem is that I do not understand in which sequence the compiler evaluates the statements. I expect the following sequence
- Method
communicate_with_python
- Receive the node indicator from Python
- Method
message_exchange
- Node 0 sends the message to node 1
- Method
onWSM
- Node 1 receives the message from node 0 and saves it to
message_to_python
variable
- Node 1 receives the message from node 0 and saves it to
- Method
communicate_with_python
- Assigns the traversed message to
msgToClient
and sends it to Python
- Assigns the traversed message to
In reality, I get the following outputs at my console
...
INFO (TraCIDemo11p)RSUExampleScenario.node[1].appl:Received from Python: 0
INFO (TraCIDemo11p)RSUExampleScenario.node[1].appl:Returning Message back to Python
...
INFO:Message from node 0 received! Message from node 0 to node 1 on node node[1]
...
INFO:Message from node 0 received! Message from node 0 to node 1 on node node[1]
...
Could somebody explain to me how the methods are called inside my class? And also how do I implement it correctly.
Thank you!