GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve python_zmq_communication · hmenke/cpp-patterns@3229bc2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • python_zmq_communication
    • CMakeLists.txt
    • communication.h
    • server.cpp
    • server.h
    • test.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎python_zmq_communication/CMakeLists.txt‎

Copy file name to clipboardExpand all lines: python_zmq_communication/CMakeLists.txt
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic")
1414

1515
add_library(backend SHARED ${SOURCE_FILES})
1616

17-
# Find ZMQ
17+
# Find ZMQ and Boost
1818

1919
find_library(ZMQ_LIBRARY zmq)
2020
list(APPEND LIBRARIES ${ZMQ_LIBRARY})
2121

22+
find_package(Boost REQUIRED)
23+
include_directories(${Boost_INCLUDE_DIRS})
24+
list(APPEND LIBRARIES ${Boost_LIBRARIES})
25+
2226
target_link_libraries(backend ${LIBRARIES})
2327

2428
# Use SWIG
Collapse file

‎python_zmq_communication/communication.h‎

Copy file name to clipboardExpand all lines: python_zmq_communication/communication.h
+30-24Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#include <memory>
44
#include <functional>
5-
#include <cstdlib>
6-
#include <cstdarg>
5+
#include <string>
76
#include <zmq.h>
87

8+
#include <boost/format.hpp>
99

1010
template < typename T >
1111
struct cptr
@@ -70,7 +70,7 @@ class error : public std::exception
7070
* used like a regular %socket pointer with the C API. For example
7171
* the `zmq_sendmsg` is not implemented but can be used nevertheless.
7272
* \code
73-
* if ( zmq_sendmsg(socket, msg, flags) != 0 )
73+
* if ( zmq_sendmsg(socket, msg, flags) == -1 )
7474
* throw zmq::error();
7575
* \endcode
7676
* When using the C API directly, errors have to be checked explicitly!
@@ -79,7 +79,7 @@ class error : public std::exception
7979
*/
8080
class socket
8181
{
82-
std::unique_ptr<void, std::function<void(void*)>> m_socket;
82+
std::unique_ptr < void, std::function < void(void*) > > m_socket;
8383
public:
8484
/** \brief Constructor
8585
*
@@ -89,7 +89,7 @@ class socket
8989
* \param[in] new_socket pre-allocated ZeroMQ socket
9090
* \throws zmq::error
9191
*/
92-
explicit socket(void * new_socket) throw(zmq::error)
92+
explicit socket(void * new_socket)
9393
: m_socket( new_socket, zmq_close )
9494
{
9595
if ( !new_socket )
@@ -112,9 +112,9 @@ class socket
112112
* \param[in] endpoint string with transport protocal and address
113113
* \throws zmq::error
114114
*/
115-
void bind(char const *endpoint) const throw(zmq::error)
115+
void bind(char const *endpoint) const
116116
{
117-
if ( zmq_bind(*this, endpoint) != 0 )
117+
if ( zmq_bind(*this, endpoint) == -1 )
118118
throw error();
119119
}
120120

@@ -126,31 +126,36 @@ class socket
126126
* \param[in] endpoint string with transport protocal and address
127127
* \throws zmq::error
128128
*/
129-
void connect(char const *endpoint) const throw(zmq::error)
129+
void connect(char const *endpoint) const
130130
{
131-
if ( zmq_connect(*this, endpoint) != 0 )
131+
if ( zmq_connect(*this, endpoint) == -1 )
132132
throw error();
133133
}
134-
134+
135135
/** \brief Send a message part on a socket
136136
*
137137
* The send() function shall queue a message created from its
138138
* printf-style arguments.
139139
*
140-
* \param[in] fmt printf-style string for argument formatting
141-
* \param[in] ... variadic arguments
140+
* \param[in] fmt printf-style string for argument formatting
141+
* \param[in] args variadic arguments
142+
* \returns number of bytes written (without zero terminator)
142143
*/
143-
void send(char const *fmt, ...) const __attribute__((format (printf, 2, 3)));
144-
void send(char const *fmt, ...)
144+
template < typename... Args >
145+
int send(char const *fmt, Args&&... args)
145146
{
146-
va_list args;
147-
va_start(args, fmt);
148-
cptr<char> tmp;
149-
int len = vasprintf(&tmp.ptr, fmt, args);
150-
if ( len == -1 )
151-
throw std::bad_alloc();
152-
if ( zmq_send(*this, tmp, len+1, 0) == -1 )
147+
boost::format formatter(fmt);
148+
149+
// This is horrible. In C++17 we have the binary fold expression
150+
using expander = int[];
151+
(void) expander { 0, (void(formatter % std::forward<Args>(args)), 0)... };
152+
153+
std::string tmp = formatter.str();
154+
int len = tmp.size();
155+
156+
if ( zmq_send(*this, tmp.c_str(), len+1, 0) == -1 )
153157
throw error();
158+
return len;
154159
}
155160
};
156161

@@ -162,9 +167,9 @@ class socket
162167
* implemented. The context object implicitly behaves like a pointer
163168
* to void which is what ZeroMQ expects. Thus a context object can be
164169
* used like a regular %context pointer with the C API. For example
165-
* the `zmq_sendmsg` is not implemented but can be used nevertheless.
170+
* the `zmq_ctx_set` is not implemented but can be used nevertheless.
166171
* \code
167-
* if ( zmq_ctx_set(context, ZMQ_IPV6, 1) != 0 )
172+
* if ( zmq_ctx_set(context, ZMQ_IPV6, 1) == -1 )
168173
* throw zmq::error();
169174
* \endcode
170175
* When using the C API directly, errors have to be checked explicitly!
@@ -173,7 +178,7 @@ class socket
173178
*/
174179
class context
175180
{
176-
std::unique_ptr<void, std::function<int(void*)>> m_context;
181+
std::unique_ptr < void, std::function < int(void*) > > m_context;
177182
public:
178183
/** \brief Constructor */
179184
context() noexcept : m_context( zmq_ctx_new(), zmq_ctx_term ) {}
@@ -195,6 +200,7 @@ class context
195200
*/
196201
zmq::socket socket(int type) const
197202
{
203+
/** errors of `zmq_socket` are handled in the zmq::socket constructor */
198204
return zmq::socket( zmq_socket(*this, type) );
199205
}
200206
};
Collapse file

‎python_zmq_communication/server.cpp‎

Copy file name to clipboardExpand all lines: python_zmq_communication/server.cpp
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#include "communication.h"
22
#include "server.h"
3+
4+
#include <chrono>
35
#include <iostream>
4-
#include <unistd.h>
6+
#include <thread>
57

6-
void perform(int nseconds)
8+
void perform(int nseconds, std::string const& endpoint)
79
{
810
// Prepare our context and socket
911
zmq::context context;
1012
zmq::socket publisher = context.socket(ZMQ_PUB);
11-
publisher.connect("tcp://localhost:5556");
13+
publisher.connect(endpoint.c_str());
1214

1315
for (int i = 0; i < 5; ++i)
1416
{
15-
sleep(nseconds);
17+
std::this_thread::sleep_for(std::chrono::seconds{nseconds});
1618

1719
publisher.send("%1d", i);
1820
}
Collapse file

‎python_zmq_communication/server.h‎

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#pragma once
22

3-
void perform(int);
3+
void perform(int, std::string const&);
Collapse file

‎python_zmq_communication/test.py‎

Copy file name to clipboardExpand all lines: python_zmq_communication/test.py
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
context = zmq.Context()
66
socket = context.socket(zmq.SUB)
7-
socket.bind("tcp://*:5556")
7+
socket.bind("tcp://*:*")
88
socket.setsockopt_string(zmq.SUBSCRIBE, u"")
9+
endpoint = socket.getsockopt_string(zmq.LAST_ENDPOINT)
910

10-
t = multiprocessing.Process(target=ex.perform, args=[1])
11+
print("Binding via {}".format(endpoint))
12+
13+
t = multiprocessing.Process(target=ex.perform, args=(1,endpoint))
1114
t.start()
1215

1316
string = ""

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.