Annotation of mqtt/src/mqtt_ping.c, revision 1.1.2.4
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: struct timeval before, after;
42: ait_val_t val;
43:
44: gettimeofday(&before, NULL);
45:
1.1.2.4 ! misho 46: if (mqtt_KeepAlive(args->cli->sock, args->ka, 1) == -1)
1.1.2.1 misho 47: return -1;
48:
49: gettimeofday(&after, NULL);
50:
1.1.2.3 misho 51: printf(" + Ping %d MQTT broker %s ... %f sec.\n", num, io_n2addr(&args->addr, &val),
1.1.2.1 misho 52: ((after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1.e6));
53: AIT_FREE_VAL(&val);
54:
55: return 0;
56: }
57:
58:
59: int
60: main(int argc, char **argv)
61: {
62: char ch;
63: ait_val_t val;
64: u_short port = atoi(MQTT_PORT);
1.1.2.4 ! misho 65: int num = 1, ret = 0, lost = 0;
1.1.2.3 misho 66: register int i;
1.1.2.1 misho 67:
68: if (!(args = malloc(sizeof(struct tagArgs)))) {
69: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
70: return 1;
71: } else
72: memset(args, 0, sizeof(struct tagArgs));
73: args->free = cleanArgs;
74:
75: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
76: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
77: args->free(args);
78: free(args);
79: return 1;
80: }
81:
82: AIT_SET_STR(&args->ConnID, "");
83: AIT_SET_STR(&args->User, "");
84: AIT_SET_STR(&args->Pass, "");
85:
86: args->ka = MQTT_KEEPALIVE;
1.1.2.3 misho 87: while ((ch = getopt(argc, argv, "T:U:P:p:c:vh")) != -1)
1.1.2.1 misho 88: switch (ch) {
1.1.2.3 misho 89: case 'c':
90: num = (int) strtol(optarg, NULL, 0);
1.1.2.1 misho 91: case 'T':
92: args->ka = (u_short) strtol(optarg, NULL, 0);
93: break;
94: case 'U':
95: AIT_FREE_VAL(&args->User);
96: AIT_SET_STR(&args->User, optarg);
97: break;
98: case 'P':
99: AIT_FREE_VAL(&args->Pass);
100: AIT_SET_STR(&args->Pass, optarg);
101: break;
102: case 'p':
103: port = (u_short) strtol(optarg, NULL, 0);
104: break;
105: case 'v':
106: io_incDebug;
107: break;
108: case 'h':
109: default:
110: args->free(args);
111: free(args);
112: Usage();
113: return 1;
114: }
115: argc -= optind;
116: argv += optind;
117: if (argc < 2) {
118: printf("Error:: host for connect not found or connection id!\n\n");
119: args->free(args);
120: free(args);
121: Usage();
122: return 1;
123: } else {
124: AIT_FREE_VAL(&args->ConnID);
125: AIT_SET_STR(&args->ConnID, argv[1]);
126: }
127: if (!io_gethostbyname(*argv, port, &args->addr)) {
128: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
129: args->free(args);
130: free(args);
131: Usage();
132: return 1;
133: }
1.1.2.2 misho 134: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
1.1.2.1 misho 135: AIT_FREE_VAL(&val);
136:
1.1.2.4 ! misho 137: if (!(args->cli = mqtt_cli_Open(&args->addr.sa))) {
1.1.2.1 misho 138: args->free(args);
139: free(args);
140: return 2;
141: }
142:
1.1.2.4 ! misho 143: switch ((ret = ConnectClient(args->cli->sock))) {
1.1.2.1 misho 144: case -1:
145: printf(">> FAILED!\n");
146: break;
147: case MQTT_RETCODE_ACCEPTED:
148: printf(">> OK\n");
149: break;
150: case MQTT_RETCODE_REFUSE_VER:
151: printf(">> Incorrect version\n");
152: break;
153: case MQTT_RETCODE_REFUSE_ID:
154: printf(">> Incorrect connectID\n");
155: break;
156: case MQTT_RETCODE_REFUSE_UNAVAIL:
157: printf(">> Service unavailable\n");
158: break;
159: case MQTT_RETCODE_REFUSE_USERPASS:
160: printf(">> Refuse user/pass\n");
161: break;
162: case MQTT_RETCODE_DENIED:
163: printf(">> DENIED.\n");
164: break;
165: }
166:
167: if (ret == MQTT_RETCODE_ACCEPTED) {
1.1.2.3 misho 168: for (i = 0; i < num; i++)
1.1.2.4 ! misho 169: if ((ret = Ping(args->cli->sock, i))) {
1.1.2.3 misho 170: ret = 3;
171: lost++;
172: }
173: printf("\nMQTT ping sended %d requests with %d lost responses\n", num, lost);
1.1.2.4 ! misho 174: } else
1.1.2.1 misho 175: ret = 4;
1.1.2.4 ! misho 176:
! 177: mqtt_cli_Close(&args->cli);
1.1.2.1 misho 178:
179: args->free(args);
180: free(args);
181: return ret;
182: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>