SHARE
TWEET

zhelpers.h

Riskybiz Aug 25th, 2014 34 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  =========================================================================
  2.     zhelpers.h - ZeroMQ helpers for example applications
  3.  
  4.     Copyright (c) 1991-2010 iMatix Corporation and contributors
  5.  
  6.     This is free software; you can redistribute it and/or modify it under
  7.     the terms of the Lesser GNU General Public License as published by
  8.     the Free Software Foundation; either version 3 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This software is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14.     Lesser GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the Lesser GNU General Public License
  17.     along with this program. If not, see <http://www.gnu.org/licenses/>.
  18.     =========================================================================
  19. */
  20.  
  21. // Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
  22.  
  23.  
  24. #ifndef __ZHELPERS_HPP_INCLUDED__
  25. #define __ZHELPERS_HPP_INCLUDED__
  26.  
  27. //  Include a bunch of headers that we will need in the examples
  28.  
  29. #include <zmq.hpp>
  30.  
  31. #include <iostream>
  32. #include <iomanip>
  33. #include <string>
  34. #include <sstream>
  35.  
  36. #if (!defined(__WINDOWS__))
  37. #include <sys/time.h>
  38. #include <unistd.h>
  39. #include <pthread.h>
  40. #endif
  41. #include <time.h>
  42. #include <assert.h>
  43. #include <stdlib.h>        // random()  RAND_MAX
  44. #include <stdio.h>
  45. #include <stdarg.h>
  46. #include <signal.h>
  47.  
  48. //  Bring Windows MSVC up to C99 scratch
  49. #if (defined (__WINDOWS__))
  50.     typedef unsigned long ulong;
  51.     typedef unsigned int  uint;
  52.     typedef __int64 int64_t;
  53. #endif
  54.  
  55. //  Provide random number from 0..(num-1)
  56. #if (!defined(__WINDOWS__))
  57. #define within(num) (int) ((float) (num) * random () / (RAND_MAX + 1.0))
  58. #else
  59. #define within(num) (int) ((float) (num) * rand () / (RAND_MAX + 1.0))
  60. #endif
  61.  
  62. //  Receive 0MQ string from socket and convert into string
  63. static std::string
  64. s_recv (zmq::socket_t & socket) {
  65.  
  66.     zmq::message_t message;
  67.     socket.recv(&message);
  68.  
  69.     return std::string(static_cast<char*>(message.data()), message.size());
  70. }
  71.  
  72. //  Convert string to 0MQ string and send to socket
  73. static bool
  74. s_send (zmq::socket_t & socket, const std::string & string) {
  75.  
  76.     zmq::message_t message(string.size());
  77.     memcpy (message.data(), string.data(), string.size());
  78.  
  79.     bool rc = socket.send (message);
  80.     return (rc);
  81. }
  82.  
  83. //  Sends string as 0MQ string, as multipart non-terminal
  84. static bool
  85. s_sendmore (zmq::socket_t & socket, const std::string & string) {
  86.  
  87.     zmq::message_t message(string.size());
  88.     memcpy (message.data(), string.data(), string.size());
  89.  
  90.     bool rc = socket.send (message, ZMQ_SNDMORE);
  91.     return (rc);
  92. }
  93.  
  94. //  Receives all message parts from socket, prints neatly
  95. //
  96. static void
  97. s_dump (zmq::socket_t & socket)
  98. {
  99.     std::cout << "----------------------------------------" << std::endl;
  100.  
  101.     while (1) {
  102.         //  Process all parts of the message
  103.         zmq::message_t message;
  104.         socket.recv(&message);
  105.  
  106.         //  Dump the message as text or binary
  107.         int size = message.size();
  108.         std::string data(static_cast<char*>(message.data()), size);
  109.  
  110.         bool is_text = true;
  111.  
  112.         int char_nbr;
  113.         unsigned char byte;
  114.         for (char_nbr = 0; char_nbr < size; char_nbr++) {
  115.             byte = data [char_nbr];
  116.             if (byte < 32 || byte > 127)
  117.                 is_text = false;
  118.         }
  119.         std::cout << "[" << std::setfill('0') << std::setw(3) << size << "]";
  120.         for (char_nbr = 0; char_nbr < size; char_nbr++) {
  121.             if (is_text)
  122.                 std::cout << (char)data [char_nbr];
  123.             else
  124.                 std::cout << std::setfill('0') << std::setw(2)
  125.                    << std::hex << (unsigned int) data [char_nbr];
  126.         }
  127.         std::cout << std::endl;
  128.  
  129.         int more = 0;           //  Multipart detection
  130.         size_t more_size = sizeof (more);
  131.         socket.getsockopt (ZMQ_RCVMORE, &more, &more_size);
  132.         if (!more)
  133.             break;              //  Last message part
  134.     }
  135. }
  136.  
  137. //  Set simple random printable identity on socket
  138. //
  139. inline std::string
  140. s_set_id (zmq::socket_t & socket)
  141. {
  142.     std::stringstream ss;
  143.     ss << std::hex << std::uppercase
  144.        << std::setw(4) << std::setfill('0') << within (0x10000) << "-"
  145.        << std::setw(4) << std::setfill('0') << within (0x10000);
  146.     socket.setsockopt(ZMQ_IDENTITY, ss.str().c_str(), ss.str().length());
  147.     return ss.str();
  148. }
  149.  
  150. //  Report 0MQ version number
  151. //
  152. static void
  153. s_version (void)
  154. {
  155.     int major, minor, patch;
  156.     zmq_version (&major, &minor, &patch);
  157.     std::cout << "Current 0MQ version is " << major << "." << minor << "." << patch << std::endl;
  158. }
  159.  
  160. static void
  161. s_version_assert (int want_major, int want_minor)
  162. {
  163.     int major, minor, patch;
  164.     zmq_version (&major, &minor, &patch);
  165.     if (major < want_major
  166.     || (major == want_major && minor < want_minor)) {
  167.         std::cout << "Current 0MQ version is " << major << "." << minor << std::endl;
  168.         std::cout << "Application needs at least " << want_major << "." << want_minor
  169.               << " - cannot continue" << std::endl;
  170.         exit (EXIT_FAILURE);
  171.     }
  172. }
  173.  
  174. //  Return current system clock as milliseconds
  175. static int64_t
  176. s_clock (void)
  177. {
  178. #if (defined (__WINDOWS__))
  179.     SYSTEMTIME st;
  180.     GetSystemTime (&st);
  181.     return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
  182. #else
  183.     struct timeval tv;
  184.     gettimeofday (&tv, NULL);
  185.     return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
  186. #endif
  187. }
  188.  
  189. //  Sleep for a number of milliseconds
  190. static void
  191. s_sleep (int msecs)
  192. {
  193. #if (defined (__WINDOWS__))
  194.     Sleep (msecs);
  195. #else
  196.     struct timespec t;
  197.     t.tv_sec = msecs / 1000;
  198.     t.tv_nsec = (msecs % 1000) * 1000000;
  199.     nanosleep (&t, NULL);
  200. #endif
  201. }
  202.  
  203. static void
  204. s_console (const char *format, ...)
  205. {
  206.     time_t curtime = time (NULL);
  207.     struct tm *loctime = localtime (&curtime);
  208.     char *formatted = new char[20];
  209.     strftime (formatted, 20, "%y-%m-%d %H:%M:%S ", loctime);
  210.     printf ("%s", formatted);
  211.     delete[] formatted;
  212.  
  213.     va_list argptr;
  214.     va_start (argptr, format);
  215.     vprintf (format, argptr);
  216.     va_end (argptr);
  217.     printf ("\n");
  218. }
  219.  
  220. //  ---------------------------------------------------------------------
  221. //  Signal handling
  222. //
  223. //  Call s_catch_signals() in your application at startup, and then exit
  224. //  your main loop if s_interrupted is ever 1. Works especially well with
  225. //  zmq_poll.
  226.  
  227. static int s_interrupted = 0;
  228. static void s_signal_handler (int signal_value)
  229. {
  230.     s_interrupted = 1;
  231. }
  232.  
  233. static void s_catch_signals ()
  234. {
  235. #if (!defined(__WINDOWS__))
  236.     struct sigaction action;
  237.     action.sa_handler = s_signal_handler;
  238.     action.sa_flags = 0;
  239.     sigemptyset (&action.sa_mask);
  240.     sigaction (SIGINT, &action, NULL);
  241.     sigaction (SIGTERM, &action, NULL);
  242. #endif
  243. }
  244.  
  245. #endif
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top