@@ -49,6 +49,10 @@ class Concore{
4949 int communication_iport = 0 ; // iport refers to input port
5050 int communication_oport = 0 ; // oport refers to input port
5151
52+ #ifdef CONCORE_USE_ZMQ
53+ map<string, concore_base::ZeroMQPort*> zmq_ports;
54+ #endif
55+
5256 public:
5357 double delay = 1 ;
5458 int retrycount = 0 ;
@@ -107,6 +111,11 @@ class Concore{
107111 */
108112 ~Concore ()
109113 {
114+ #ifdef CONCORE_USE_ZMQ
115+ for (auto & kv : zmq_ports)
116+ delete kv.second ;
117+ zmq_ports.clear ();
118+ #endif
110119#ifdef __linux__
111120 // Detach the shared memory segment from the process
112121 if (communication_oport == 1 && sharedData_create != nullptr ) {
@@ -621,6 +630,117 @@ class Concore{
621630 }
622631 }
623632
633+ #ifdef CONCORE_USE_ZMQ
634+ /* *
635+ * @brief Registers a ZMQ port for use with read()/write().
636+ * @param port_name The ZMQ port name.
637+ * @param port_type "bind" or "connect".
638+ * @param address The ZMQ address.
639+ * @param socket_type_str The socket type string.
640+ */
641+ void init_zmq_port (string port_name, string port_type, string address, string socket_type_str) {
642+ if (zmq_ports.count (port_name)) return ;
643+ int sock_type = concore_base::zmq_socket_type_from_string (socket_type_str);
644+ if (sock_type == -1 ) {
645+ cerr << " init_zmq_port: unknown socket type '" << socket_type_str << " '" << endl;
646+ return ;
647+ }
648+ zmq_ports[port_name] = new concore_base::ZeroMQPort (port_type, address, sock_type);
649+ }
650+
651+ /* *
652+ * @brief Reads data from a ZMQ port. Strips simtime prefix, updates simtime.
653+ * @param port_name The ZMQ port name.
654+ * @param name The name of the file.
655+ * @param initstr The initial string.
656+ * @return a vector of double values
657+ */
658+ vector<double > read_ZMQ (string port_name, string name, string initstr) {
659+ auto it = zmq_ports.find (port_name);
660+ if (it == zmq_ports.end ()) {
661+ cerr << " read_ZMQ: port '" << port_name << " ' not initialized" << endl;
662+ return parser (initstr);
663+ }
664+ vector<double > inval = it->second ->recv_with_retry ();
665+ if (inval.empty ())
666+ inval = parser (initstr);
667+ if (inval.empty ()) return inval;
668+ simtime = simtime > inval[0 ] ? simtime : inval[0 ];
669+ s += port_name;
670+ inval.erase (inval.begin ());
671+ return inval;
672+ }
673+
674+ /* *
675+ * @brief Writes a vector of double values to a ZMQ port. Prepends simtime+delta.
676+ * @param port_name The ZMQ port name.
677+ * @param name The name of the file.
678+ * @param val The vector of double values to write.
679+ * @param delta The delta value (default: 0).
680+ */
681+ void write_ZMQ (string port_name, string name, vector<double > val, int delta=0 ) {
682+ auto it = zmq_ports.find (port_name);
683+ if (it == zmq_ports.end ()) {
684+ cerr << " write_ZMQ: port '" << port_name << " ' not initialized" << endl;
685+ return ;
686+ }
687+ val.insert (val.begin (), simtime + delta);
688+ it->second ->send_with_retry (val);
689+ // simtime must not be mutated here (issue #385).
690+ }
691+
692+ /* *
693+ * @brief Writes a string to a ZMQ port.
694+ * @param port_name The ZMQ port name.
695+ * @param name The name of the file.
696+ * @param val The string to write.
697+ * @param delta The delta value (default: 0).
698+ */
699+ void write_ZMQ (string port_name, string name, string val, int delta=0 ) {
700+ auto it = zmq_ports.find (port_name);
701+ if (it == zmq_ports.end ()) {
702+ cerr << " write_ZMQ: port '" << port_name << " ' not initialized" << endl;
703+ return ;
704+ }
705+ chrono::milliseconds timespan ((int )(2000 *delay));
706+ this_thread::sleep_for (timespan);
707+ it->second ->send_string_with_retry (val);
708+ }
709+
710+ /* *
711+ * @brief deviate the read to ZMQ communication protocol when port identifier is a string key.
712+ * @param port_name The ZMQ port name.
713+ * @param name The name of the file.
714+ * @param initstr The initial string.
715+ * @return
716+ */
717+ vector<double > read (string port_name, string name, string initstr) {
718+ return read_ZMQ (port_name, name, initstr);
719+ }
720+
721+ /* *
722+ * @brief deviate the write to ZMQ communication protocol when port identifier is a string key.
723+ * @param port_name The ZMQ port name.
724+ * @param name The name of the file.
725+ * @param val The vector of double values to write.
726+ * @param delta The delta value (default: 0).
727+ */
728+ void write (string port_name, string name, vector<double > val, int delta=0 ) {
729+ return write_ZMQ (port_name, name, val, delta);
730+ }
731+
732+ /* *
733+ * @brief deviate the write to ZMQ communication protocol when port identifier is a string key.
734+ * @param port_name The ZMQ port name.
735+ * @param name The name of the file.
736+ * @param val The string to write.
737+ * @param delta The delta value (default: 0).
738+ */
739+ void write (string port_name, string name, string val, int delta=0 ) {
740+ return write_ZMQ (port_name, name, val, delta);
741+ }
742+ #endif // CONCORE_USE_ZMQ
743+
624744 /* *
625745 * @brief Strips leading and trailing whitespace from a string.
626746 * @param str The input string.
0 commit comments