[英]How to send the message between two nodes correctly in OMNeT++
我是 OMNeT++ 的新手,我在 C++ 方面没有太多经验。在我的 OMNeT++ 场景中,我有两个节点(来自 SUMO 场景)。 我想从 Python 发送一个node_indicator
(在本例中为 0),然后将一些消息从节点 0 发送到节点 1,然后将该消息返回到 Python。我正在尝试使用以下代码实现此目的:
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";
}
这是行不通的。 在 Python 中只返回 ''。 我的问题是我不明白编译器按什么顺序计算语句。 我期待以下顺序
communicate_with_python
message_exchange
onWSM
方法
message_to_python
变量中communicate_with_python
msgToClient
发送给Python实际上,我在控制台得到以下输出
...
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]
...
有人可以向我解释如何在我的 class 中调用这些方法吗? 还有我如何正确实施它。
谢谢!
您是否按照 TraciDemo11p 示例中给出的传统方式尝试过它。 您能否在这里解释一下使用 sockets 的需要。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.