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