stdoutsub.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2013 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial contribution
  15. * Ian Craggs - change delimiter option from char to string
  16. * Al Stockdill-Mander - Version using the embedded C client
  17. *******************************************************************************/
  18. /*
  19. stdout subscriber
  20. compulsory parameters:
  21. topic to subscribe to
  22. defaulted parameters:
  23. --host localhost
  24. --port 1883
  25. --qos 2
  26. --delimiter \n
  27. --clientid stdout_subscriber
  28. --userid none
  29. --password none
  30. for example:
  31. stdoutsub topic/of/interest --host iot.eclipse.org
  32. */
  33. #include <stdio.h>
  34. #include "MQTTClient.h"
  35. #include <stdio.h>
  36. #include <signal.h>
  37. #include <memory.h>
  38. #include <sys/time.h>
  39. volatile int toStop = 0;
  40. void usage()
  41. {
  42. printf("MQTT stdout subscriber\n");
  43. printf("Usage: stdoutsub topicname <options>, where options are:\n");
  44. printf(" --host <hostname> (default is localhost)\n");
  45. printf(" --port <port> (default is 1883)\n");
  46. printf(" --qos <qos> (default is 2)\n");
  47. printf(" --delimiter <delim> (default is \\n)\n");
  48. printf(" --clientid <clientid> (default is hostname+timestamp)\n");
  49. printf(" --username none\n");
  50. printf(" --password none\n");
  51. printf(" --showtopics <on or off> (default is on if the topic has a wildcard, else off)\n");
  52. exit(-1);
  53. }
  54. void cfinish(int sig)
  55. {
  56. signal(SIGINT, NULL);
  57. toStop = 1;
  58. }
  59. struct opts_struct
  60. {
  61. char* clientid;
  62. int nodelimiter;
  63. char* delimiter;
  64. enum QoS qos;
  65. char* username;
  66. char* password;
  67. char* host;
  68. int port;
  69. int showtopics;
  70. } opts =
  71. {
  72. (char*)"stdout-subscriber", 0, (char*)"\n", QOS2, NULL, NULL, (char*)"localhost", 1883, 0
  73. };
  74. void getopts(int argc, char** argv)
  75. {
  76. int count = 2;
  77. while (count < argc)
  78. {
  79. if (strcmp(argv[count], "--qos") == 0)
  80. {
  81. if (++count < argc)
  82. {
  83. if (strcmp(argv[count], "0") == 0)
  84. opts.qos = QOS0;
  85. else if (strcmp(argv[count], "1") == 0)
  86. opts.qos = QOS1;
  87. else if (strcmp(argv[count], "2") == 0)
  88. opts.qos = QOS2;
  89. else
  90. usage();
  91. }
  92. else
  93. usage();
  94. }
  95. else if (strcmp(argv[count], "--host") == 0)
  96. {
  97. if (++count < argc)
  98. opts.host = argv[count];
  99. else
  100. usage();
  101. }
  102. else if (strcmp(argv[count], "--port") == 0)
  103. {
  104. if (++count < argc)
  105. opts.port = atoi(argv[count]);
  106. else
  107. usage();
  108. }
  109. else if (strcmp(argv[count], "--clientid") == 0)
  110. {
  111. if (++count < argc)
  112. opts.clientid = argv[count];
  113. else
  114. usage();
  115. }
  116. else if (strcmp(argv[count], "--username") == 0)
  117. {
  118. if (++count < argc)
  119. opts.username = argv[count];
  120. else
  121. usage();
  122. }
  123. else if (strcmp(argv[count], "--password") == 0)
  124. {
  125. if (++count < argc)
  126. opts.password = argv[count];
  127. else
  128. usage();
  129. }
  130. else if (strcmp(argv[count], "--delimiter") == 0)
  131. {
  132. if (++count < argc)
  133. opts.delimiter = argv[count];
  134. else
  135. opts.nodelimiter = 1;
  136. }
  137. else if (strcmp(argv[count], "--showtopics") == 0)
  138. {
  139. if (++count < argc)
  140. {
  141. if (strcmp(argv[count], "on") == 0)
  142. opts.showtopics = 1;
  143. else if (strcmp(argv[count], "off") == 0)
  144. opts.showtopics = 0;
  145. else
  146. usage();
  147. }
  148. else
  149. usage();
  150. }
  151. count++;
  152. }
  153. }
  154. void messageArrived(MessageData* md)
  155. {
  156. MQTTMessage* message = md->message;
  157. if (opts.showtopics)
  158. printf("%.*s\t", md->topicName->lenstring.len, md->topicName->lenstring.data);
  159. if (opts.nodelimiter)
  160. printf("%.*s", (int)message->payloadlen, (char*)message->payload);
  161. else
  162. printf("%.*s%s", (int)message->payloadlen, (char*)message->payload, opts.delimiter);
  163. //fflush(stdout);
  164. }
  165. int main(int argc, char** argv)
  166. {
  167. int rc = 0;
  168. unsigned char buf[100];
  169. unsigned char readbuf[100];
  170. if (argc < 2)
  171. usage();
  172. char* topic = argv[1];
  173. if (strchr(topic, '#') || strchr(topic, '+'))
  174. opts.showtopics = 1;
  175. if (opts.showtopics)
  176. printf("topic is %s\n", topic);
  177. getopts(argc, argv);
  178. Network n;
  179. Client c;
  180. signal(SIGINT, cfinish);
  181. signal(SIGTERM, cfinish);
  182. NewNetwork(&n);
  183. ConnectNetwork(&n, opts.host, opts.port);
  184. MQTTClient(&c, &n, 1000, buf, 100, readbuf, 100);
  185. MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
  186. data.willFlag = 0;
  187. data.MQTTVersion = 3;
  188. data.clientID.cstring = opts.clientid;
  189. data.username.cstring = opts.username;
  190. data.password.cstring = opts.password;
  191. data.keepAliveInterval = 10;
  192. data.cleansession = 1;
  193. printf("Connecting to %s %d\n", opts.host, opts.port);
  194. rc = MQTTConnect(&c, &data);
  195. printf("Connected %d\n", rc);
  196. printf("Subscribing to %s\n", topic);
  197. rc = MQTTSubscribe(&c, topic, opts.qos, messageArrived);
  198. printf("Subscribed %d\n", rc);
  199. while (!toStop)
  200. {
  201. MQTTYield(&c, 1000);
  202. }
  203. printf("Stopping\n");
  204. MQTTDisconnect(&c);
  205. n.disconnect(&n);
  206. return 0;
  207. }