From 5cdfe3239c70d55aa977ffbe961bbdd0a389d180 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Thu, 15 Mar 2018 11:21:42 +0000 Subject: [PATCH] Separate sub client output code into its own file. --- client/CMakeLists.txt | 2 +- client/Makefile | 5 +- client/sub_client.c | 277 +------------------------------- client/sub_client_output.c | 315 +++++++++++++++++++++++++++++++++++++ 4 files changed, 321 insertions(+), 278 deletions(-) create mode 100644 client/sub_client_output.c diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 0b6f4202..04577564 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -9,7 +9,7 @@ if (${WITH_SRV} STREQUAL ON) endif (${WITH_SRV} STREQUAL ON) add_executable(mosquitto_pub pub_client.c ${shared_src}) -add_executable(mosquitto_sub sub_client.c ${shared_src}) +add_executable(mosquitto_sub sub_client.c sub_client_output.c ${shared_src}) target_link_libraries(mosquitto_pub libmosquitto) target_link_libraries(mosquitto_sub libmosquitto) diff --git a/client/Makefile b/client/Makefile index 855946a1..ac9c979a 100644 --- a/client/Makefile +++ b/client/Makefile @@ -17,7 +17,7 @@ static_sub : sub_client.o client_shared.o ../lib/libmosquitto.a mosquitto_pub : pub_client.o client_shared.o ${CROSS_COMPILE}${CC} $^ -o $@ ${CLIENT_LDFLAGS} -mosquitto_sub : sub_client.o client_shared.o +mosquitto_sub : sub_client.o sub_client_output.o client_shared.o ${CROSS_COMPILE}${CC} $^ -o $@ ${CLIENT_LDFLAGS} pub_client.o : pub_client.c ../lib/libmosquitto.so.${SOVERSION} @@ -26,6 +26,9 @@ pub_client.o : pub_client.c ../lib/libmosquitto.so.${SOVERSION} sub_client.o : sub_client.c ../lib/libmosquitto.so.${SOVERSION} ${CROSS_COMPILE}${CC} -c $< -o $@ ${CLIENT_CFLAGS} +sub_client_output.o : sub_client_output.c ../lib/libmosquitto.so.${SOVERSION} + ${CROSS_COMPILE}${CC} -c $< -o $@ ${CLIENT_CFLAGS} + client_shared.o : client_shared.c client_shared.h ${CROSS_COMPILE}${CC} -c $< -o $@ ${CLIENT_CFLAGS} diff --git a/client/sub_client.c b/client/sub_client.c index 280dca56..528bc8bd 100644 --- a/client/sub_client.c +++ b/client/sub_client.c @@ -34,282 +34,7 @@ Contributors: bool process_messages = true; int msg_count = 0; -static int get_time(struct tm **ti, long *ns) -{ -#ifdef WIN32 - SYSTEMTIME st; -#else - struct timespec ts; -#endif - time_t s; - -#ifdef WIN32 - s = time(NULL); - - GetLocalTime(&st); - *ns = st.wMilliseconds*1000000L; -#else - if(clock_gettime(CLOCK_REALTIME, &ts) != 0){ - fprintf(stderr, "Error obtaining system time.\n"); - return 1; - } - s = ts.tv_sec; - *ns = ts.tv_nsec; -#endif - - *ti = localtime(&s); - if(!(*ti)){ - fprintf(stderr, "Error obtaining system time.\n"); - return 1; - } - - return 0; -} - -static void write_payload(const unsigned char *payload, int payloadlen, bool hex) -{ - int i; - - if(!hex){ - (void)fwrite(payload, 1, payloadlen, stdout); - }else{ - for(i=0; i=0 && payload[i] < 32)){ - printf("\\u%04d", payload[i]); - }else{ - fputc(payload[i], stdout); - } - } -} - -void json_print(const struct mosquitto_message *message, const struct tm *ti, bool escaped) -{ - char buf[100]; - - strftime(buf, 100, "%s", ti); - printf("{\"tst\":%s,\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen); - if(message->qos > 0){ - printf("\"mid\":%d,", message->mid); - } - if(escaped){ - fputs("\"payload\":\"", stdout); - write_json_payload(message->payload, message->payloadlen); - fputs("\"}", stdout); - }else{ - fputs("\"payload\":", stdout); - write_payload(message->payload, message->payloadlen, false); - fputs("}", stdout); - } -} - -void formatted_print(const struct mosq_config *cfg, const struct mosquitto_message *message) -{ - int len; - int i; - struct tm *ti = NULL; - long ns; - char strf[3]; - char buf[100]; - - len = strlen(cfg->format); - - for(i=0; iformat[i] == '%'){ - if(i < len-1){ - i++; - switch(cfg->format[i]){ - case '%': - fputc('%', stdout); - break; - - case 'I': - if(!ti){ - if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); - return; - } - } - if(strftime(buf, 100, "%FT%T%z", ti) != 0){ - fputs(buf, stdout); - } - break; - - case 'j': - if(!ti){ - if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); - return; - } - } - json_print(message, ti, true); - break; - - case 'J': - if(!ti){ - if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); - return; - } - } - json_print(message, ti, false); - break; - - case 'l': - printf("%d", message->payloadlen); - break; - - case 'm': - printf("%d", message->mid); - break; - - case 'p': - write_payload(message->payload, message->payloadlen, false); - break; - - case 'q': - fputc(message->qos + 48, stdout); - break; - - case 'r': - if(message->retain){ - fputc('1', stdout); - }else{ - fputc('0', stdout); - } - break; - - case 't': - fputs(message->topic, stdout); - break; - - case 'U': - if(!ti){ - if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); - return; - } - } - if(strftime(buf, 100, "%s", ti) != 0){ - printf("%s.%09ld", buf, ns); - } - break; - - case 'x': - write_payload(message->payload, message->payloadlen, true); - break; - } - } - }else if(cfg->format[i] == '@'){ - if(i < len-1){ - i++; - if(cfg->format[i] == '@'){ - fputc('@', stdout); - }else{ - if(!ti){ - if(get_time(&ti, &ns)){ - fprintf(stderr, "Error obtaining system time.\n"); - return; - } - } - - strf[0] = '%'; - strf[1] = cfg->format[i]; - strf[2] = 0; - - if(cfg->format[i] == 'N'){ - printf("%09ld", ns); - }else{ - if(strftime(buf, 100, strf, ti) != 0){ - fputs(buf, stdout); - } - } - } - } - }else if(cfg->format[i] == '\\'){ - if(i < len-1){ - i++; - switch(cfg->format[i]){ - case '\\': - fputc('\\', stdout); - break; - - case '0': - fputc('\0', stdout); - break; - - case 'a': - fputc('\a', stdout); - break; - - case 'e': - fputc('\033', stdout); - break; - - case 'n': - fputc('\n', stdout); - break; - - case 'r': - fputc('\r', stdout); - break; - - case 't': - fputc('\t', stdout); - break; - - case 'v': - fputc('\v', stdout); - break; - } - } - }else{ - fputc(cfg->format[i], stdout); - } - } - if(cfg->eol){ - fputc('\n', stdout); - } - fflush(stdout); -} - - -void print_message(struct mosq_config *cfg, const struct mosquitto_message *message) -{ - if(cfg->format){ - formatted_print(cfg, message); - }else if(cfg->verbose){ - if(message->payloadlen){ - printf("%s ", message->topic); - write_payload(message->payload, message->payloadlen, false); - if(cfg->eol){ - printf("\n"); - } - }else{ - if(cfg->eol){ - printf("%s (null)\n", message->topic); - } - } - fflush(stdout); - }else{ - if(message->payloadlen){ - write_payload(message->payload, message->payloadlen, false); - if(cfg->eol){ - printf("\n"); - } - fflush(stdout); - } - } -} +void print_message(struct mosq_config *cfg, const struct mosquitto_message *message); void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) diff --git a/client/sub_client_output.c b/client/sub_client_output.c new file mode 100644 index 00000000..b65611e8 --- /dev/null +++ b/client/sub_client_output.c @@ -0,0 +1,315 @@ +/* +Copyright (c) 2009-2016 Roger Light + +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License v1.0 +and Eclipse Distribution License v1.0 which accompany this distribution. + +The Eclipse Public License is available at + http://www.eclipse.org/legal/epl-v10.html +and the Eclipse Distribution License is available at + http://www.eclipse.org/org/documents/edl-v10.php. + +Contributors: + Roger Light - initial implementation and documentation. +*/ + +#include +#include +#include +#include +#include +#include +#ifndef WIN32 +#include +#else +#include +#include +#define snprintf sprintf_s +#endif + +#include +#include "client_shared.h" + + +static int get_time(struct tm **ti, long *ns) +{ +#ifdef WIN32 + SYSTEMTIME st; +#else + struct timespec ts; +#endif + time_t s; + +#ifdef WIN32 + s = time(NULL); + + GetLocalTime(&st); + *ns = st.wMilliseconds*1000000L; +#else + if(clock_gettime(CLOCK_REALTIME, &ts) != 0){ + fprintf(stderr, "Error obtaining system time.\n"); + return 1; + } + s = ts.tv_sec; + *ns = ts.tv_nsec; +#endif + + *ti = localtime(&s); + if(!(*ti)){ + fprintf(stderr, "Error obtaining system time.\n"); + return 1; + } + + return 0; +} + + +static void write_payload(const unsigned char *payload, int payloadlen, bool hex) +{ + int i; + + if(!hex){ + (void)fwrite(payload, 1, payloadlen, stdout); + }else{ + for(i=0; i=0 && payload[i] < 32)){ + printf("\\u%04d", payload[i]); + }else{ + fputc(payload[i], stdout); + } + } +} + + +static void json_print(const struct mosquitto_message *message, const struct tm *ti, bool escaped) +{ + char buf[100]; + + strftime(buf, 100, "%s", ti); + printf("{\"tst\":%s,\"topic\":\"%s\",\"qos\":%d,\"retain\":%d,\"payloadlen\":%d,", buf, message->topic, message->qos, message->retain, message->payloadlen); + if(message->qos > 0){ + printf("\"mid\":%d,", message->mid); + } + if(escaped){ + fputs("\"payload\":\"", stdout); + write_json_payload(message->payload, message->payloadlen); + fputs("\"}", stdout); + }else{ + fputs("\"payload\":", stdout); + write_payload(message->payload, message->payloadlen, false); + fputs("}", stdout); + } +} + + +static void formatted_print(const struct mosq_config *cfg, const struct mosquitto_message *message) +{ + int len; + int i; + struct tm *ti = NULL; + long ns; + char strf[3]; + char buf[100]; + + len = strlen(cfg->format); + + for(i=0; iformat[i] == '%'){ + if(i < len-1){ + i++; + switch(cfg->format[i]){ + case '%': + fputc('%', stdout); + break; + + case 'I': + if(!ti){ + if(get_time(&ti, &ns)){ + fprintf(stderr, "Error obtaining system time.\n"); + return; + } + } + if(strftime(buf, 100, "%FT%T%z", ti) != 0){ + fputs(buf, stdout); + } + break; + + case 'j': + if(!ti){ + if(get_time(&ti, &ns)){ + fprintf(stderr, "Error obtaining system time.\n"); + return; + } + } + json_print(message, ti, true); + break; + + case 'J': + if(!ti){ + if(get_time(&ti, &ns)){ + fprintf(stderr, "Error obtaining system time.\n"); + return; + } + } + json_print(message, ti, false); + break; + + case 'l': + printf("%d", message->payloadlen); + break; + + case 'm': + printf("%d", message->mid); + break; + + case 'p': + write_payload(message->payload, message->payloadlen, false); + break; + + case 'q': + fputc(message->qos + 48, stdout); + break; + + case 'r': + if(message->retain){ + fputc('1', stdout); + }else{ + fputc('0', stdout); + } + break; + + case 't': + fputs(message->topic, stdout); + break; + + case 'U': + if(!ti){ + if(get_time(&ti, &ns)){ + fprintf(stderr, "Error obtaining system time.\n"); + return; + } + } + if(strftime(buf, 100, "%s", ti) != 0){ + printf("%s.%09ld", buf, ns); + } + break; + + case 'x': + write_payload(message->payload, message->payloadlen, true); + break; + } + } + }else if(cfg->format[i] == '@'){ + if(i < len-1){ + i++; + if(cfg->format[i] == '@'){ + fputc('@', stdout); + }else{ + if(!ti){ + if(get_time(&ti, &ns)){ + fprintf(stderr, "Error obtaining system time.\n"); + return; + } + } + + strf[0] = '%'; + strf[1] = cfg->format[i]; + strf[2] = 0; + + if(cfg->format[i] == 'N'){ + printf("%09ld", ns); + }else{ + if(strftime(buf, 100, strf, ti) != 0){ + fputs(buf, stdout); + } + } + } + } + }else if(cfg->format[i] == '\\'){ + if(i < len-1){ + i++; + switch(cfg->format[i]){ + case '\\': + fputc('\\', stdout); + break; + + case '0': + fputc('\0', stdout); + break; + + case 'a': + fputc('\a', stdout); + break; + + case 'e': + fputc('\033', stdout); + break; + + case 'n': + fputc('\n', stdout); + break; + + case 'r': + fputc('\r', stdout); + break; + + case 't': + fputc('\t', stdout); + break; + + case 'v': + fputc('\v', stdout); + break; + } + } + }else{ + fputc(cfg->format[i], stdout); + } + } + if(cfg->eol){ + fputc('\n', stdout); + } + fflush(stdout); +} + + +void print_message(struct mosq_config *cfg, const struct mosquitto_message *message) +{ + if(cfg->format){ + formatted_print(cfg, message); + }else if(cfg->verbose){ + if(message->payloadlen){ + printf("%s ", message->topic); + write_payload(message->payload, message->payloadlen, false); + if(cfg->eol){ + printf("\n"); + } + }else{ + if(cfg->eol){ + printf("%s (null)\n", message->topic); + } + } + fflush(stdout); + }else{ + if(message->payloadlen){ + write_payload(message->payload, message->payloadlen, false); + if(cfg->eol){ + printf("\n"); + } + fflush(stdout); + } + } +} +