I’m working on a project where I need to connect my OMNeT++ simulator (version 4.6) with a Python-based machine learning agent while both are running. My setup uses the INET framework for network simulation.
// Example OMNeT++ module sending data
void NetworkController::transmitMetrics() {
double linkQuality = calculateLinkQuality();
cMessage *dataMsg = new cMessage("metrics");
dataMsg->addPar("snr") = linkQuality;
send(dataMsg, "pythonInterface");
}
# Python agent receiving simulation data
class NetworkAgent:
def receive_simulation_data(self, snr_values, node_positions):
# Process incoming data from OMNeT++
action = self.model.predict(snr_values)
return self.generate_control_signal(action)
Basically, my simulator needs to send network metrics like signal quality and device positions to the Python agent. The agent then processes this info using machine learning and sends back control commands to optimize network performance. How can I establish this bidirectional communication between the two running processes?
I’d go with shared memory for this. Had a similar project last year - needed real-time communication between C++ simulation and Python ML models.
Create a shared memory segment in your OMNeT++ module using boost::interprocess or POSIX shared memory. Write metrics to specific memory locations with a simple protocol.
// In your NetworkController
void NetworkController::transmitMetrics() {
SharedMemoryWriter* writer = getSystemModule()->getSubmodule("sharedMem");
writer->writeMetric("snr", linkQuality);
writer->writeMetric("timestamp", simTime().dbl());
writer->signalUpdate();
}
Python side: use mmap to read from the same memory segment. You’ll need simple synchronization - a flag or semaphore so Python knows when new data’s available.
I got microsecond latency vs millisecond delays with sockets. For ML feedback loops, that timing difference matters.
Watch out though - handle Python process crashes or OMNeT++ might hang waiting for responses that never come.
i think zero mq is the way to go. i tried it with omnet++ 4.6 and it was awesome. just set up a zmq socket in ur NetworkController, bind it to a port, and connect from python. way simpler than files or pipes!