Guest User

test_concurrency_curve.cpp

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