device_demo.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. Copyright 2016 Garrett D'Amore <garrett@damore.org>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"),
  5. to deal in the Software without restriction, including without limitation
  6. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. and/or sell copies of the Software, and to permit persons to whom
  8. the Software is furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included
  10. in all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  14. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  16. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  17. IN THE SOFTWARE.
  18. "nanomsg" is a trademark of Martin Sustrik
  19. */
  20. /* This program serves as an example for how to write a simple device service, using
  21. a rendezvous. This works by having the program support three modes. The protocol is
  22. is REQ/REP, where the REQ is a name, and the REP is a greeting based on the name, and
  23. an instance number (we use the process ID) and time of day.
  24. We provide a rendezvous server running the device code, where servers and clients can
  25. connect. Both sides of the device are in bind mode, and both servers and clients run
  26. in connect mode. This lets us support many servers and clients simultaneously.
  27. For example, if I want to have servers rendezvous at port 5554 and clients at port 5555:
  28. % ./device_demo -d tcp://127.0.0.1:5554 tcp://127.0.0.1:5555 &
  29. % ./device_demo -s tcp://127.0.0.1:5554 &
  30. % ./device_demo -c tcp://127.0.0.1:5555 Garrett
  31. Good morning, Garrett.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <time.h>
  37. #include <unistd.h>
  38. #include <nanomsg/nn.h>
  39. #include <nanomsg/reqrep.h>
  40. /* The server runs forever. */
  41. int server(const char *url)
  42. {
  43. int fd;
  44. /* Create the socket. */
  45. fd = nn_socket (AF_SP, NN_REP);
  46. if (fd < 0) {
  47. fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
  48. return (-1);
  49. }
  50. /* Connect to the URL. This will connect to the address and listen
  51. synchronously; new clients will be accepted asynchronously
  52. without further action from the calling program. */
  53. if (nn_connect (fd, url) < 0) {
  54. fprintf (stderr, "nn_connect: %s\n", nn_strerror (nn_errno ()));
  55. nn_close (fd);
  56. return (-1);
  57. }
  58. /* Now we can just process results. Note that there is no explicit
  59. accept required. We just receive a request, and reply to it.
  60. Its important to note that we must not issue two receives in a
  61. row without replying first, or the following receive(s) will
  62. cancel any unreplied requests. */
  63. for (;;) {
  64. char username[128];
  65. char greeting[128];
  66. time_t secs;
  67. struct tm *now;
  68. char *daytime;
  69. int rc;
  70. char *fmt;
  71. rc = nn_recv (fd, username, sizeof (username), 0);
  72. if (rc < 0) {
  73. /* Any error here is unexpected. */
  74. fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ()));
  75. break;
  76. }
  77. secs = time (NULL);
  78. now = localtime (&secs);
  79. if (now->tm_hour < 12) {
  80. daytime = "morning";
  81. } else if (now->tm_hour < 17) {
  82. daytime = "afternoon";
  83. } else if (now->tm_hour < 20) {
  84. daytime = "evening";
  85. } else {
  86. daytime = "night";
  87. }
  88. /* Ensure ASCIIZ terminated string. */
  89. if (rc < sizeof (username)) {
  90. username[rc] = '\0';
  91. } else {
  92. username[sizeof (username) - 1] = '\0';
  93. }
  94. fmt = "Good %s, %s (from %d).";
  95. /* Technically this might be overly pessimistic about size. */
  96. if ((strlen (username) + strlen (daytime) + strlen (fmt)) >=
  97. sizeof (greeting)) {
  98. fmt = "I'm sorry, your name is too long. But good %s anyway.";
  99. }
  100. /* snprintf would be safer, but the above check protects us. */
  101. sprintf (greeting, fmt, daytime, username, (int)getpid());
  102. rc = nn_send (fd, greeting, strlen (greeting), 0);
  103. if (rc < 0) {
  104. /* There are several legitimate reasons this can fail.
  105. We note them for debugging purposes, but then ignore
  106. otherwise. If the socket is closed or failing, we will
  107. notice in recv above, and exit then. */
  108. fprintf (stderr, "nn_send: %s (ignoring)\n",
  109. nn_strerror (nn_errno ()));
  110. }
  111. }
  112. nn_close (fd);
  113. return (-1);
  114. }
  115. /* The client runs just once, and then returns. */
  116. int client (const char *url, const char *username)
  117. {
  118. int fd;
  119. int rc;
  120. char *greeting;
  121. char *msg;
  122. fd = nn_socket (AF_SP, NN_REQ);
  123. if (fd < 0) {
  124. fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
  125. return (-1);
  126. }
  127. if (nn_connect (fd, url) < 0) {
  128. fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
  129. nn_close (fd);
  130. return (-1);
  131. }
  132. usleep(1000);
  133. if (nn_send (fd, username, strlen (username), 0) < 0) {
  134. fprintf (stderr, "nn_send: %s\n", nn_strerror (nn_errno ()));
  135. nn_close (fd);
  136. return (-1);
  137. }
  138. /* Here we ask the library to allocate response buffer for us (NN_MSG). */
  139. rc = nn_recv (fd, &msg, NN_MSG, 0);
  140. if (rc < 0) {
  141. fprintf (stderr, "nn_recv: %s\n", nn_strerror (nn_errno ()));
  142. nn_close (fd);
  143. return (-1);
  144. }
  145. nn_close (fd);
  146. /* Response is not ASCIIZ terminated. */
  147. greeting = calloc (rc + 1, 1);
  148. if (greeting == NULL) {
  149. fprintf (stderr, "calloc: %s\n", strerror (errno));
  150. return (-1);
  151. }
  152. memcpy(greeting, msg, rc);
  153. nn_freemsg (msg);
  154. printf ("%s\n", greeting);
  155. free (greeting);
  156. return (0);
  157. }
  158. int device (const char *url1, const char *url2)
  159. {
  160. int s1, s2, rv;
  161. s1 = nn_socket (AF_SP_RAW, NN_REQ);
  162. if (s1 < 0) {
  163. fprintf (stderr, "nn_socket: %s\n", nn_strerror (nn_errno ()));
  164. return (-1);
  165. }
  166. if (nn_bind (s1, url1) < 0) {
  167. fprintf (stderr, "nn_bind1(%s): %s\n", url1, nn_strerror (nn_errno ()));
  168. return (-1);
  169. }
  170. s2 = nn_socket (AF_SP_RAW, NN_REP);
  171. if (s2 < 0) {
  172. fprintf (stderr, "nn_socket: %s\n", nn_strerror(nn_errno ()));
  173. return (-1);
  174. }
  175. if (nn_bind (s2, url2) < 0) {
  176. fprintf (stderr, "nn_bind2(%s): %s\n", url2, nn_strerror (nn_errno ()));
  177. return (-1);
  178. }
  179. if (nn_device (s1, s2) != 0) {
  180. fprintf (stderr, "nn_device: %s\n", nn_strerror (nn_errno ()));
  181. return (-1);
  182. }
  183. return (0);
  184. }
  185. int main (int argc, char **argv)
  186. {
  187. int rc;
  188. if ((argc == 3) && (strcmp (argv[1], "-s") == 0)) {
  189. rc = server (argv[2]);
  190. } else if ((argc == 4) && (strcmp (argv[1], "-d") == 0)) {
  191. rc = device (argv[2], argv[3]);
  192. } else if ((argc == 4) && (strcmp (argv[1], "-c") == 0)) {
  193. rc = client (argv[2], argv[3]);
  194. } else {
  195. fprintf (stderr, "Usage: %s -s <serverurl>\n", argv[0]);
  196. fprintf (stderr, "Usage: %s -d <serverurl> <clienturl>\n", argv[0]);
  197. fprintf (stderr, "Usage: %s -c <clienturl> <name>\n", argv[0]);
  198. exit (EXIT_FAILURE);
  199. }
  200. exit (rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
  201. }