Guest User

test_concurrency_curve.cpp

a guest
Oct 30th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.38 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
  3.  
  4.     This file is part of 0MQ.
  5.  
  6.     0MQ is free software; you can redistribute it and/or modify it under
  7.     the terms of the GNU Lesser 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.     0MQ 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.     GNU Lesser General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Lesser General Public License
  17.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. */
  19.  
  20. #include "testutil.hpp"
  21. #include "../include/zmq_utils.h"
  22. #ifdef HAVE_LIBSODIUM
  23. #include <sodium.h>
  24. #endif
  25.  
  26. //  Test keys from the zmq_curve man page
  27. // static char client_public [] = "Yne@$w-vo<fVvi]a<NY6T1ed:M$fCG*[IaLV{hID";
  28. // static char client_secret [] = "D:)Q[IlAW!ahhC2ac:9*A}h:p?([4%wOTJ%JR%cs";
  29. static char server_public [] = "rq:rM>}U?@Lns47E1%kR.o@n%FcmmsL/@{H8]yf7";
  30. static char server_secret [] = "JTKVSB%%)wK0E.X)V>+}o?pNmC{O&4W4b!Ni{Lh6";
  31.  
  32.  
  33.  
  34.  
  35. // Asynchronous client-to-server (DEALER to ROUTER) - pure libzmq
  36. //
  37. // While this example runs in a single process, that is to make
  38. // it easier to start and stop the example. Each task may have its own
  39. // context and conceptually acts as a separate process. To have this
  40. // behaviour, it is necessary to replace the inproc transport of the
  41. // control socket by a tcp transport.
  42.  
  43. // This is our client task
  44. // It connects to the server, and then sends a request once per second
  45. // It collects responses as they arrive, and it prints them out. We will
  46. // run several client tasks in parallel, each with a different random ID.
  47.  
  48. #define CONTENT_SIZE 13
  49. #define CONTENT_SIZE_MAX 32
  50. #define ID_SIZE 10
  51. #define ID_SIZE_MAX 32
  52. #define QT_WORKERS  5
  53. #define QT_CLIENTS  30
  54. #define is_verbose 0
  55.  
  56. static void
  57. client_task (void *args)
  58. {
  59.     // generate keys
  60.     uint8_t client_pub[32], client_sec[32];
  61.     int rc = crypto_box_keypair (client_pub, client_sec);
  62.  
  63.     void *ctx = zmq_ctx_new (); // if we want our own context, we shall use tcp instead of inproc for the control socket
  64.     assert (ctx);
  65.     void *client = zmq_socket (ctx, ZMQ_DEALER);
  66.     assert (client);
  67.     rc = zmq_setsockopt (client, ZMQ_CURVE_SERVERKEY, server_public, 40);
  68.     assert (rc == 0);
  69.     rc = zmq_setsockopt (client, ZMQ_CURVE_PUBLICKEY, client_pub, 32); // client_public, 40); //
  70.     assert (rc == 0);
  71.     rc = zmq_setsockopt (client, ZMQ_CURVE_SECRETKEY, client_sec, 32); // client_secret, 40); //
  72.     assert (rc == 0);
  73.  
  74.     // Control socket receives terminate command from main over inproc
  75.     void *control = zmq_socket (ctx, ZMQ_SUB);
  76.     assert (control);
  77.     rc = zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0);
  78.     assert (rc == 0);
  79.     rc = zmq_connect (control, "tcp://127.0.0.1:9998");
  80.     assert (rc == 0);
  81.  
  82.     char content [CONTENT_SIZE_MAX];
  83.     // Set random identity to make tracing easier
  84.     char identity [ID_SIZE];
  85.     sprintf (identity, "%04X-%04X", rand() % 0xFFFF, rand() % 0xFFFF);
  86.     rc = zmq_setsockopt (client, ZMQ_IDENTITY, identity, ID_SIZE); // includes '\0' as an helper for printf
  87.     assert (rc == 0);
  88.     rc = zmq_connect (client, "tcp://127.0.0.1:9999");
  89.     assert (rc == 0);
  90.  
  91.     zmq_pollitem_t items [] = { { client, 0, ZMQ_POLLIN, 0 }, { control, 0, ZMQ_POLLIN, 0 } };
  92.     int request_nbr = 0;
  93.     bool run = true;
  94.     while (run) {
  95.         // Tick once per 200 ms, pulling in arriving messages
  96.         int centitick;
  97.         for (centitick = 0; centitick < 20; centitick++) {
  98.             zmq_poll (items, 2, 10);
  99.             if (items [0].revents & ZMQ_POLLIN) {
  100.                 int rcvmore;
  101.                 size_t sz = sizeof (rcvmore);
  102.                 rc = zmq_recv (client, content, CONTENT_SIZE_MAX, 0);
  103.                 assert (rc == CONTENT_SIZE);
  104.                 if (is_verbose) printf("client receive - identity = %s    content = %s\n", identity, content);
  105.                 //  Check that message is still the same
  106.                 assert (memcmp (content, "request #", 9) == 0);
  107.                 rc = zmq_getsockopt (client, ZMQ_RCVMORE, &rcvmore, &sz);
  108.                 assert (rc == 0);
  109.                 assert (!rcvmore);
  110.             }
  111.             if (items [1].revents & ZMQ_POLLIN) {
  112.                 rc = zmq_recv (control, content, CONTENT_SIZE_MAX, 0);
  113.                 if (is_verbose) printf("client receive - identity = %s    command = %s\n", identity, content);
  114.                 if (memcmp (content, "TERMINATE", 10) == 0) {
  115.                     run = false;
  116.                     break;
  117.                 }
  118.             }
  119.         }
  120.         sprintf(content, "request #%03d", ++request_nbr); // CONTENT_SIZE
  121.         rc = zmq_send (client, content, CONTENT_SIZE, 0);
  122.         assert (rc == CONTENT_SIZE);
  123.     }
  124.  
  125.     rc = zmq_close (client);
  126.     assert (rc == 0);
  127.     rc = zmq_close (control);
  128.     assert (rc == 0);
  129.     rc = zmq_ctx_term (ctx);
  130.     assert (rc == 0);
  131. }
  132.  
  133. // This is our server task.
  134. // It uses the multithreaded server model to deal requests out to a pool
  135. // of workers and route replies back to clients. One worker can handle
  136. // one request at a time but one client can talk to multiple workers at
  137. // once.
  138.  
  139. static void server_worker (void *ctx);
  140.  
  141. void
  142. server_task (void *args)
  143. {
  144.     void *ctx = zmq_ctx_new (); // if we want our own context, we shall use tcp instead of inproc for the control socket
  145.     assert (ctx);
  146.  
  147.     // Frontend socket talks to clients over TCP
  148.     void *frontend = zmq_socket (ctx, ZMQ_ROUTER);
  149.     assert (frontend);
  150.     int as_server = 1;
  151.     int rc = zmq_setsockopt (frontend, ZMQ_CURVE_SERVER, &as_server, sizeof (int));
  152.     assert (rc == 0);
  153.     rc = zmq_setsockopt (frontend, ZMQ_CURVE_SECRETKEY, server_secret, 40);
  154.     assert (rc == 0);
  155.     rc = zmq_bind (frontend, "tcp://127.0.0.1:9999");
  156.     assert (rc == 0);
  157.  
  158.     // Backend socket talks to workers over inproc
  159.     void *backend = zmq_socket (ctx, ZMQ_DEALER);
  160.     assert (backend);
  161.     rc = zmq_bind (backend, "inproc://backend");
  162.     assert (rc == 0);
  163.  
  164.     // Control socket receives terminate command from main over inproc
  165.     void *control = zmq_socket (ctx, ZMQ_SUB);
  166.     assert (control);
  167.     rc = zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0);
  168.     assert (rc == 0);
  169.     rc = zmq_connect (control, "tcp://127.0.0.1:9998");
  170.     assert (rc == 0);
  171.  
  172.     // Launch pool of worker threads, precise number is not critical
  173.     int thread_nbr;
  174.     void* threads [5];
  175.     for (thread_nbr = 0; thread_nbr < QT_WORKERS; thread_nbr++)
  176.         threads[thread_nbr] = zmq_threadstart (&server_worker, ctx);
  177.  
  178.     // Connect backend to frontend via a proxy
  179.     zmq_proxy_steerable (frontend, backend, NULL, control);
  180.  
  181.     for (thread_nbr = 0; thread_nbr < QT_WORKERS; thread_nbr++)
  182.         zmq_threadclose (threads[thread_nbr]);
  183.  
  184.     rc = zmq_close (frontend);
  185.     assert (rc == 0);
  186.     rc = zmq_close (backend);
  187.     assert (rc == 0);
  188.     rc = zmq_close (control);
  189.     assert (rc == 0);
  190.     rc = zmq_ctx_term (ctx);
  191.     assert (rc == 0);
  192. }
  193.  
  194. // Each worker task works on one request at a time and sends a random number
  195. // of replies back, with random delays between replies:
  196. // The comments in the first column, if suppressed, makes it a poller version
  197.  
  198. static void
  199. server_worker (void *ctx)
  200. {
  201.     void *worker = zmq_socket (ctx, ZMQ_DEALER);
  202.     assert (worker);
  203.     int rc = zmq_connect (worker, "inproc://backend");
  204.     assert (rc == 0);
  205.  
  206.     // Control socket receives terminate command from main over inproc
  207.     void *control = zmq_socket (ctx, ZMQ_SUB);
  208.     assert (control);
  209.     rc = zmq_setsockopt (control, ZMQ_SUBSCRIBE, "", 0);
  210.     assert (rc == 0);
  211.     rc = zmq_connect (control, "tcp://127.0.0.1:9998");
  212.     assert (rc == 0);
  213.  
  214.     char content [CONTENT_SIZE_MAX]; // bigger than what we need to check that
  215.     char identity [ID_SIZE_MAX];     // the size received is the size sent
  216.  
  217. //  zmq_pollitem_t items [] = { { worker, 0, ZMQ_POLLIN, 0 }, { control, 0, ZMQ_POLLIN, 0 } }; // POLLING
  218.     bool run = true;
  219.     while (run) {
  220. //      zmq_poll (items, 2, 10); // POLLING
  221. //      if (items [1].revents & ZMQ_POLLIN) { // POLLING
  222.             rc = zmq_recv (control, content, CONTENT_SIZE_MAX, ZMQ_DONTWAIT); // usually, rc == -1 (no message)
  223.             if (rc > 0) {
  224.                 if (is_verbose) printf("server_worker receives command = %s\n", content);
  225.                 if (memcmp (content, "TERMINATE", 10) == 0)
  226.                     run = false;
  227.             }
  228. //      } // POLLING
  229.  
  230. //      if (items [0].revents & ZMQ_POLLIN) { // POLLING
  231.             // The DEALER socket gives us the reply envelope and message
  232.             rc = zmq_recv (worker, identity, ID_SIZE_MAX, ZMQ_DONTWAIT); // if we don't poll, we have to use ZMQ_DONTWAIT, if we poll, we can block-receive with 0
  233. //          if (rc != -1) // DEBUG
  234. //              printf("server receives something\n"); // DEBUG
  235.             if (rc == ID_SIZE) {
  236.                 rc = zmq_recv (worker, content, CONTENT_SIZE_MAX, 0);
  237.                 assert (rc == CONTENT_SIZE);
  238.                 if (is_verbose) printf("server receive - identity = %s    content = %s\n", identity, content);
  239.  
  240.                 // Send 0..4 replies back
  241.                 int reply, replies = rand() % 5;
  242.                 for (reply = 0; reply < replies; reply++) {
  243.                     // Sleep for some fraction of a second
  244.                     msleep (rand () % 10 + 1);
  245.                     //  Send message from server to client
  246.                     rc = zmq_send (worker, identity, ID_SIZE, ZMQ_SNDMORE);
  247.                     assert (rc == ID_SIZE);
  248.                     rc = zmq_send (worker, content, CONTENT_SIZE, 0);
  249.                     assert (rc == CONTENT_SIZE);
  250.                 }
  251.             }
  252. //      } // POLLING
  253.     }
  254.     rc = zmq_close (worker);
  255.     assert (rc == 0);
  256.     rc = zmq_close (control);
  257.     assert (rc == 0);
  258. }
  259.  
  260. // The main thread simply starts several clients and a server, and then
  261. // waits for the server to finish.
  262.  
  263. int main (void)
  264. {
  265.     setup_test_environment ();
  266.  
  267.     void *ctx = zmq_ctx_new ();
  268.     assert (ctx);
  269.     // Control socket receives terminate command from main over inproc
  270.     void *control = zmq_socket (ctx, ZMQ_PUB);
  271.     assert (control);
  272.     int rc = zmq_bind (control, "tcp://127.0.0.1:9998");
  273.     assert (rc == 0);
  274.  
  275.     void* threads [QT_CLIENTS + 1];
  276.     for (int i = 0; i < QT_CLIENTS; i++)
  277.     {
  278.         threads[i] = zmq_threadstart  (&client_task, NULL);
  279.     }
  280.     threads[QT_CLIENTS] = zmq_threadstart  (&server_task, NULL);
  281.     msleep (500); // Run for 500 ms then quit
  282.  
  283.     rc = zmq_send (control, "TERMINATE", 10, 0);
  284.     assert (rc == 10);
  285.  
  286.     // clean everything
  287.     rc = zmq_close (control);
  288.     assert (rc == 0);
  289.     //msleep (1000); // not sure it is usefull
  290.  
  291.     for (int i = 0; i < QT_CLIENTS + 1; i++)
  292.         zmq_threadclose (threads[i]);
  293.     rc = zmq_ctx_term (ctx);
  294.     assert (rc == 0);
  295.     return 0;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment