Annotation of mqtt/src/mqtt_ping.c, revision 1.2.2.1
1.2.2.1 ! misho 1: /*************************************************************************
! 2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
! 3: * by Michael Pounov <misho@openbsd-bg.org>
! 4: *
! 5: * $Author: misho $
! 6: * $Id: global.h,v 1.4 2012/07/03 08:57:04 misho Exp $
! 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:
! 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
! 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: io_enableDEBUG;
52:
53: extern char compiled[], compiledby[], compilehost[];
54:
55: struct tagArgs *args;
56:
57:
58: static void
59: Usage(void)
60: {
61: printf( " -= MQTT PING =- MQTT Ping from ELWIX\n"
62: "=== %s@%s === Compiled: %s ===\n\n"
63: " Syntax: mqtt_ping [options] <connect_to_broker[:port]> <ConnectID>\n\n"
64: "\t-c <num>\t\tNumber of ping requests (default: 1)\n"
65: "\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
66: "\t-T <timeout>\t\tKeep alive timeout in seconds (default: 10sec)\n"
67: "\t-U <username>\t\tUsername\n"
68: "\t-P <password>\t\tPassword\n"
69: "\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
70: "\t-h\t\t\tHelp! This screen\n\n",
71: compiledby, compilehost, compiled);
72: }
73:
74: static void
75: cleanArgs(struct tagArgs * __restrict args)
76: {
77: mqtt_msgFree(&args->msg, 42);
78: AIT_FREE_VAL(&args->User);
79: AIT_FREE_VAL(&args->Pass);
80: AIT_FREE_VAL(&args->ConnID);
81: }
82:
83: static int
84: Ping(int sock, int num, struct timeval * __restrict accu)
85: {
86: struct timeval before, after, calc;
87: ait_val_t val;
88:
89: gettimeofday(&before, NULL);
90:
91: if (mqtt_KeepAlive(args->cli->sock, args->ka, 1) == -1)
92: return -1;
93:
94: gettimeofday(&after, NULL);
95:
96: timersub(&after, &before, &calc);
97: timeradd(accu, &calc, accu);
98:
99: printf(" + Ping %d MQTT broker %s ... %f sec.\n", num, io_n2addr(&args->addr, &val),
100: calc.tv_sec + calc.tv_usec / 1.e6);
101: AIT_FREE_VAL(&val);
102:
103: return 0;
104: }
105:
106:
107: int
108: main(int argc, char **argv)
109: {
110: char ch;
111: ait_val_t val;
112: u_short port = atoi(MQTT_PORT);
113: int num = 1, ret = 0, lost = 0;
114: register int i;
115: struct timeval accu;
116:
117: if (!(args = io_malloc(sizeof(struct tagArgs)))) {
118: printf("Error:: in alloc arguments #%d - %s\n", errno, strerror(errno));
119: return 1;
120: } else
121: memset(args, 0, sizeof(struct tagArgs));
122: args->free = cleanArgs;
123:
124: if (!(args->msg = mqtt_msgAlloc(USHRT_MAX))) {
125: printf("Error:: in mqtt buffer #%d - %s\n", mqtt_GetErrno(), mqtt_GetError());
126: args->free(args);
127: io_free(args);
128: return 1;
129: }
130:
131: AIT_SET_STR(&args->ConnID, "");
132: AIT_SET_STR(&args->User, "");
133: AIT_SET_STR(&args->Pass, "");
134:
135: args->ka = MQTT_KEEPALIVE;
136: while ((ch = getopt(argc, argv, "T:U:P:p:c:vh")) != -1)
137: switch (ch) {
138: case 'c':
139: num = (int) strtol(optarg, NULL, 0);
140: break;
141: case 'T':
142: args->ka = (u_short) strtol(optarg, NULL, 0);
143: break;
144: case 'U':
145: AIT_FREE_VAL(&args->User);
146: AIT_SET_STR(&args->User, optarg);
147: break;
148: case 'P':
149: AIT_FREE_VAL(&args->Pass);
150: AIT_SET_STR(&args->Pass, optarg);
151: break;
152: case 'p':
153: port = (u_short) strtol(optarg, NULL, 0);
154: break;
155: case 'v':
156: io_incDebug;
157: break;
158: case 'h':
159: default:
160: args->free(args);
161: io_free(args);
162: Usage();
163: return 1;
164: }
165: argc -= optind;
166: argv += optind;
167: if (argc < 2) {
168: printf("Error:: host for connect not found or connection id!\n\n");
169: args->free(args);
170: io_free(args);
171: Usage();
172: return 1;
173: } else {
174: AIT_FREE_VAL(&args->ConnID);
175: AIT_SET_STR(&args->ConnID, argv[1]);
176: }
177: if (!io_gethostbyname(*argv, port, &args->addr)) {
178: printf("Error:: host not valid #%d - %s\n", io_GetErrno(), io_GetError());
179: args->free(args);
180: io_free(args);
181: Usage();
182: return 1;
183: }
184: printf("Connecting to %s:%d ... ", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
185: AIT_FREE_VAL(&val);
186:
187: if (!(args->cli = mqtt_cli_Open(&args->addr.sa, args->ka))) {
188: args->free(args);
189: io_free(args);
190: return 2;
191: }
192:
193: switch ((ret = ConnectClient(args->cli->sock))) {
194: case -1:
195: printf(">> FAILED!\n");
196: break;
197: case MQTT_RETCODE_ACCEPTED:
198: printf(">> OK\n");
199: break;
200: case MQTT_RETCODE_REFUSE_VER:
201: printf(">> Incorrect version\n");
202: break;
203: case MQTT_RETCODE_REFUSE_ID:
204: printf(">> Incorrect connectID\n");
205: break;
206: case MQTT_RETCODE_REFUSE_UNAVAIL:
207: printf(">> Service unavailable\n");
208: break;
209: case MQTT_RETCODE_REFUSE_USERPASS:
210: printf(">> Refuse user/pass\n");
211: break;
212: case MQTT_RETCODE_DENIED:
213: printf(">> DENIED.\n");
214: break;
215: }
216:
217: timerclear(&accu);
218: if (ret == MQTT_RETCODE_ACCEPTED) {
219: for (i = 0; i < num; i++)
220: if ((ret = Ping(args->cli->sock, i, &accu))) {
221: ret = 3;
222: lost++;
223: }
224: printf("\nMQTT ping sended %d requests with %d lost responses and avarage %f sec.\n",
225: num, lost, (accu.tv_sec + accu.tv_usec / 1.e6) / (num - lost));
226: } else
227: ret = 4;
228:
229: mqtt_cli_Close(&args->cli);
230:
231: args->free(args);
232: io_free(args);
233: return ret;
234: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>