File:  [ELWIX - Embedded LightWeight unIX -] / mqtt / src / Attic / mqtt.c
Revision 1.1.1.1.2.23: download - view: text, annotated - select for diffs - revision graph
Tue Dec 13 09:01:43 2011 UTC (12 years, 6 months ago) by misho
Branches: mqtt1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
add user/pass

    1: #include "global.h"
    2: #include "rtlm.h"
    3: #include "mqtt.h"
    4: #include "client.h"
    5: 
    6: 
    7: sl_config cfg;
    8: sqlite3 *acc, *pub;
    9: FILE *logg;
   10: extern char compiled[], compiledby[], compilehost[];
   11: static char szCfgName[MAXPATHLEN];
   12: int Verbose, Kill;
   13: 
   14: struct tagArgs *args;
   15: 
   16: 
   17: static void
   18: Usage(void)
   19: {
   20: 	printf(	" -= MQTT Client =- Publisher/Subscriber from ELWIX\n"
   21: 		"=== %s@%s === Compiled: %s ===\n\n"
   22: 		" Syntax: mqtt [options] <connect_to_broker[:port]> [value_for_publish]\n\n"
   23: 		"\t-t <topic>\t\tPublish topic\n"
   24: 		"\t-s <topic[|QoS]>\tSubscribe for this topic, if wish add different |QoS to topic\n"
   25: 		"\t-q <QoS>\t\tQoS level (0-at most 1, 1-at least 1, 2-exactly 1)\n"
   26: 		"\t-d\t\t\tSend duplicate message\n"
   27: 		"\t-r\t\t\tRetain message from broker\n"
   28: 		"\t-c <config>\t\tService config\n"
   29: 		"\t-f\t\t\t'value_for_publish' is file instead text\n"
   30: 		"\t-p <port>\t\tDifferent port for connect (default: 1883)\n"
   31: 		"\t-U <username>\t\tUsername\n"
   32: 		"\t-P <password>\t\tPassword\n"
   33: 		"\t-D\t\t\tDaemon mode\n"
   34: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   35: 		"\t-h\t\t\tHelp! This screen\n\n", 
   36: 		compiledby, compilehost, compiled);
   37: }
   38: 
   39: static void
   40: cleanArgs(struct tagArgs * __restrict args)
   41: {
   42: 	AIT_FREE_VAL(&args->User);
   43: 	AIT_FREE_VAL(&args->Pass);
   44: 	AIT_FREE_VAL(&args->Publish);
   45: 	AIT_FREE_VAL(&args->Value);
   46: 	io_freeVars(&args->Subscribes);
   47: }
   48: 
   49: 
   50: int
   51: main(int argc, char **argv)
   52: {
   53: 	char ch, batch = 1, szStr[STRSIZ] = { 0 };
   54: 	ait_val_t *v, val;
   55: 	u_short port = atoi(MQTT_PORT);
   56: 	int sock;
   57: 
   58: 	if (!(args = malloc(sizeof(struct tagArgs)))) {
   59: 		printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
   60: 		return 1;
   61: 	} else if (!(args->Subscribes = io_allocVars(1))) {
   62: 		printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
   63: 		free(args);
   64: 		return 1;
   65: 	} else
   66: 		args->free = cleanArgs;
   67: 
   68: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
   69: 	while ((ch = getopt(argc, argv, "U:P:p:t:s:q:drc:fDvh")) != -1)
   70: 		switch (ch) {
   71: 			case 'U':
   72: 				AIT_FREE_VAL(&args->User);
   73: 				AIT_SET_STR(&args->User, optarg);
   74: 				break;
   75: 			case 'P':
   76: 				AIT_FREE_VAL(&args->Pass);
   77: 				AIT_SET_STR(&args->Pass, optarg);
   78: 				break;
   79: 			case 'p':
   80: 				port = (u_short) strtol(optarg, NULL, 0);
   81: 				break;
   82: 			case 't':
   83: 				AIT_FREE_VAL(&args->Publish);
   84: 				AIT_SET_STR(&args->Publish, optarg);
   85: 				break;
   86: 			case 's':
   87: 				v = malloc(sizeof(ait_val_t));
   88: 				if (!v) {
   89: 					printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
   90: 					args->free(args);
   91: 					free(args);
   92: 					return 1;
   93: 				} else
   94: 					AIT_SET_STR(v, optarg);
   95: 				io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), (void**) &v);
   96: 				break;
   97: 			case 'q':
   98: 				args->QoS = (char) strtol(optarg, NULL, 0);
   99: 				if (args->QoS < MQTT_QOS_ONCE || args->QoS > MQTT_QOS_EXACTLY) {
  100: 					printf("Error:: invalid QoS level %d\n", args->QoS);
  101: 					args->free(args);
  102: 					free(args);
  103: 					return 1;
  104: 				}
  105: 				break;
  106: 			case 'd':
  107: 				args->Dup++;
  108: 				break;
  109: 			case 'r':
  110: 				args->Retain++;
  111: 				break;
  112: 			case 'f':
  113: 				args->isFile++;
  114: 				break;
  115: 			case 'c':
  116: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
  117: 				break;
  118: 			case 'D':
  119: 				batch = 0;
  120: 				break;
  121: 			case 'v':
  122: 				Verbose++;
  123: 				break;
  124: 			case 'h':
  125: 			default:
  126: 				args->free(args);
  127: 				free(args);
  128: 				Usage();
  129: 				return 1;
  130: 		}
  131: 	argc -= optind;
  132: 	argv += optind;
  133: 	if (!argc) {
  134: 		printf("Error:: host for connect not found!\n");
  135: 		args->free(args);
  136: 		free(args);
  137: 		Usage();
  138: 		return 1;
  139: 	}
  140: 	if (argc > 1)
  141: 		AIT_SET_STR(&args->Value, argv[1]);
  142: 	if (!io_gethostbyname(*argv, port, &args->addr)) {
  143: 		printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
  144: 		args->free(args);
  145: 		free(args);
  146: 		Usage();
  147: 		return 1;
  148: 	}
  149: 	VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
  150: 
  151: 	if (LoadConfig(szCfgName, &cfg)) {
  152: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
  153: 		args->free(args);
  154: 		free(args);
  155: 		return 1;
  156: 	}
  157: 
  158: 	if ((sock = InitClient()) == -1) {
  159: 		args->free(args);
  160: 		free(args);
  161: 		return 2;
  162: 	}
  163: 
  164: 	shutdown(sock, SHUT_RDWR);
  165: 	close(sock);
  166: 
  167: 	UnloadConfig(&cfg);
  168: 	args->free(args);
  169: 	free(args);
  170: 	return 0;
  171: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>