/* Copyright (c) 2009-2018 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 "config.h" #include #include #include #include #include #include #ifndef WIN32 #include #include #else #include #include #define snprintf sprintf_s #endif #include #include "client_shared.h" static struct mosq_config cfg; bool process_messages = true; int msg_count = 0; struct mosquitto *mosq = NULL; #ifndef WIN32 void my_signal_handler(int signum) { if(signum == SIGALRM){ process_messages = false; mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props); } } #endif 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) { int i; bool res; if(process_messages == false) return; if(cfg.retained_only && !message->retain && process_messages){ process_messages = false; mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props); return; } if(message->retain && cfg.no_retain) return; if(cfg.filter_outs){ for(i=0; itopic, &res); if(res) return; } } print_message(&cfg, message); if(cfg.msg_count>0){ msg_count++; if(cfg.msg_count == msg_count){ process_messages = false; mosquitto_disconnect_with_properties(mosq, cfg.disconnect_props); } } } void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags) { int i; if(!result){ mosquitto_subscribe_multiple(mosq, NULL, cfg.topic_count, cfg.topics, cfg.qos, cfg.subscribe_props); for(i=0; i0 && rc == MOSQ_ERR_NO_CONN){ rc = 0; } if(rc){ fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); } return rc; }