Guest User

Untitled

a guest
Feb 24th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. // --------------------------------------------------------------------------
  2. // Example Zyre distributed chat application
  3. //
  4. // Copyright (c) 2010-2014 The Authors
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a
  7. // copy of this software and associated documentation files (the "Software"),
  8. // to deal in the Software without restriction, including without limitation
  9. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. // and/or sell copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. // DEALINGS IN THE SOFTWARE.
  23. // --------------------------------------------------------------------------
  24. //
  25. // build with gcc -o chat-gossip chat-gossip.c -l zyre -l czmq -l zmq
  26.  
  27. #include "zyre.h"
  28.  
  29.  
  30. static void
  31. chat_slave_actor (zsock_t *pipe, void *args)
  32. {
  33. zyre_t *node = zyre_new ((char *) args);
  34. if (!node)
  35. return; // Could not create new node
  36.  
  37. zyre_set_verbose( node );
  38. zyre_set_endpoint ( node, "ipc://%s", (char *) args);
  39.  
  40. zyre_gossip_connect( node, "ipc://gossip-hub");
  41.  
  42. zyre_start (node);
  43. zyre_join (node, "CHAT");
  44. zsock_signal (pipe, 0); // Signal "ready" to caller
  45.  
  46. bool terminated = false;
  47. zpoller_t *poller = zpoller_new (pipe, zyre_socket (node), NULL);
  48. while (!terminated) {
  49. void *which = zpoller_wait (poller, -1);
  50. if (which == pipe) {
  51. zmsg_t *msg = zmsg_recv (which);
  52. if (!msg)
  53. break; // Interrupted
  54.  
  55. char *command = zmsg_popstr (msg);
  56. if (streq (command, "$TERM"))
  57. terminated = true;
  58. else
  59. if (streq (command, "SHOUT")) {
  60. char *string = zmsg_popstr (msg);
  61. zyre_shouts (node, "CHAT", "%s", string);
  62. }
  63. else {
  64. puts ("E: invalid message to actor");
  65. assert (false);
  66. }
  67. free (command);
  68. zmsg_destroy (&msg);
  69. }
  70. else
  71. if (which == zyre_socket (node)) {
  72. zmsg_t *msg = zmsg_recv (which);
  73. char *event = zmsg_popstr (msg);
  74. char *peer = zmsg_popstr (msg);
  75. char *name = zmsg_popstr (msg);
  76. char *group = zmsg_popstr (msg);
  77. char *message = zmsg_popstr (msg);
  78.  
  79. if (streq (event, "ENTER"))
  80. printf ("%s has joined the chat\n", name);
  81. else
  82. if (streq (event, "EXIT"))
  83. printf ("%s has left the chat\n", name);
  84. else
  85. if (streq (event, "SHOUT"))
  86. printf ("%s: %s\n", name, message);
  87. else
  88. if (streq (event, "EVASIVE"))
  89. printf ("%s is being evasive\n", name);
  90.  
  91. free (event);
  92. free (peer);
  93. free (name);
  94. free (group);
  95. free (message);
  96. zmsg_destroy (&msg);
  97. }
  98. }
  99. zpoller_destroy (&poller);
  100. zyre_stop (node);
  101. zclock_sleep (100);
  102. zyre_destroy (&node);
  103. }
  104.  
  105.  
  106. static void
  107. chat_master_actor (zsock_t *pipe, void *args)
  108. {
  109. zyre_t *node = zyre_new ((char *) args);
  110. if (!node)
  111. return; // Could not create new node
  112.  
  113. //zyre_set_verbose( node );
  114. zyre_set_endpoint ( node, "ipc://%s", (char *) args);
  115. zyre_gossip_bind ( node, "ipc://gossip-hub");
  116.  
  117. zyre_start (node);
  118. zyre_join (node, "CHAT");
  119. zsock_signal (pipe, 0); // Signal "ready" to caller
  120.  
  121. bool terminated = false;
  122. zpoller_t *poller = zpoller_new (pipe, zyre_socket (node), NULL);
  123. while (!terminated) {
  124. void *which = zpoller_wait (poller, -1);
  125. if (which == pipe) {
  126. zmsg_t *msg = zmsg_recv (which);
  127. if (!msg)
  128. break; // Interrupted
  129.  
  130. char *command = zmsg_popstr (msg);
  131. if (streq (command, "$TERM"))
  132. terminated = true;
  133. else
  134. if (streq (command, "SHOUT")) {
  135. char *string = zmsg_popstr (msg);
  136. zyre_shouts (node, "CHAT", "%s", string);
  137. }
  138. else {
  139. puts ("E: invalid message to actor");
  140. assert (false);
  141. }
  142. free (command);
  143. zmsg_destroy (&msg);
  144. }
  145. else
  146. if (which == zyre_socket (node)) {
  147. zmsg_t *msg = zmsg_recv (which);
  148. char *event = zmsg_popstr (msg);
  149. char *peer = zmsg_popstr (msg);
  150. char *name = zmsg_popstr (msg);
  151. char *group = zmsg_popstr (msg);
  152. char *message = zmsg_popstr (msg);
  153.  
  154. if (streq (event, "ENTER"))
  155. printf ("%s has joined the chat\n", name);
  156. else
  157. if (streq (event, "EXIT"))
  158. printf ("%s has left the chat\n", name);
  159. else
  160. if (streq (event, "SHOUT"))
  161. printf ("%s: %s\n", name, message);
  162. else
  163. if (streq (event, "EVASIVE"))
  164. printf ("%s is being evasive\n", name);
  165.  
  166. free (event);
  167. free (peer);
  168. free (name);
  169. free (group);
  170. free (message);
  171. zmsg_destroy (&msg);
  172. }
  173. }
  174. zpoller_destroy (&poller);
  175. zyre_stop (node);
  176. zclock_sleep (100);
  177. zyre_destroy (&node);
  178. }
  179.  
  180. int
  181. main (int argc, char *argv [])
  182. {
  183. zactor_t *actor;
  184. if (argc == 2) {
  185. actor = zactor_new (chat_slave_actor, argv [1]);
  186. }
  187. else
  188. if ( argc > 2 && streq (argv[1], "-m" ) ) {
  189. actor = zactor_new (chat_master_actor, argv [2]);
  190. }
  191. else {
  192. puts ("syntax: ./chat [-m] myname");
  193. puts (" -m: start as master, only one node needs to be master");
  194. exit (0);
  195. }
  196. assert (actor);
  197.  
  198. while (!zsys_interrupted) {
  199. char message [1024];
  200. if (!fgets (message, 1024, stdin))
  201. break;
  202. message [strlen (message) - 1] = 0; // Drop the trailing linefeed
  203. zstr_sendx (actor, "SHOUT", message, NULL);
  204. }
  205. zactor_destroy (&actor);
  206. return 0;
  207. }
Add Comment
Please, Sign In to add comment