--- mqtt/src/Attic/mqtt.c 2011/12/08 17:02:25 1.1.1.1.2.19 +++ mqtt/src/Attic/mqtt.c 2011/12/12 09:24:25 1.1.1.1.2.20 @@ -1,123 +1,80 @@ #include "global.h" #include "rtlm.h" +#include "mqtt.h" sl_config cfg; +sqlite3 *acc, *pub; +FILE *logg; +extern char compiled[], compiledby[], compilehost[]; +static char szCfgName[MAXPATHLEN]; +int Verbose, Kill; +char szPublish[STRSIZ]; + +static void +Usage(void) +{ + printf( " -= MQTT Client =- Publisher/Subscriber from ELWIX\n" + "=== %s@%s === Compiled: %s ===\n\n" + " Syntax: mqtt [options] [value_for_publish]\n\n" + "\t-p \tPublish topic\n" + "\t-s \tSubscribe for this topic\n" + "\t-q \tQoS level (0-at most 1, 1-at least 1, 2-explicit 1)\n" + "\t-d\t\tSend duplicate message\n" + "\t-r\t\tRetain message from broker\n" + "\t-c \tService config\n" + "\t-f\t\t'value_for_publish' is file instead text\n" + "\t-D\t\tDaemon mode\n" + "\t-v\t\tVerbose (more -vvv, more verbose)\n" + "\t-h\t\tHelp! This screen\n\n", + compiledby, compilehost, compiled); +} + + int main(int argc, char **argv) { - sqlite3 *acc = NULL, *pub = NULL; - FILE *logg = NULL; - int ret = 0; - mqtt_subscr_t *s, *p; + char ch, batch = 1, szStr[STRSIZ] = { 0 }; - if (LoadConfig("/etc/mqtt.conf", &cfg)) { - printf("Error:: Load config #%d - %s\n", cfg_GetErrno(), cfg_GetError()); + strlcpy(szCfgName, DEFAULT_CONFIG, sizeof szCfgName); + while ((ch = getopt(argc, argv, "p:s:q:drc:fDvh")) != -1) + switch (ch) { + case 'p': + break; + case 's': + break; + case 'q': + break; + case 'd': + break; + case 'r': + break; + case 'f': + break; + case 'c': + strlcpy(szCfgName, optarg, sizeof szCfgName); + break; + case 'D': + batch = 0; + break; + case 'v': + Verbose++; + break; + case 'h': + default: + Usage(); + return 1; + } + argc -= optind; + argv += optind; + + if (LoadConfig(szCfgName, &cfg)) { + printf("Error:: can't load #%d - %s\n", cfg_GetErrno(), cfg_GetError()); return 1; } - if (!mqttLoadRTLM(&cfg, 0)) { - printf("Error:: Can't load RTL ACC module\n"); - UnloadConfig(&cfg); - return 2; - } - if (!mqttLoadRTLM(&cfg, 1)) { - printf("Error:: Can't load RTL PUB module\n"); - mqttUnloadRTLM(0); - UnloadConfig(&cfg); - return 2; - } - if (!mqttLoadRTLM(&cfg, 2)) { - printf("Error:: Can't load RTL LOG module\n"); - mqttUnloadRTLM(1); - mqttUnloadRTLM(0); - UnloadConfig(&cfg); - return 2; - } - acc = call.OpenACC(&cfg); - if (!acc) - goto end; - pub = call.OpenPUB(&cfg); - if (!pub) - goto end; - logg = call.OpenLOG(&cfg); - if (!logg) - goto end; - - if (mqttMkDir(&cfg)) { - printf("Error:: in statedir #%d - %s\n", errno, strerror(errno)); - goto end; - } - - call.LOG(logg, "success!\n"); - if ((ret = call.LoginACC(&cfg, acc, "misho", "test123")) == -1) { - printf("Error:: Authentication problem\n"); - goto end; - } else - call.LOG(logg, "Login: %s\n", ret ? "ALLOW" : "DENIED"); - - if ((ret = call.InitSessPUB(&cfg, pub, "misho", "doophenschmerz", "127.0.0.1", 12345, 1, - "Topic43", "mmm", 2, 0)) == -1) { - printf("Error:: Session init problem\n"); - goto end; - } else - call.LOG(logg, "InitSess: %d\n", ret); - - if ((ret = call.ChkSessPUB(&cfg, pub, "misho", "doophenschmerz", "127.0._.%")) == -1) { - printf("Error:: Session check problem\n"); - goto end; - } else - call.LOG(logg, "ChkSess: %d\n", ret); - - if ((ret = call.DeletePUB(&cfg, pub, 10, "%", "%", "%", -1)) == -1) { - printf("Error:: Clear problem\n"); - goto end; - } else - call.LOG(logg, "Clear: %d\n", ret); - - if ((ret = call.WritePUB(&cfg, pub, 10, "oho/boho", "MRYN \n tryn brymbryn", "misho", "1.1.1.1", 1)) == -1) { - printf("Error:: Publish problem\n"); - goto end; - } else - call.LOG(logg, "Publish: %d\n", ret); - if ((ret = call.WritePUB(&cfg, pub, 11, "boh", "testing", "misho", "1.1.1.2", 0)) == -1) { - printf("Error:: Publish problem\n"); - goto end; - } else - call.LOG(logg, "Publish: %d\n", ret); - if (!(s = call.ReadPUB(&cfg, pub, 10, "%", -1))) { - printf("Error:: Subscribe problem\n"); - goto end; - } else { - call.LOG(logg, "Subscribe: %p\n", s); - for (p = s; p->sub_topic._base; p++) { - printf("Retain=%d Topic(%d)=%s Value(%d)=%s\n", p->sub_ret, - p->sub_topic._size, p->sub_topic._base, - p->sub_value._size, p->sub_value._base); - } - mqtt_subFree(&s); - } - if ((ret = call.DeletePUB(&cfg, pub, 11, "boh", "misho", "1.1.1._", 0)) == -1) { - printf("Error:: Delete problem\n"); - goto end; - } else - call.LOG(logg, "Delete: %d\n", ret); - - if ((ret = call.FiniSessPUB(&cfg, pub, "misho", "doophenschmerz", "127.0._.1%")) == -1) { - printf("Error:: Session fini problem\n"); - goto end; - } else - call.LOG(logg, "FiniSess: %d\n", ret); - -end: - call.CloseLOG(logg); - call.ClosePUB(pub); - call.CloseACC(acc); - mqttUnloadRTLM(2); - mqttUnloadRTLM(1); - mqttUnloadRTLM(0); UnloadConfig(&cfg); return 0; }