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