Annotation of mqtt/src/mqtt_ping.c, revision 1.3.4.1
1.3 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.3.4.1 ! misho 6: * $Id: mqtt_ping.c,v 1.3 2012/07/03 12:46:00 misho Exp $
1.3 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.3.4.1 ! misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.3 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.2 misho 46: #include "global.h"
47: #include "mqtt.h"
48: #include "client.h"
49:
50:
51: extern char compiled[], compiledby[], compilehost[];
52:
53: struct tagArgs *args;
54:
55:
56: static void
57: Usage(void)
58: {
59: printf( " -= MQTT PING =- MQTT Ping from ELWIX\n"
60: "=== %s@%s === Compiled: %s ===\n\n"
61: " Syntax: mqtt_ping [options] <connect_to_broker[:port]> <ConnectID>\n\n"
62: "\t-c <num>\t\tNumber of ping requests (default: 1)\n"
63: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
64: "\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
65: "\t-U <username>\t\tUsername\n"
66: "\t-P <password>\t\tPassword\n"
67: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
68: "\t-h\t\t\tHelp! This screen\n\n",
69: compiledby, compilehost, compiled);
70: }
71:
72: static void
73: cleanArgs(struct tagArgs * __restrict args)
74: {
75: mqtt_msgFree(&args->msg, 42);
76: AIT_FREE_VAL(&args->User);
77: AIT_FREE_VAL(&args->Pass);
78: AIT_FREE_VAL(&args->ConnID);
79: }
80:
81: static int
82: Ping(int sock, int num, struct timeval * __restrict accu)
83: {
84: struct timeval before, after, calc;
85: ait_val_t val;
86:
87: gettimeofday(&before, NULL);
88:
89: if (mqtt_KeepAlive(args->cli->sock, args->ka, 1) == -1)
90: return -1;
91:
92: gettimeofday(&after, NULL);
93:
94: timersub(&after, &before, &calc);
95: timeradd(accu, &calc, accu);
96:
1.3.4.1 ! misho 97: printf(" + Ping %d MQTT broker %s ... %f sec.\n", num, e_n2addr(&args->addr, &val),
1.2 misho 98: calc.tv_sec + calc.tv_usec / 1.e6);
99: AIT_FREE_VAL(&val);
100:
101: return 0;
102: }
103:
104:
105: int
106: main(int argc, char **argv)
107: {
108: char ch;
109: ait_val_t val;
110: u_short port = atoi(MQTT_PORT);
111: int num = 1, ret = 0, lost = 0;
112: register int i;
113: struct timeval accu;
114:
1.3.4.1 ! misho 115: if (!(args = e_malloc(sizeof(struct tagArgs)))) {
1.2 misho 116: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
117: return 1;
118: } else
119: memset(args, 0, sizeof(struct tagArgs));
120: args->free = cleanArgs;
121:
122: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
123: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
124: args->free(args);
1.3.4.1 ! misho 125: e_free(args);
1.2 misho 126: return 1;
127: }
128:
129: AIT_SET_STR(&args->ConnID, "");
130: AIT_SET_STR(&args->User, "");
131: AIT_SET_STR(&args->Pass, "");
132:
133: args->ka = MQTT_KEEPALIVE;
134: while ((ch = getopt(argc, argv, "T:U:P:p:c:vh")) != -1)
135: switch (ch) {
136: case 'c':
137: num = (int) strtol(optarg, NULL, 0);
138: break;
139: case 'T':
140: args->ka = (u_short) strtol(optarg, NULL, 0);
141: break;
142: case 'U':
143: AIT_FREE_VAL(&args->User);
144: AIT_SET_STR(&args->User, optarg);
145: break;
146: case 'P':
147: AIT_FREE_VAL(&args->Pass);
148: AIT_SET_STR(&args->Pass, optarg);
149: break;
150: case 'p':
151: port = (u_short) strtol(optarg, NULL, 0);
152: break;
153: case 'v':
1.3.4.1 ! misho 154: e_incVerbose;
1.2 misho 155: break;
156: case 'h':
157: default:
158: args->free(args);
1.3.4.1 ! misho 159: e_free(args);
1.2 misho 160: Usage();
161: return 1;
162: }
163: argc -= optind;
164: argv += optind;
165: if (argc < 2) {
166: printf("Error:: host for connect not found or connection id!\n\n");
167: args->free(args);
1.3.4.1 ! misho 168: e_free(args);
1.2 misho 169: Usage();
170: return 1;
171: } else {
172: AIT_FREE_VAL(&args->ConnID);
173: AIT_SET_STR(&args->ConnID, argv[1]);
174: }
1.3.4.1 ! misho 175: if (!e_gethostbyname(*argv, port, &args->addr)) {
! 176: printf("Error:: host not valid #%d - %s\n", elwix_GetErrno(), elwix_GetError());
1.2 misho 177: args->free(args);
1.3.4.1 ! misho 178: e_free(args);
1.2 misho 179: Usage();
180: return 1;
181: }
1.3.4.1 ! misho 182: printf("Connecting to %s:%d ... ", e_n2addr(&args->addr, &val), e_n2port(&args->addr));
1.2 misho 183: AIT_FREE_VAL(&val);
184:
185: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
186: args->free(args);
1.3.4.1 ! misho 187: e_free(args);
1.2 misho 188: return 2;
189: }
190:
191: switch ((ret = ConnectClient(args->cli->sock))) {
192: case -1:
193: printf(">> FAILED!\n");
194: break;
195: case MQTT_RETCODE_ACCEPTED:
196: printf(">> OK\n");
197: break;
198: case MQTT_RETCODE_REFUSE_VER:
199: printf(">> Incorrect version\n");
200: break;
201: case MQTT_RETCODE_REFUSE_ID:
202: printf(">> Incorrect connectID\n");
203: break;
204: case MQTT_RETCODE_REFUSE_UNAVAIL:
205: printf(">> Service unavailable\n");
206: break;
207: case MQTT_RETCODE_REFUSE_USERPASS:
208: printf(">> Refuse user/pass\n");
209: break;
210: case MQTT_RETCODE_DENIED:
211: printf(">> DENIED.\n");
212: break;
213: }
214:
215: timerclear(&accu);
216: if (ret == MQTT_RETCODE_ACCEPTED) {
217: for (i = 0; i < num; i++)
218: if ((ret = Ping(args->cli->sock, i, &accu))) {
219: ret = 3;
220: lost++;
221: }
222: printf("\nMQTT ping sended %d requests with %d lost responses and avarage %f sec.\n",
223: num, lost, (accu.tv_sec + accu.tv_usec / 1.e6) / (num - lost));
224: } else
225: ret = 4;
226:
227: mqtt_cli_Close(&args->cli);
228:
229: args->free(args);
1.3.4.1 ! misho 230: e_free(args);
1.2 misho 231: return ret;
232: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>