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

    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-p <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-D\t\t\tDaemon mode\n"
   32: 		"\t-v\t\t\tVerbose (more -vvv, more verbose)\n"
   33: 		"\t-h\t\t\tHelp! This screen\n\n", 
   34: 		compiledby, compilehost, compiled);
   35: }
   36: 
   37: static void
   38: cleanArgs(struct tagArgs * __restrict args)
   39: {
   40: 	AIT_FREE_VAL(&args->Publish);
   41: 	AIT_FREE_VAL(&args->Value);
   42: 	io_freeVars(&args->Subscribes);
   43: }
   44: 
   45: 
   46: int
   47: main(int argc, char **argv)
   48: {
   49: 	char ch, batch = 1, szStr[STRSIZ] = { 0 };
   50: 	ait_val_t *v, val;
   51: 	u_short port = atoi(MQTT_PORT);
   52: 	int sock;
   53: 
   54: 	if (!(args = malloc(sizeof(struct tagArgs)))) {
   55: 		printf("Error:: in arguments #%d - %s\n", errno, strerror(errno));
   56: 		return 1;
   57: 	} else if (!(args->Subscribes = io_allocVars(1))) {
   58: 		printf("Error:: in subscribes array #%d - %s\n", io_GetErrno(), io_GetError());
   59: 		free(args);
   60: 		return 1;
   61: 	} else
   62: 		args->free = cleanArgs;
   63: 
   64: 	strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName);
   65: 	while ((ch = getopt(argc, argv, "P:p:s:q:drc:fDvh")) != -1)
   66: 		switch (ch) {
   67: 			case 'P':
   68: 				port = (u_short) strtol(optarg, NULL, 0);
   69: 				break;
   70: 			case 'p':
   71: 				AIT_FREE_VAL(&args->Publish);
   72: 				AIT_SET_STR(&args->Publish, optarg);
   73: 				break;
   74: 			case 's':
   75: 				v = malloc(sizeof(ait_val_t));
   76: 				if (!v) {
   77: 					printf("Error:: not enough memory #%d - %s\n", errno, strerror(errno));
   78: 					args->free(args);
   79: 					free(args);
   80: 					return 1;
   81: 				} else
   82: 					AIT_SET_STR(v, optarg);
   83: 				io_arrayElem(args->Subscribes, io_arraySize(args->Subscribes), (void**) &v);
   84: 				break;
   85: 			case 'q':
   86: 				args->QoS = (char) strtol(optarg, NULL, 0);
   87: 				if (args->QoS < MQTT_QOS_ONCE || args->QoS > MQTT_QOS_EXACTLY) {
   88: 					printf("Error:: invalid QoS level %d\n", args->QoS);
   89: 					args->free(args);
   90: 					free(args);
   91: 					return 1;
   92: 				}
   93: 				break;
   94: 			case 'd':
   95: 				args->Dup++;
   96: 				break;
   97: 			case 'r':
   98: 				args->Retain++;
   99: 				break;
  100: 			case 'f':
  101: 				args->isFile++;
  102: 				break;
  103: 			case 'c':
  104: 				strlcpy(szCfgName, optarg, sizeof szCfgName);
  105: 				break;
  106: 			case 'D':
  107: 				batch = 0;
  108: 				break;
  109: 			case 'v':
  110: 				Verbose++;
  111: 				break;
  112: 			case 'h':
  113: 			default:
  114: 				args->free(args);
  115: 				free(args);
  116: 				Usage();
  117: 				return 1;
  118: 		}
  119: 	argc -= optind;
  120: 	argv += optind;
  121: 	if (!argc) {
  122: 		printf("Error:: host for connect not found!\n");
  123: 		args->free(args);
  124: 		free(args);
  125: 		Usage();
  126: 		return 1;
  127: 	}
  128: 	if (argc > 1)
  129: 		AIT_SET_STR(&args->Value, argv[1]);
  130: 	if (!io_gethostbyname(*argv, port, &args->addr)) {
  131: 		printf("Error:: host not connect #%d - %s\n", io_GetErrno(), io_GetError());
  132: 		args->free(args);
  133: 		free(args);
  134: 		Usage();
  135: 		return 1;
  136: 	}
  137: 	VERB(1) printf("Connecting to %s:%d ...\n", io_n2addr(&args->addr, &val), io_n2port(&args->addr));
  138: 
  139: 	if (LoadConfig(szCfgName, &cfg)) {
  140: 		printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError());
  141: 		args->free(args);
  142: 		free(args);
  143: 		return 1;
  144: 	}
  145: 
  146: 	if ((sock = InitClient()) == -1) {
  147: 		args->free(args);
  148: 		free(args);
  149: 		return 2;
  150: 	}
  151: 
  152: 	shutdown(sock, SHUT_RDWR);
  153: 	close(sock);
  154: 
  155: 	UnloadConfig(&cfg);
  156: 	args->free(args);
  157: 	free(args);
  158: 	return 0;
  159: }

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