test6.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*******************************************************************************
  2. * Copyright (c) 2011, 2020 IBM Corp. and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  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 API and implementation and/or initial documentation
  15. *******************************************************************************/
  16. /**
  17. * @file
  18. * Async C client program for the MQTT v3 restart/recovery test suite.
  19. */
  20. #include "MQTTAsync.h"
  21. /*#define NO_HEAP_TRACKING 1
  22. #include "Heap.h"*/
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #if !defined(_WINDOWS)
  26. #include <sys/time.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #else
  30. #include <windows.h>
  31. #endif
  32. void usage(void)
  33. {
  34. printf("help!!\n");
  35. exit(EXIT_FAILURE);
  36. }
  37. static char pub_topic[200];
  38. static char sub_topic[200];
  39. struct
  40. {
  41. char* connection; /**< connection to system under test. */
  42. char** connections; /**< HA connection list */
  43. int connection_count;
  44. char* control_connection; /**< MQTT control connection, for test sync */
  45. char* topic;
  46. char* control_topic;
  47. char* clientid;
  48. int slot_no;
  49. int qos;
  50. int retained;
  51. char* username;
  52. char* password;
  53. int verbose;
  54. int persistence;
  55. } opts =
  56. {
  57. "tcp://localhost:1884",
  58. NULL,
  59. 0,
  60. "tcp://localhost:7777",
  61. "Eclipse/Paho/restart_test",
  62. "Eclipse/Paho/restart_test/control",
  63. "C_broken_client",
  64. 1,
  65. 2,
  66. 0,
  67. NULL,
  68. NULL,
  69. 0,
  70. 0,
  71. };
  72. void getopts(int argc, char** argv)
  73. {
  74. int count = 1;
  75. while (count < argc)
  76. {
  77. if (strcmp(argv[count], "--qos") == 0)
  78. {
  79. if (++count < argc)
  80. {
  81. if (strcmp(argv[count], "0") == 0)
  82. opts.qos = 0;
  83. else if (strcmp(argv[count], "1") == 0)
  84. opts.qos = 1;
  85. else if (strcmp(argv[count], "2") == 0)
  86. opts.qos = 2;
  87. else
  88. usage();
  89. }
  90. else
  91. usage();
  92. }
  93. else if (strcmp(argv[count], "--slot_no") == 0)
  94. {
  95. if (++count < argc)
  96. opts.slot_no = atoi(argv[count]);
  97. else
  98. usage();
  99. }
  100. else if (strcmp(argv[count], "--connection") == 0)
  101. {
  102. if (++count < argc)
  103. opts.connection = argv[count];
  104. else
  105. usage();
  106. }
  107. else if (strcmp(argv[count], "--connections") == 0)
  108. {
  109. if (++count < argc)
  110. {
  111. opts.connection_count = 0;
  112. opts.connections = malloc(sizeof(char*) * 5);
  113. char* tok = strtok(argv[count], " ");
  114. while (tok)
  115. {
  116. opts.connections[opts.connection_count] = malloc(strlen(tok)+1);
  117. strcpy(opts.connections[opts.connection_count], tok);
  118. opts.connection_count++;
  119. tok = strtok(NULL, " ");
  120. }
  121. }
  122. else
  123. usage();
  124. }
  125. else if (strcmp(argv[count], "--control_connection") == 0)
  126. {
  127. if (++count < argc)
  128. opts.control_connection = argv[count];
  129. else
  130. usage();
  131. }
  132. else if (strcmp(argv[count], "--clientid") == 0)
  133. {
  134. if (++count < argc)
  135. opts.clientid = argv[count];
  136. else
  137. usage();
  138. }
  139. else if (strcmp(argv[count], "--username") == 0)
  140. {
  141. if (++count < argc)
  142. opts.username = argv[count];
  143. else
  144. usage();
  145. }
  146. else if (strcmp(argv[count], "--password") == 0)
  147. {
  148. if (++count < argc)
  149. opts.password = argv[count];
  150. else
  151. usage();
  152. }
  153. else if (strcmp(argv[count], "--persistent") == 0)
  154. opts.persistence = 1;
  155. else if (strcmp(argv[count], "--verbose") == 0)
  156. opts.verbose = 1;
  157. count++;
  158. }
  159. }
  160. #define LOGA_DEBUG 0
  161. #define LOGA_ALWAYS 1
  162. #define LOGA_INFO 2
  163. #include <stdarg.h>
  164. #include <time.h>
  165. #include <sys/timeb.h>
  166. void MyLog(int LOGA_level, char* format, ...)
  167. {
  168. static char msg_buf[256];
  169. va_list args;
  170. #if defined(_WIN32) || defined(_WINDOWS)
  171. struct timeb ts;
  172. #else
  173. struct timeval ts;
  174. #endif
  175. struct tm timeinfo;
  176. if (LOGA_level == LOGA_DEBUG && opts.verbose == 0)
  177. return;
  178. #if defined(_WIN32) || defined(_WINDOWS)
  179. ftime(&ts);
  180. localtime_s(&timeinfo, &ts.time);
  181. #else
  182. gettimeofday(&ts, NULL);
  183. localtime_r(&ts.tv_sec, &timeinfo);
  184. #endif
  185. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  186. #if defined(_WIN32) || defined(_WINDOWS)
  187. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  188. #else
  189. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  190. #endif
  191. va_start(args, format);
  192. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  193. va_end(args);
  194. printf("%s\n", msg_buf);
  195. fflush(stdout);
  196. }
  197. void MySleep(long milliseconds)
  198. {
  199. #if defined(_WIN32) || defined(_WIN64)
  200. Sleep(milliseconds);
  201. #else
  202. usleep(milliseconds*1000);
  203. #endif
  204. }
  205. #if defined(_WIN32) || defined(_WINDOWS)
  206. #define START_TIME_TYPE DWORD
  207. static DWORD start_time = 0;
  208. START_TIME_TYPE start_clock(void)
  209. {
  210. return GetTickCount();
  211. }
  212. #elif defined(AIX)
  213. #define START_TIME_TYPE struct timespec
  214. START_TIME_TYPE start_clock(void)
  215. {
  216. static struct timespec start;
  217. clock_gettime(CLOCK_REALTIME, &start);
  218. return start;
  219. }
  220. #else
  221. #define START_TIME_TYPE struct timeval
  222. /* TODO - unused - remove? static struct timeval start_time; */
  223. START_TIME_TYPE start_clock(void)
  224. {
  225. struct timeval start_time;
  226. gettimeofday(&start_time, NULL);
  227. return start_time;
  228. }
  229. #endif
  230. #if defined(_WIN32)
  231. long elapsed(START_TIME_TYPE start_time)
  232. {
  233. return GetTickCount() - start_time;
  234. }
  235. #elif defined(AIX)
  236. #define assert(a)
  237. long elapsed(struct timespec start)
  238. {
  239. struct timespec now, res;
  240. clock_gettime(CLOCK_REALTIME, &now);
  241. ntimersub(now, start, res);
  242. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  243. }
  244. #else
  245. long elapsed(START_TIME_TYPE start_time)
  246. {
  247. struct timeval now, res;
  248. gettimeofday(&now, NULL);
  249. timersub(&now, &start_time, &res);
  250. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  251. }
  252. #endif
  253. MQTTAsync control_client;
  254. MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
  255. MQTTAsync client;
  256. int arrivedCount = 0;
  257. int expectedCount = 0;
  258. int measuring = 0;
  259. long roundtrip_time = 0L;
  260. int errors = 0;
  261. int stopping = 0;
  262. int connection_lost = 0; /* for use with the persistence option */
  263. int recreated = 0;
  264. int client_cleaned = 0;
  265. char* wait_message = NULL;
  266. char* wait_message2 = NULL;
  267. int control_found = 0;
  268. long last_completion_time = -1;
  269. int test_count = 1000;
  270. void control_connectionLost(void* context, char* cause)
  271. {
  272. MyLog(LOGA_ALWAYS, "Control connection lost - stopping");
  273. stopping = 1;
  274. }
  275. /**-----------------------------------------------------------------------------
  276. * Callback which receives messages from the control connection
  277. * @param context
  278. * @param topicName the name of the topic on which the message is received
  279. * @param topicLen the length of the topic name (in case of embedded nulls)
  280. * @param m pointer to the message received
  281. * @return boolean
  282. */
  283. int control_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* m)
  284. {
  285. MyLog(LOGA_ALWAYS, "Control message arrived: %.*s wait message: %s",
  286. m->payloadlen, m->payload, (wait_message == NULL) ? "None" : wait_message);
  287. if (strncmp(m->payload, "stop", 4) == 0)
  288. {
  289. MyLog(LOGA_ALWAYS, "Stop message arrived, stopping...");
  290. stopping = 1;
  291. }
  292. else if (wait_message != NULL && strncmp(wait_message, m->payload,
  293. strlen(wait_message)) == 0)
  294. {
  295. MyLog(LOGA_ALWAYS, "Wait message %s found", wait_message);
  296. control_found = 1;
  297. wait_message = NULL;
  298. }
  299. else if (wait_message2 != NULL && strncmp(wait_message2, m->payload,
  300. strlen(wait_message2)) == 0)
  301. {
  302. MyLog(LOGA_ALWAYS, "Wait message2 %s found", wait_message);
  303. control_found = 2;
  304. wait_message2 = NULL;
  305. }
  306. MQTTAsync_free(topicName);
  307. MQTTAsync_freeMessage(&m);
  308. return 1;
  309. }
  310. int control_send(char* message)
  311. {
  312. char buf[156];
  313. int rc = 0;
  314. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  315. sprintf(buf, "%s: %s", opts.clientid, message);
  316. MyLog(LOGA_ALWAYS, "Sending control message: %s", message);
  317. rc = MQTTAsync_send(control_client, pub_topic, (int)strlen(buf),
  318. buf, 1, 0, &ropts);
  319. MyLog(LOGA_DEBUG, "Control message sent: %s", buf);
  320. return rc;
  321. }
  322. /* wait for a specific message on the control topic. */
  323. int control_wait(char* message)
  324. {
  325. int count = 0;
  326. char buf[120];
  327. control_found = 0;
  328. wait_message = message;
  329. sprintf(buf, "waiting for: %s", message);
  330. control_send(buf);
  331. MyLog(LOGA_ALWAYS, "Waiting for: %s", message);
  332. while (control_found == 0 && stopping == 0)
  333. {
  334. if (++count == 300)
  335. {
  336. stopping = 1;
  337. MyLog(LOGA_ALWAYS, "Failed to receive message %s, stopping ", message);
  338. return 0; /* time out and tell the caller the message was not found */
  339. }
  340. MySleep(1000);
  341. }
  342. MyLog(LOGA_ALWAYS, "Control message found: %s, control_found %d", message, control_found);
  343. return control_found;
  344. }
  345. /* wait for a specific message on the control topic. */
  346. int control_which(char* message1, char* message2)
  347. {
  348. int count = 0;
  349. control_found = 0;
  350. wait_message = message1;
  351. wait_message2 = message2;
  352. while (control_found == 0)
  353. {
  354. if (++count == 300)
  355. break; /* time out and tell the caller the message was not found */
  356. MySleep(1000);
  357. }
  358. return control_found;
  359. }
  360. START_TIME_TYPE global_start_time;
  361. int messageArrived(void* context, char* topicName, int topicLen,
  362. MQTTAsync_message* m)
  363. {
  364. int seqno = -1;
  365. char* token = NULL;
  366. token = strtok(m->payload, " ");
  367. token = strtok(NULL, " ");
  368. token = strtok(NULL, " ");
  369. if (token)
  370. seqno = atoi(token);
  371. if (m->qos != opts.qos)
  372. {
  373. MyLog(LOGA_ALWAYS, "Error, expecting QoS %d but got %d", opts.qos,
  374. m->qos);
  375. errors++;
  376. } else if (seqno != arrivedCount + 1)
  377. {
  378. if (m->qos == 2 || (m->qos == 1 && seqno > arrivedCount + 1))
  379. {
  380. if (seqno == -1)
  381. MyLog(LOGA_ALWAYS,
  382. "Error, expecting sequence number %d but got message id %d, payload was %.*s",
  383. arrivedCount + 1, m->msgid, m->payloadlen, m->payload);
  384. else
  385. MyLog(LOGA_ALWAYS,
  386. "Error, expecting sequence number %d but got %d message id %d",
  387. arrivedCount + 1, seqno, m->msgid);
  388. errors++;
  389. }
  390. }
  391. arrivedCount++;
  392. MQTTAsync_free(topicName);
  393. MQTTAsync_freeMessage(&m);
  394. if (measuring && arrivedCount == test_count)
  395. roundtrip_time = elapsed(global_start_time);
  396. return 1;
  397. }
  398. void client_onReconnect(void* context, MQTTAsync_successData* response)
  399. {
  400. MQTTAsync c = (MQTTAsync)context;
  401. MyLog(LOGA_ALWAYS, "Successfully reconnected");
  402. }
  403. void client_onReconnectFailure(void* context, MQTTAsync_failureData* response)
  404. {
  405. MQTTAsync c = (MQTTAsync)context;
  406. int rc;
  407. MyLog(LOGA_ALWAYS, "Failed to reconnect with return code %d", (response) ? response->code : -9999);
  408. conn_opts.context = context;
  409. conn_opts.keepAliveInterval = 10;
  410. conn_opts.username = opts.username;
  411. conn_opts.password = opts.password;
  412. conn_opts.cleansession = 0;
  413. conn_opts.onSuccess = client_onReconnect;
  414. conn_opts.onFailure = client_onReconnectFailure;
  415. rc = MQTTAsync_connect(c, &conn_opts);
  416. if (rc != MQTTASYNC_SUCCESS)
  417. {
  418. MyLog(LOGA_ALWAYS, "Failed to start reconnect with return code %d", rc);
  419. stopping = 1;
  420. }
  421. }
  422. void connectionLost(void* context, char* cause)
  423. {
  424. MQTTAsync c = (MQTTAsync)context;
  425. int rc = 0;
  426. MyLog(LOGA_ALWAYS, "Connection lost when %d messages arrived out of %d expected",
  427. arrivedCount, expectedCount);
  428. //dotrace = 1;
  429. if (opts.persistence)
  430. connection_lost = 1;
  431. else
  432. {
  433. conn_opts.context = context;
  434. conn_opts.keepAliveInterval = 10;
  435. conn_opts.username = opts.username;
  436. conn_opts.password = opts.password;
  437. conn_opts.cleansession = 0;
  438. conn_opts.onSuccess = client_onReconnect;
  439. conn_opts.onFailure = client_onReconnectFailure;
  440. if (opts.connections)
  441. {
  442. conn_opts.serverURIcount = opts.connection_count;
  443. conn_opts.serverURIs = opts.connections;
  444. }
  445. else
  446. {
  447. conn_opts.serverURIcount = 0;
  448. conn_opts.serverURIs = NULL;
  449. }
  450. //printf("reconnecting to first serverURI %s\n", conn_opts.serverURIs[0]);
  451. MyLog(LOGA_ALWAYS, "Starting reconnect attempt");
  452. rc = MQTTAsync_connect(context, &conn_opts);
  453. if (rc != MQTTASYNC_SUCCESS)
  454. {
  455. MyLog(LOGA_ALWAYS, "Failed to start reconnect with return code %d", rc);
  456. stopping = 1;
  457. }
  458. }
  459. }
  460. int recreateReconnect(void)
  461. {
  462. int rc;
  463. if (recreated == 0)
  464. {
  465. MyLog(LOGA_ALWAYS, "Recreating client");
  466. MQTTAsync_destroy(&client); /* destroy the client object so that we force persistence to be read on recreate */
  467. #if !defined(_WINDOWS)
  468. /*heap_info* mqtt_mem = 0;
  469. mqtt_mem = Heap_get_info();
  470. MyLog(LOGA_INFO, "MQTT mem current %ld, max %ld",mqtt_mem->current_size,mqtt_mem->max_size);
  471. if (mqtt_mem->current_size > 20)
  472. HeapScan(5); */
  473. #endif
  474. rc = MQTTAsync_create(&client, opts.connection, opts.clientid, MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  475. if (rc != MQTTASYNC_SUCCESS)
  476. {
  477. MyLog(LOGA_ALWAYS, "MQTTAsync_create failed, rc %d", rc);
  478. goto exit;
  479. }
  480. if ((rc = MQTTAsync_setCallbacks(client, client, connectionLost,
  481. messageArrived, NULL)) != MQTTASYNC_SUCCESS)
  482. {
  483. MyLog(LOGA_ALWAYS, "MQTTAsync_setCallbacks failed, rc %d", rc);
  484. goto exit;
  485. }
  486. recreated = 1;
  487. }
  488. MyLog(LOGA_ALWAYS, "Reconnecting client");
  489. conn_opts.keepAliveInterval = 10;
  490. conn_opts.username = opts.username;
  491. conn_opts.password = opts.password;
  492. conn_opts.cleansession = 0;
  493. conn_opts.context = client;
  494. conn_opts.onSuccess = client_onReconnect;
  495. conn_opts.onFailure = client_onReconnectFailure;
  496. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  497. MyLog(LOGA_ALWAYS, "MQTTAsync_connect failed, rc %d", rc);
  498. else
  499. connection_lost = 0;
  500. exit:
  501. return rc;
  502. }
  503. int success(int count)
  504. {
  505. int rc = 1;
  506. if (errors)
  507. {
  508. MyLog(LOGA_ALWAYS, "Workload test failed because the callback had errors");
  509. rc = 0;
  510. }
  511. if (arrivedCount != count)
  512. {
  513. if (opts.qos == 2 || (opts.qos == 1 && arrivedCount < count))
  514. {
  515. MyLog(LOGA_ALWAYS,
  516. "Workload test failed because the wrong number of messages"
  517. " was received: %d whereas %d were expected",
  518. arrivedCount, count);
  519. rc = 0;
  520. }
  521. }
  522. if (rc == 1)
  523. control_send("verdict: pass");
  524. else
  525. control_send("verdict: fail");
  526. return rc;
  527. }
  528. int waitForCompletion(START_TIME_TYPE start_time)
  529. {
  530. int lastreport = 0;
  531. int wait_count = 0;
  532. int limit = 120;
  533. MyLog(LOGA_ALWAYS, "Wait for completion");
  534. if (opts.qos == 0)
  535. limit = 30; /* we aren't going to get back all QoS 0 messages anyway */
  536. MySleep(1000);
  537. while (arrivedCount < expectedCount)
  538. {
  539. if (arrivedCount > lastreport)
  540. {
  541. MyLog(LOGA_ALWAYS, "%d messages arrived out of %d expected, in %d seconds",
  542. arrivedCount, expectedCount, elapsed(start_time) / 1000);
  543. lastreport = arrivedCount;
  544. }
  545. MySleep(1000);
  546. if (opts.persistence && connection_lost)
  547. recreateReconnect();
  548. if (++wait_count > limit || stopping)
  549. break;
  550. }
  551. last_completion_time = elapsed(start_time) / 1000;
  552. if (opts.qos > 0)
  553. {
  554. MyLog(LOGA_ALWAYS, "Extra wait to see if any duplicates arrive");
  555. MySleep(10000); /* check if any duplicate messages arrive */
  556. }
  557. MyLog(LOGA_ALWAYS, "%d messages arrived out of %d expected, in %d seconds",
  558. arrivedCount, expectedCount, elapsed(start_time) / 1000);
  559. return success(expectedCount);
  560. }
  561. int messagesSent = 0;
  562. void messageSent(void* context, MQTTAsync_successData* response)
  563. {
  564. messagesSent++;
  565. }
  566. void one_iteration(void)
  567. {
  568. int interval = 0;
  569. int i = 0;
  570. int seqno = 0;
  571. int rc = 0;
  572. START_TIME_TYPE start_time;
  573. int last_expected_count = expectedCount;
  574. int test_interval = 30;
  575. if (control_wait("start_measuring") == 0)
  576. goto exit;
  577. connection_lost = 0;
  578. recreated = 0;
  579. /* find the time for evaluation_count round-trip messages */
  580. MyLog(LOGA_INFO, "Evaluating how many messages needed");
  581. expectedCount = arrivedCount = 0;
  582. measuring = 1;
  583. global_start_time = start_clock();
  584. for (i = 1; i <= test_count; ++i)
  585. {
  586. char payload[128];
  587. sprintf(payload, "message number %d", i);
  588. rc = MQTTAsync_send(client, opts.topic, (int)(strlen(payload)+1), payload,
  589. opts.qos, opts.retained, NULL);
  590. while (rc != MQTTASYNC_SUCCESS)
  591. {
  592. if (opts.persistence && (connection_lost || rc == MQTTASYNC_DISCONNECTED))
  593. recreateReconnect();
  594. if (stopping)
  595. goto exit;
  596. MySleep(1000);
  597. rc = MQTTAsync_send(client, opts.topic, (int)(strlen(payload)+1), payload,
  598. opts.qos, opts.retained, NULL);
  599. while (seqno - messagesSent > 2000)
  600. MySleep(1000);
  601. }
  602. }
  603. MyLog(LOGA_INFO, "Messages sent... waiting for echoes");
  604. while (arrivedCount < test_count)
  605. {
  606. if (opts.persistence && connection_lost)
  607. recreateReconnect();
  608. if (stopping)
  609. goto exit;
  610. MySleep(1000);
  611. MyLog(LOGA_ALWAYS, "arrivedCount %d", arrivedCount);
  612. }
  613. measuring = 0;
  614. /* Now set a target of 30 seconds total round trip */
  615. if (1) //last_completion_time == -1)
  616. {
  617. MyLog(LOGA_ALWAYS, "Round trip time for %d messages is %d ms", test_count, roundtrip_time);
  618. // test_count messages in 3039 ms: (test_interval * 1000) / roundtrip_time * test_count
  619. //expectedCount = 1000 * test_count * test_interval / roundtrip_time / 2;
  620. expectedCount = (test_interval * 1000) / roundtrip_time * test_count;
  621. }
  622. else
  623. {
  624. MyLog(LOGA_ALWAYS, "Last time, %d messages took %d s.", last_expected_count, last_completion_time);
  625. expectedCount = last_expected_count * test_interval / last_completion_time;
  626. }
  627. MyLog(LOGA_ALWAYS, "Therefore %d messages needed for 30 seconds", expectedCount);
  628. if (control_wait("start_test") == 0) /* now synchronize the test interval */
  629. goto exit;
  630. MyLog(LOGA_ALWAYS, "Starting 30 second test run with %d messages", expectedCount);
  631. arrivedCount = 0;
  632. messagesSent = 0;
  633. start_time = start_clock();
  634. while (seqno < expectedCount)
  635. {
  636. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  637. char payload[128];
  638. ropts.onSuccess = messageSent;
  639. seqno++;
  640. sprintf(payload, "message number %d", seqno);
  641. rc = MQTTAsync_send(client, opts.topic, (int)(strlen(payload)+1), payload,
  642. opts.qos, opts.retained, &ropts);
  643. while (rc != MQTTASYNC_SUCCESS)
  644. {
  645. MyLog(LOGA_INFO, "Rc %d from publish with payload %s, retrying", rc, payload);
  646. if (opts.persistence && (connection_lost || rc == MQTTASYNC_DISCONNECTED))
  647. recreateReconnect();
  648. if (stopping)
  649. goto exit;
  650. MySleep(1000);
  651. rc = MQTTAsync_send(client, opts.topic, (int)(strlen(payload)+1), payload,
  652. opts.qos, opts.retained, &ropts);
  653. }
  654. //MyLog(LOGA_DEBUG, "Successful publish with payload %s", payload);
  655. //while (seqno - messagesSent > 2000)
  656. //{
  657. //if (opts.persistence && (connection_lost || rc == MQTTASYNC_DISCONNECTED))
  658. // recreateReconnect();
  659. //}
  660. // MySleep(1000);
  661. }
  662. MyLog(LOGA_ALWAYS, "%d messages sent in %d seconds", expectedCount, elapsed(start_time) / 1000);
  663. waitForCompletion(start_time);
  664. control_wait("test finished");
  665. exit:
  666. ; /* dummy statement for target of exit */
  667. }
  668. static int client_subscribed = 0;
  669. void client_onSubscribe(void* context, MQTTAsync_successData* response)
  670. {
  671. MQTTAsync c = (MQTTAsync)context;
  672. MyLog(LOGA_DEBUG, "In client subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  673. client_subscribed = 1;
  674. }
  675. void client_onFailure(void* context, MQTTAsync_failureData* response)
  676. {
  677. MQTTAsync c = (MQTTAsync)context;
  678. MyLog(LOGA_INFO, "In failure callback");
  679. client_subscribed = -1;
  680. }
  681. void client_onConnect(void* context, MQTTAsync_successData* response)
  682. {
  683. MQTTAsync c = (MQTTAsync)context;
  684. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  685. int rc;
  686. sprintf(sub_topic, "%s/send", opts.control_topic);
  687. sprintf(pub_topic, "%s/receive", opts.control_topic);
  688. ropts.context = context;
  689. ropts.onSuccess = client_onSubscribe;
  690. ropts.onFailure = client_onFailure;
  691. ropts.context = c;
  692. if ((rc = MQTTAsync_subscribe(c, opts.topic, opts.qos, &ropts)) != MQTTASYNC_SUCCESS)
  693. {
  694. MyLog(LOGA_ALWAYS, "client MQTTAsync_subscribe failed, rc %d", rc);
  695. client_subscribed = -1;
  696. }
  697. }
  698. void client_onCleanedDisconnected(void* context, MQTTAsync_successData* response)
  699. {
  700. client_cleaned = 1;
  701. }
  702. void client_onCleaned(void* context, MQTTAsync_successData* response)
  703. {
  704. MQTTAsync c = (MQTTAsync)context;
  705. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  706. int rc;
  707. dopts.context = context;
  708. dopts.onSuccess = client_onCleanedDisconnected;
  709. dopts.onFailure = client_onFailure;
  710. dopts.context = c;
  711. if ((rc = MQTTAsync_disconnect(c, &dopts)) != MQTTASYNC_SUCCESS)
  712. {
  713. MyLog(LOGA_ALWAYS, "client MQTTAsync_disconnect failed, rc %d", rc);
  714. stopping = 1;
  715. }
  716. }
  717. int sendAndReceive(void)
  718. {
  719. int rc = 0;
  720. int persistence = MQTTCLIENT_PERSISTENCE_NONE;
  721. MyLog(LOGA_ALWAYS, "v3 async C client topic workload using QoS %d", opts.qos);
  722. MyLog(LOGA_DEBUG, "Connecting to %s", opts.connection);
  723. if (opts.persistence)
  724. persistence = MQTTCLIENT_PERSISTENCE_DEFAULT;
  725. rc = MQTTAsync_create(&client, opts.connection, opts.clientid, persistence, NULL);
  726. if (rc != MQTTASYNC_SUCCESS)
  727. {
  728. MyLog(LOGA_ALWAYS, "MQTTAsync_create failed, rc %d", rc);
  729. rc = 99;
  730. goto exit;
  731. }
  732. if ((rc = MQTTAsync_setCallbacks(client, client, connectionLost,
  733. messageArrived, NULL)) != MQTTASYNC_SUCCESS)
  734. {
  735. MyLog(LOGA_ALWAYS, "MQTTAsync_setCallbacks failed, rc %d", rc);
  736. rc = 99;
  737. goto destroy_exit;
  738. }
  739. /* wait to know that the controlling process is running before connecting to the SUT */
  740. if (control_wait("who is ready?") == 0)
  741. {
  742. MyLog(LOGA_ALWAYS, "Wait for controller failed");
  743. goto exit;
  744. }
  745. /* connect cleansession, and then disconnect, to clean up */
  746. conn_opts.keepAliveInterval = 10;
  747. conn_opts.username = opts.username;
  748. conn_opts.password = opts.password;
  749. conn_opts.cleansession = 1;
  750. conn_opts.context = client;
  751. conn_opts.onSuccess = client_onCleaned;
  752. conn_opts.onFailure = client_onFailure;
  753. if (opts.connections)
  754. {
  755. conn_opts.serverURIcount = opts.connection_count;
  756. conn_opts.serverURIs = opts.connections;
  757. }
  758. else
  759. {
  760. conn_opts.serverURIcount = 0;
  761. conn_opts.serverURIs = NULL;
  762. }
  763. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  764. {
  765. MyLog(LOGA_ALWAYS, "MQTTAsync_connect failed, rc %d", rc);
  766. rc = 99;
  767. goto destroy_exit;
  768. }
  769. while (client_cleaned == 0)
  770. MySleep(1000);
  771. MyLog(LOGA_ALWAYS, "Client state cleaned up");
  772. conn_opts.cleansession = 0;
  773. conn_opts.context = client;
  774. conn_opts.onSuccess = client_onConnect;
  775. conn_opts.onFailure = client_onFailure;
  776. conn_opts.retryInterval = 1;
  777. if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
  778. {
  779. MyLog(LOGA_ALWAYS, "MQTTAsync_connect failed, rc %d", rc);
  780. rc = 99;
  781. goto destroy_exit;
  782. }
  783. /* wait until subscribed */
  784. while (client_subscribed == 0)
  785. MySleep(1000);
  786. if (client_subscribed != 1)
  787. goto disconnect_exit;
  788. while (1)
  789. {
  790. control_send("Ready");
  791. if (control_which("who is ready?", "continue") == 2)
  792. break;
  793. control_send("Ready");
  794. }
  795. while (!stopping)
  796. {
  797. one_iteration();
  798. }
  799. disconnect_exit:
  800. MQTTAsync_disconnect(client, 0);
  801. destroy_exit:
  802. MQTTAsync_destroy(&client);
  803. exit:
  804. return rc;
  805. }
  806. static int control_subscribed = 0;
  807. void control_onSubscribe(void* context, MQTTAsync_successData* response)
  808. {
  809. MQTTAsync c = (MQTTAsync)context;
  810. MyLog(LOGA_DEBUG, "In control subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  811. control_subscribed = 1;
  812. MyLog(LOGA_ALWAYS, "Connected and subscribed to control connection");
  813. }
  814. void control_onFailure(void* context, MQTTAsync_failureData* response)
  815. {
  816. MQTTAsync c = (MQTTAsync)context;
  817. control_subscribed = -1;
  818. }
  819. void control_onConnect(void* context, MQTTAsync_successData* response)
  820. {
  821. MQTTAsync c = (MQTTAsync)context;
  822. MQTTAsync_responseOptions ropts = MQTTAsync_responseOptions_initializer;
  823. int rc;
  824. sprintf(sub_topic, "%s/send", opts.control_topic);
  825. sprintf(pub_topic, "%s/receive", opts.control_topic);
  826. ropts.onSuccess = control_onSubscribe;
  827. ropts.onFailure = control_onFailure;
  828. ropts.context = c;
  829. MyLog(LOGA_ALWAYS, "Subscribing to control topic %s", sub_topic);
  830. if ((rc = MQTTAsync_subscribe(c, sub_topic, 2, &ropts)) != MQTTASYNC_SUCCESS)
  831. {
  832. MyLog(LOGA_ALWAYS, "control MQTTAsync_subscribe failed, rc %d", rc);
  833. control_subscribed = -1;
  834. }
  835. }
  836. void trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  837. {
  838. //if (level == MQTTASYNC_TRACE_ERROR || strstr(message, "Connect") || strstr(message, "failed"))
  839. printf("Trace : %d, %s\n", level, message);
  840. }
  841. int main(int argc, char** argv)
  842. {
  843. MQTTAsync_connectOptions control_conn_opts = MQTTAsync_connectOptions_initializer;
  844. int rc = 0;
  845. static char topic_buf[200];
  846. static char clientid[40];
  847. #if !defined(_WIN32)
  848. signal(SIGPIPE, SIG_IGN);
  849. #endif
  850. MQTTAsync_nameValue* info = MQTTAsync_getVersionInfo();
  851. while (info->name)
  852. {
  853. MyLog(LOGA_ALWAYS, "%s: %s\n", info->name, info->value);
  854. info++;
  855. }
  856. getopts(argc, argv);
  857. sprintf(topic_buf, "%s_%d", opts.topic, opts.slot_no);
  858. opts.topic = topic_buf;
  859. sprintf(clientid, "%s_%d", opts.clientid, opts.slot_no);
  860. opts.clientid = clientid;
  861. MyLog(LOGA_ALWAYS, "Starting with clientid %s", opts.clientid);
  862. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  863. MQTTAsync_setTraceCallback(trace_callback);
  864. rc = MQTTAsync_create(&control_client, opts.control_connection,
  865. opts.clientid, MQTTCLIENT_PERSISTENCE_NONE, NULL);
  866. if (rc != MQTTASYNC_SUCCESS)
  867. {
  868. MyLog(LOGA_ALWAYS, "control MQTTAsync_create failed, rc %d", rc);
  869. rc = 99;
  870. goto exit;
  871. }
  872. if ((rc = MQTTAsync_setCallbacks(control_client, control_client, control_connectionLost,
  873. control_messageArrived, NULL)) != MQTTASYNC_SUCCESS)
  874. {
  875. MyLog(LOGA_ALWAYS, "control MQTTAsync_setCallbacks failed, rc %d", rc);
  876. rc = 99;
  877. goto destroy_exit;
  878. }
  879. control_subscribed = 0;
  880. control_conn_opts.context = control_client;
  881. control_conn_opts.keepAliveInterval = 10;
  882. control_conn_opts.onSuccess = control_onConnect;
  883. control_conn_opts.onFailure = control_onFailure;
  884. if ((rc = MQTTAsync_connect(control_client, &control_conn_opts))
  885. != MQTTASYNC_SUCCESS)
  886. {
  887. MyLog(LOGA_ALWAYS, "control MQTTAsync_connect failed, rc %d", rc);
  888. rc = 99;
  889. goto destroy_exit;
  890. }
  891. while (control_subscribed == 0)
  892. MySleep(1000);
  893. if (control_subscribed != 1)
  894. goto destroy_exit;
  895. sendAndReceive();
  896. exit:
  897. MQTTAsync_disconnect(control_client, 0);
  898. destroy_exit:
  899. MQTTAsync_destroy(&control_client);
  900. return 0;
  901. }