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