24 #ifndef __ZHELPERS_HPP_INCLUDED__
25 #define __ZHELPERS_HPP_INCLUDED__
47 #if (defined (__WINDOWS__))
48 typedef unsigned long ulong;
49 typedef unsigned int uint;
50 typedef __int64 int64_t;
54 #define within(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
61 socket.
recv(&message);
63 return std::string(static_cast<char*>(message.
data()), message.
size());
71 memcpy(message.data(),
string.data(),
string.size());
75 rc = socket.
send(message);
84 s_sendmore (
zmq::socket_t & socket,
const std::string &
string) {
87 memcpy(message.data(),
string.data(),
string.size());
89 bool rc = socket.
send(message, ZMQ_SNDMORE);
98 std::cout <<
"----------------------------------------" <<
std::endl;
104 socket.
recv(&message);
107 int size = message.
size();
108 std::string
data(static_cast<char*>(message.
data()), size);
114 for (char_nbr = 0; char_nbr < size; char_nbr++) {
118 if (((
unsigned char)
data [char_nbr]) < 32 || ((
unsigned char)
data [char_nbr]) > 127)
122 std::cout <<
"[" << std::setfill(
'0') << std::setw(3) << size <<
"]";
124 for (char_nbr = 0; char_nbr < size; char_nbr++) {
126 std::cout << (char)
data [char_nbr];
128 std::cout << std::setfill(
'0') << std::setw(2)
129 << std::hex << (
unsigned int)
data [char_nbr];
135 size_t more_size =
sizeof (more);
136 socket.
getsockopt(ZMQ_RCVMORE, &more, &more_size);
148 std::stringstream ss;
149 ss << std::hex << std::uppercase
150 << std::setw(4) << std::setfill(
'0') <<
within (0x10000) <<
"-"
151 << std::setw(4) << std::setfill(
'0') <<
within (0x10000);
152 socket.
setsockopt(ZMQ_IDENTITY, ss.str().c_str(), ss.str().length());
161 int major, minor, patch;
162 zmq_version (&major, &minor, &patch);
163 std::cout <<
"Current 0MQ version is " << major <<
"." << minor <<
"." << patch <<
std::endl;
167 s_version_assert (
int want_major,
int want_minor)
169 int major, minor, patch;
170 zmq_version (&major, &minor, &patch);
171 if (major < want_major
172 || (major == want_major && minor < want_minor)) {
173 std::cout <<
"Current 0MQ version is " << major <<
"." << minor <<
std::endl;
174 std::cout <<
"Application needs at least " << want_major <<
"." << want_minor
184 #if (defined (__WINDOWS__))
187 return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
190 gettimeofday (&tv, NULL);
194 return (int64_t) (tv.tv_sec * 1000LL + tv.tv_usec / 1000LL);
202 #if (defined (__WINDOWS__))
206 t.tv_sec = msecs / 1000;
207 t.tv_nsec = (msecs % 1000) * 1000000;
208 nanosleep (&t, NULL);
213 s_console (
const char *format, ...)
215 time_t curtime =
time (NULL);
216 struct tm *loctime = localtime (&curtime);
217 char *formatted =
new char[20];
218 strftime (formatted, 20,
"%y-%m-%d %H:%M:%S ", loctime);
219 printf (
"%s", formatted);
223 va_start (argptr, format);
224 vprintf (format, argptr);
236 static int s_interrupted = 0;
237 static void s_signal_handler (
int signal_value)
242 static void s_catch_signals ()
244 struct sigaction action;
245 action.sa_handler = s_signal_handler;
247 sigemptyset (&action.sa_mask);
248 sigaction (SIGINT, &action, NULL);
249 sigaction (SIGTERM, &action, NULL);