Annotation of mqtt/src/mqtt_ping.c, revision 1.1.2.3
1.1.2.1 misho 1: #include "global.h"
2: #include "mqtt.h"
3: #include "client.h"
4:
5:
6: io_enableDEBUG;
7:
8: extern char compiled[], compiledby[], compilehost[];
9:
10: struct tagArgs *args;
11:
12:
13: static void
14: Usage(void)
15: {
16: printf( " -= MQTT PING =- MQTT Ping from ELWIX\n"
17: "=== %s@%s === Compiled: %s ===\n\n"
18: " Syntax: mqtt_ping [options] <connect_to_broker[:port]> <ConnectID>\n\n"
1.1.2.3 ! misho 19: "\t-c <num>\t\tNumber of ping requests (default: 1)\n"
1.1.2.1 misho 20: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
21: "\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
22: "\t-U <username>\t\tUsername\n"
23: "\t-P <password>\t\tPassword\n"
24: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
25: "\t-h\t\t\tHelp! This screen\n\n",
26: compiledby, compilehost, compiled);
27: }
28:
29: static void
30: cleanArgs(struct tagArgs * __restrict args)
31: {
32: mqtt_msgFree(&args->msg, 42);
33: AIT_FREE_VAL(&args->User);
34: AIT_FREE_VAL(&args->Pass);
35: AIT_FREE_VAL(&args->ConnID);
36: }
37:
38: static int
1.1.2.3 ! misho 39: Ping(int sock, int num)
1.1.2.1 misho 40: {
41: int siz = 0;
42: struct timeval before, after;
43: ait_val_t val;
1.1.2.2 misho 44: struct mqtthdr *hdr;
1.1.2.1 misho 45:
46: gettimeofday(&before, NULL);
47:
48: siz = mqtt_msgPINGREQ(args->msg);
49: if (siz == -1) {
50: printf("Error:: msgPINGREQ #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
51: return -1;
52: }
53: if (SendTo(sock, siz) == -1)
54: return -1;
55:
1.1.2.3 ! misho 56: reply:
! 57: if ((siz = RecvFrom(sock)) == -1 || !siz)
! 58: return -1;
! 59: else
! 60: hdr = (struct mqtthdr*) args->msg->msg_base;
! 61: if (hdr->mqtt_msg.type == MQTT_TYPE_PINGREQ) {
! 62: siz = mqtt_msgPINGRESP(args->msg);
! 63: if (siz == -1) {
! 64: printf("Error:: msgPINGRESP #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
1.1.2.2 misho 65: return -1;
66: }
1.1.2.3 ! misho 67: if (SendTo(sock, siz) == -1)
! 68: return -1;
! 69:
! 70: goto reply;
! 71: } else {
! 72: siz = mqtt_readPINGRESP(args->msg);
! 73: if (siz == -1) {
! 74: printf("Error:: readPINGRESP #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
! 75: return -1;
! 76: }
! 77: }
1.1.2.1 misho 78:
79: gettimeofday(&after, NULL);
80:
1.1.2.3 ! misho 81: printf(" + Ping %d MQTT broker %s ... %f sec.\n", num, io_n2addr(&args->addr, &val),
1.1.2.1 misho 82: ((after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1.e6));
83: AIT_FREE_VAL(&val);
84:
85: return 0;
86: }
87:
88:
89: int
90: main(int argc, char **argv)
91: {
92: char ch;
93: ait_val_t val;
94: u_short port = atoi(MQTT_PORT);
1.1.2.3 ! misho 95: int sock, num = 1, ret = 0, lost = 0;
! 96: register int i;
1.1.2.1 misho 97:
98: if (!(args = malloc(sizeof(struct tagArgs)))) {
99: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
100: return 1;
101: } else
102: memset(args, 0, sizeof(struct tagArgs));
103: args->free = cleanArgs;
104:
105: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
106: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
107: args->free(args);
108: free(args);
109: return 1;
110: }
111:
112: AIT_SET_STR(&args->ConnID, "");
113: AIT_SET_STR(&args->User, "");
114: AIT_SET_STR(&args->Pass, "");
115:
116: args->ka = MQTT_KEEPALIVE;
1.1.2.3 ! misho 117: while ((ch = getopt(argc, argv, "T:U:P:p:c:vh")) != -1)
1.1.2.1 misho 118: switch (ch) {
1.1.2.3 ! misho 119: case 'c':
! 120: num = (int) strtol(optarg, NULL, 0);
1.1.2.1 misho 121: case 'T':
122: args->ka = (u_short) strtol(optarg, NULL, 0);
123: break;
124: case 'U':
125: AIT_FREE_VAL(&args->User);
126: AIT_SET_STR(&args->User, optarg);
127: break;
128: case 'P':
129: AIT_FREE_VAL(&args->Pass);
130: AIT_SET_STR(&args->Pass, optarg);
131: break;
132: case 'p':
133: port = (u_short) strtol(optarg, NULL, 0);
134: break;
135: case 'v':
136: io_incDebug;
137: break;
138: case 'h':
139: default:
140: args->free(args);
141: free(args);
142: Usage();
143: return 1;
144: }
145: argc -= optind;
146: argv += optind;
147: if (argc < 2) {
148: printf("Error:: host for connect not found or connection id!\n\n");
149: args->free(args);
150: free(args);
151: Usage();
152: return 1;
153: } else {
154: AIT_FREE_VAL(&args->ConnID);
155: AIT_SET_STR(&args->ConnID, argv[1]);
156: }
157: if (!io_gethostbyname(*argv, port, &args->addr)) {
158: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
159: args->free(args);
160: free(args);
161: Usage();
162: return 1;
163: }
1.1.2.2 misho 164: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
1.1.2.1 misho 165: AIT_FREE_VAL(&val);
166:
167: if ((sock = InitClient()) == -1) {
168: args->free(args);
169: free(args);
170: return 2;
171: }
172:
173: switch ((ret = ConnectClient(sock))) {
174: case -1:
175: printf(">> FAILED!\n");
176: break;
177: case MQTT_RETCODE_ACCEPTED:
178: printf(">> OK\n");
179: break;
180: case MQTT_RETCODE_REFUSE_VER:
181: printf(">> Incorrect version\n");
182: break;
183: case MQTT_RETCODE_REFUSE_ID:
184: printf(">> Incorrect connectID\n");
185: break;
186: case MQTT_RETCODE_REFUSE_UNAVAIL:
187: printf(">> Service unavailable\n");
188: break;
189: case MQTT_RETCODE_REFUSE_USERPASS:
190: printf(">> Refuse user/pass\n");
191: break;
192: case MQTT_RETCODE_DENIED:
193: printf(">> DENIED.\n");
194: break;
195: }
196:
197: if (ret == MQTT_RETCODE_ACCEPTED) {
1.1.2.3 ! misho 198: for (i = 0; i < num; i++)
! 199: if ((ret = Ping(sock, i))) {
! 200: ret = 3;
! 201: lost++;
! 202: }
1.1.2.1 misho 203: CloseClient(sock);
1.1.2.3 ! misho 204:
! 205: printf("\nMQTT ping sended %d requests with %d lost responses\n", num, lost);
1.1.2.1 misho 206: } else {
207: close(sock);
208: ret = 4;
209: }
210:
211: args->free(args);
212: free(args);
213: return ret;
214: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>