case7_double_connect.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*******************************************************************************
  2. * Copyright (c) 2014 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 API and implementation and/or initial documentation
  15. * Sergio R. Caprile - clarifications and/or documentation extension
  16. *
  17. * Description:
  18. * Short topic name used to avoid registration process
  19. *******************************************************************************/
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include "MQTTSNPacket.h"
  26. #include "transport.h"
  27. #include "int_test_result.h"
  28. #define TLOG(fmt, ...) tlog("case7: ", fmt, ## __VA_ARGS__)
  29. int connect(MQTTSNPacket_connectData *options, char *host, int port, unsigned char *buf, int buflen)
  30. {
  31. int len = 0, rc = 0;
  32. int connack_rc = -1;
  33. TLOG("Sending to hostname %s port %d\n", host, port);
  34. len = MQTTSNSerialize_connect(buf, buflen, options);
  35. if((rc = transport_sendPacketBuffer(host, port, buf, len)) != 0)
  36. {
  37. TLOG("Send connect failed, rc:%d\n", rc);
  38. return rc;
  39. }
  40. /* wait for connack */
  41. if (MQTTSNPacket_read(buf, buflen, transport_getdata) == MQTTSN_CONNACK)
  42. {
  43. if (MQTTSNDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0)
  44. {
  45. TLOG("Unable to connect, return code %d\n", connack_rc);
  46. }
  47. else
  48. TLOG("connected rc %d\n", connack_rc);
  49. }
  50. return connack_rc;
  51. }
  52. int main(int argc, char** argv)
  53. {
  54. int rc = 0;
  55. int mysock;
  56. unsigned char buf[200];
  57. int buflen = sizeof(buf);
  58. unsigned char payload[16];
  59. int payloadlen = 0;
  60. int len = 0;
  61. int qos = 0;
  62. int retained = 0;
  63. short packetid = 0;
  64. char ascii = 0;
  65. // char *topicname = "a long topic name";
  66. char *host = "127.0.0.1";
  67. int port = 1884;
  68. int i = 0;
  69. MQTTSNString msstr = {0};
  70. MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
  71. mysock = transport_open();
  72. if(mysock < 0)
  73. return mysock;
  74. if (argc > 1)
  75. host = argv[1];
  76. if (argc > 2)
  77. port = atoi(argv[2]);
  78. options.clientID.cstring = "testclientid_case7";
  79. options.duration = 100;
  80. if((rc = connect(&options, host, port, buf, buflen) != 0))
  81. goto exit;
  82. sleep(2);
  83. // a new client-id on the same udp port will be refused by emq-sn
  84. options.clientID.cstring = "testclientid_case7_new";
  85. TLOG("will send connect with new clientId:%s\n", options.clientID.cstring);
  86. if((rc = connect(&options, host, port, buf, buflen)) == 0)
  87. goto exit;
  88. sleep(2);
  89. // go back to the old client-id and the connection will be successful
  90. options.clientID.cstring = "testclientid_case7";
  91. TLOG("will send connect with old clientId:%s\n", options.clientID.cstring);
  92. if((rc = connect(&options, host, port, buf, buflen)) != 0)
  93. goto exit;
  94. sleep(2);
  95. options.duration = 90;
  96. TLOG("will send connect with new duration:%d\n", options.duration);
  97. if((rc = connect(&options, host, port, buf, buflen)) == 0)
  98. goto exit;
  99. sleep(2);
  100. options.duration = 100;
  101. TLOG("will send connect with old duration:%d\n", options.duration);
  102. if((rc = connect(&options, host, port, buf, buflen)) != 0)
  103. goto exit;
  104. mark_result(argv[0], RESULT_PASS);
  105. transport_close();
  106. return 0;
  107. exit:
  108. mark_result(argv[0], RESULT_FAIL);
  109. transport_close();
  110. return 0;
  111. }