int MqttHal_Uninit() { mosquitto_destroy(g_mosq); mosquitto_lib_cleanup(); return 0; }
int main(int argc, char *argv[]) { int i; char *host = "localhost"; int port = 1883; int keepalive = 60; bool clean_session = false; // Note this, set it to false if you want to get offline message struct mosquitto *mosq = NULL; mosquitto_lib_init(); // Note this, this first argv is id, it must be set if clean_session is false mosq = mosquitto_new("1", clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } while(!mosquitto_loop(mosq, -1, 1)){ } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
int main(int argc, char *argv[]) { int i; char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; sprintf(mqMsg,"%s",argv[1]); printf("And the word is >> %s <<\n", mqMsg); mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
/* Loop until it is explicitly halted or the network is lost, then clean up. */ static int run_loop(struct client_info *info) { int res = MOSQ_ERR_SUCCESS; int i = 0; char buf[1024]; memset(buf, 0, sizeof(buf)); do { res = mosquitto_loop(info->m, -1, 1 /* unused */); if (res != MOSQ_ERR_SUCCESS) break; sleep(5); memset(buf, 0, sizeof(buf)); sprintf(buf, "test_msg#%d\n", i++); res = mosquitto_publish(info->m, &mid_sent, PUBLISH_TOPIC, strlen(buf), buf, PUBLISH_MSG_QOS, 0); }while(res == MOSQ_ERR_SUCCESS && connected); mosquitto_destroy(info->m); (void)mosquitto_lib_cleanup(); if (res == MOSQ_ERR_SUCCESS) { return 0; } else { return 1; } }
struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *userdata) { struct mosquitto *mosq = NULL; int rc; if(clean_session == false && id == NULL) { errno = EINVAL; return NULL; } #ifndef WIN32 signal(SIGPIPE, SIG_IGN); #endif mosq = (struct mosquitto *)_mosquitto_calloc(1, sizeof(struct mosquitto)); if(mosq) { mosq->sock = INVALID_SOCKET; #ifdef WITH_THREADING mosq->thread_id = pthread_self(); #endif rc = mosquitto_reinitialise(mosq, id, clean_session, userdata); if(rc) { mosquitto_destroy(mosq); if(rc == MOSQ_ERR_INVAL) { errno = EINVAL; } else if(rc == MOSQ_ERR_NOMEM) { errno = ENOMEM; } return NULL; } } else { errno = ENOMEM; } return mosq; }
void mloop(dispatch_data *dd) { char id[30]; char *host = "130.102.128.123"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; local_dd = dd; mosq = mosquitto_new(id, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return; } mosq_instance=mosq; mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); if(mosquitto_connect(mosq, host, port, keepalive, clean_session)){ fprintf(stderr, "Unable to connect.\n"); return; } while(mosquitto_loop(mosq, -1) != -1){ } mosquitto_destroy(mosq); return; }
int mqPub(void) { int i; char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_message_callback_set(mosq, my_message_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
/* You can only call this if you _setup() succeeded all the way!. */ void mosq_cleanup(struct _squash *st) { event_free(st->mosq_write); event_free(st->mosq_readidle); mosquitto_disconnect(st->mosq); mosquitto_destroy(st->mosq); mosquitto_lib_cleanup(); }
QMosquitto::~QMosquitto() { this->close(); if(this->mosq != NULL) mosquitto_destroy(this->mosq); this->mosq = NULL; }
void emit_close() { mosquitto_disconnect(config.mosq); mosquitto_loop_stop(config.mosq, false); mosquitto_destroy(config.mosq); mosquitto_lib_cleanup(); }
int main(int argc, char *argv[]) { struct mosquitto *mosq; int i; double dstart, dstop, diff; FILE *fptr; uint8_t *buf; buf = malloc(MESSAGE_SIZE*MESSAGE_COUNT); if(!buf){ printf("Error: Out of memory.\n"); return 1; } start.tv_sec = 0; start.tv_usec = 0; stop.tv_sec = 0; stop.tv_usec = 0; if(create_data()){ printf("Error: Unable to create random input data.\n"); return 1; } fptr = fopen("msgsps_pub.dat", "rb"); if(!fptr){ printf("Error: Unable to open random input data.\n"); return 1; } fread(buf, sizeof(uint8_t), MESSAGE_SIZE*MESSAGE_COUNT, fptr); fclose(fptr); mosquitto_lib_init(); mosq = mosquitto_new("perftest", true, NULL); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); mosquitto_connect(mosq, "127.0.0.1", 1884, 600); i=0; while(!mosquitto_loop(mosq, 1, 10) && run){ if(i<MESSAGE_COUNT){ mosquitto_publish(mosq, NULL, "perf/test", MESSAGE_SIZE, &buf[i*MESSAGE_SIZE], 0, false); i++; } } dstart = (double)start.tv_sec*1.0e6 + (double)start.tv_usec; dstop = (double)stop.tv_sec*1.0e6 + (double)stop.tv_usec; diff = (dstop-dstart)/1.0e6; printf("Start: %g\nStop: %g\nDiff: %g\nMessages/s: %g\n", dstart, dstop, diff, (double)MESSAGE_COUNT/diff); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
void mqtt_exit(mqtt_t inst) { int rval; mosquitto_destroy(inst->mosq); rval = mosquitto_lib_cleanup(); if (rval != MOSQ_ERR_SUCCESS) { error("Cannot cleanup library: '%s'", mosquitto_strerror(rval)); } }
bool mosq_setup(struct _squash *st) { mosquitto_lib_init(); DLOG("mosquitto -> (re)connecting\n"); st->mosq = mosquitto_new(NULL, true, st); mosquitto_log_callback_set(st->mosq, mosq_logger); mosquitto_message_callback_set(st->mosq, st->msg_handler); int rc = mosquitto_connect(st->mosq, st->mq_host, 1883, 60); if (MOSQ_ERR_SUCCESS != rc) { WLOG("Failed to connect: %s\n", strerror(errno)); rc = -1; goto unwind; } int mosq_fd = mosquitto_socket(st->mosq); if (evutil_make_socket_nonblocking(mosq_fd)) { WLOG("Failed to make non-blocking: fd = %d, possibly ok\n", mosq_fd); } st->mosq_readidle = event_new(st->base, mosq_fd, EV_READ|EV_PERSIST, mosq_ev_io, st); if (st->mosq_readidle == NULL) { WLOG("Failed to create mosquitto read/idle watcher\n"); rc = -1; goto unwind_readidle; } st->mosq_write = event_new(st->base, mosq_fd, EV_WRITE, mosq_ev_io, st); if (st->mosq_write == NULL) { WLOG("Failed to create mosquitto write watcher\n"); rc = -1; goto unwind_write; } if (mosquitto_want_write(st->mosq)) { event_add(st->mosq_write, NULL); } struct timeval mosq_idle_loop_time = { 0, 100 * 1000 }; if (event_add(st->mosq_readidle, &mosq_idle_loop_time) < 0) { WLOG("Failed to activate mosquitto watcher\n"); rc = -1; goto unwind_write; } goto out; unwind_write: event_free(st->mosq_write); unwind_readidle: event_free(st->mosq_readidle); unwind: mosquitto_destroy(st->mosq); mosquitto_lib_cleanup(); out: return rc == 0; }
void MQTT_start_receiver_process() { int rc; do{ rc = mosquitto_loop(MQTT_mosq, -1); }while(rc == MOSQ_ERR_SUCCESS); printf("[MQTT] Error :%i\n", rc); mosquitto_destroy(MQTT_mosq); mosquitto_lib_cleanup(); }
void /*PORT_NAME*/_start_receiver_process() { int rc; do{ rc = mosquitto_loop(/*PORT_NAME*/_mosq, -1); }while(rc == MOSQ_ERR_SUCCESS); /*TRACE_LEVEL_1*/printf("[/*PORT_NAME*/] Error :%i\n", rc); mosquitto_destroy(/*PORT_NAME*/_mosq); mosquitto_lib_cleanup(); }
MosquittoHandler::~MosquittoHandler() { if(m_mosquittoStruct) { mosquitto_destroy(m_mosquittoStruct); m_mosquittoStruct = NULL; } numOfInstances--; if(numOfInstances == 0 && m_libInit) { mosquitto_lib_cleanup(); } //std::cout << "Mosquitto cleaned" << std::endl; }
int main(int argc, char *argv[]) { char clientid[24]="FcBitu'"; int rc = 0; //int nr = 0; struct mosquitto *mosq; int mid; signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); mosquitto_lib_init(); mosq = mosquitto_new(clientid, true, NULL); if(mosq){ mosquitto_connect_callback_set(mosq, connect_callback); mosquitto_message_callback_set(mosq, message_callback); rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60); mosquitto_subscribe(mosq, NULL, "coords", 0); while(run){ rc = mosquitto_loop(mosq, 1, 1); if(run && rc){ sleep(2); printf ("\n run: %d rc: %d \n", run,rc); mosquitto_reconnect(mosq); } printf("robot: timestamp: %d X: %d Y: %d unghi: %d\n ",coordrob[4] .timestamp,coordrob[4] .x,coordrob[4] .y,coordrob[4] .angle); printf("0: timestamp: %d X: %d Y: %d unghi: %d\n ",coordrob[0] .timestamp,coordrob[0] .x,coordrob[0] .y,coordrob[0] .angle); quarterback_oriented(); mosquitto_publish(mosq, &mid, idfundas, sizeof(struct control), &ctr[fundas], 0, false); mosquitto_publish(mosq, &mid, idminge, sizeof(struct control), &ctr[0], 0, false); //mosquitto_publish(mosq, &mid, idportar, sizeof(struct control), &ctr[ceva_portar], 0, false); //usleep(1000); /*if (nr >= 255) nr = 0; ctr.right = nr; ctr.left = nr; nr = nr + 1; printf ("comenzi robot: ctr.right: %d ctr.left: %d timestamp: %d\n", ctr.right,ctr.left,coordrob[0].timestamp); fflush(stdout);*/ } mosquitto_destroy(mosq); } mosquitto_lib_cleanup(); return rc; }
// establish the MQTT connection int mqtt_loop(char *host, int port, char *username, char *password) { struct mosquitto *mosq; int r = 0; mosquitto_lib_init(); if ((mosq = mosquitto_new(NULL, true, NULL)) == NULL) { r = -1; printf("Failed to build the MQTT object.\n" ); goto FUNCTION_END; } // declare the callback functions mosquitto_connect_callback_set(mosq, mqtt_callback_connect); mosquitto_message_callback_set(mosq, mqtt_callback_message); mosquitto_disconnect_callback_set(mosq, mqtt_callback_disconnect); // authentication r = mosquitto_username_pw_set(mosq, username, password); if (r) { r = -2; printf("Failed to setup the MQTT authentication - %d.\n", r); goto FUNCTION_END; } // connect and wait if (mosquitto_connect(mosq, host, port, 60) == MOSQ_ERR_SUCCESS) { r = 0; mosquitto_loop_forever(mosq, -1, 1); } else { r = -3; printf("Failed to connect to MQTT broker.\n"); } mosquitto_destroy(mosq); FUNCTION_END: mosquitto_lib_cleanup(); return r; }
static int ctx_destroy(lua_State *L) { ctx_t *ctx = ctx_check(L, 1); mosquitto_destroy(ctx->mosq); /* clean up Lua callback functions in the registry */ ctx__on_clear(ctx); /* remove all methods operating on ctx */ lua_newtable(L); lua_setmetatable(L, -2); return mosq__pstatus(L, MOSQ_ERR_SUCCESS); }
/** * Handles the signal from ctrl+C. Simple close the connection and close the file. */ void sig_handler(int sig) { if( SIGINT != sig ) { CRIT("Got unexpected signal."); } mosquitto_disconnect(config.mosq); mosquitto_destroy(config.mosq); mosquitto_lib_cleanup(); fclose(config.fd); exit(0); }
static void mqtt_free(mqtt_client_conf_t *conf) { if (conf == NULL) return; if (conf->connected) (void)mosquitto_disconnect(conf->mosq); conf->connected = 0; (void)mosquitto_destroy(conf->mosq); sfree(conf->host); sfree(conf->username); sfree(conf->password); sfree(conf->client_id); sfree(conf->topic_prefix); sfree(conf); }
int main(int argc, char *argv[]) { char *host = "5.44.237.19"; int port = 1883; int keepalive = 60; bool clean_session = true; struct mosquitto *mosq = NULL; /* argv[1] is the filename, argv[2] is the number of MAC addresses */ /* and argv[3] is the timestamp in secs since the epoch */ if(argc==4) { macFile=argv[1]; messagesToSend=atoi(argv[2]); if(messagesToSend==0) { return 1; } timeNow=argv[3]; } else { printf("USAGE: thingithonmq macFileName numRecords timeStamp\n Where timeStamp is in seconds, like date +\%s\n"); return 1; } /* Start up Mosquitto */ mosquitto_lib_init(); mosq = mosquitto_new(NULL, clean_session, NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } mosquitto_log_callback_set(mosq, my_log_callback); mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); if(mosquitto_connect(mosq, host, port, keepalive)){ fprintf(stderr, "Unable to connect.\n"); return 1; } mosquitto_loop_forever(mosq, -1, 1); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return 0; }
void ZnpActorProcess(PACTOROPTION option) { mosquitto_lib_init(); ZnpActorCreat(option->guid, option->psw, option->host, option->port); if (pZnpActor == NULL) { mosquitto_lib_cleanup(); return; } while(1) { ActorProcessEvent(pZnpActor); mosquitto_loop(pZnpActor->client, 0, 1); usleep(10000); } mosquitto_disconnect(pZnpActor->client); mosquitto_destroy(pZnpActor->client); mosquitto_lib_cleanup(); }
static void LedActorStart(PACTOROPTION option) { mosquitto_lib_init(); LedActorCreate(option->guid, option->psw, option->host, option->port); if (pLedActor == NULL) { mosquitto_lib_cleanup(); return; } while(1) { ActorProcessEvent(pLedActor); LedTransition(); mosquitto_loop(pLedActor->client, 0, 1); usleep(10000); } mosquitto_disconnect(pLedActor->client); mosquitto_destroy(pLedActor->client); mosquitto_lib_cleanup(); }
int main(int argc, char *argv[]) { char clientid[24]="Move Test"; struct mosquitto *mosq; int rc = 0; int mid; signal(SIGINT, handle_signal); signal(SIGTERM, handle_signal); mosquitto_lib_init(); mosq = mosquitto_new(clientid, true, NULL); if(mosq) { mosquitto_connect_callback_set(mosq, connect_callback); mosquitto_message_callback_set(mosq, message_callback); rc = mosquitto_connect(mosq, mqtt_host, mqtt_port, 60); mosquitto_subscribe(mosq, NULL, "#", 0); ctr.left=100; ctr.right=100; ctr.time=0; mosquitto_publish(mosq, &mid, "in15", sizeof(ctr), &ctr, 2, false); while(run) { rc = mosquitto_loop(mosq, -1, 1); if(run && rc) { sleep(20); mosquitto_reconnect(mosq); } } mosquitto_destroy(mosq); } mosquitto_lib_cleanup(); return rc; }
int main(int argc, char *argv[]) { struct mosquitto *mosq; int fd; bool clean_session = true; int keepalive = 60; mosq = mosquitto_new("packetgen", NULL); if(!mosq){ fprintf(stderr, "Error: Out of memory.\n"); return 1; } /* CONNECT */ fd = open("mqtt.connect", O_CREAT|O_WRONLY, 00644); if(fd<0){ fprintf(stderr, "Error: Unable to open mqtt.connect for writing.\n"); return 1; } mosq->core.sock = fd; printf("_mosquitto_send_connect(): %d\n", _mosquitto_send_connect(mosq, keepalive, clean_session)); printf("loop: %d\n", mosquitto_loop_write(mosq)); close(fd); /* SUBSCRIBE */ fd = open("mqtt.subscribe", O_CREAT|O_WRONLY, 00644); if(fd<0){ fprintf(stderr, "Error: Unable to open mqtt.subscribe for writing.\n"); return 1; } mosq->core.sock = fd; printf("_mosquitto_send_subscribe(): %d\n", _mosquitto_send_subscribe(mosq, NULL, false, "subscribe/topic", 2)); printf("loop: %d\n", mosquitto_loop_write(mosq)); close(fd); mosquitto_destroy(mosq); return 0; }
mosquittopp::~mosquittopp() { mosquitto_destroy(m_mosq); }
int main(int argc, char *argv[]) { int opt; int rc = 0; struct mosquitto *mosq = NULL; luzz_mqtt_t mqtt = { .host = "localhost", .port = 1883, .keepalive = 60, .id = NULL, .clean_session = true, .timeout = 0, .max_packets = 1, .mid = NULL, .topic = NULL, .qos = 0, .retain = 0, }; luzz_ctx_t ctx = { .mqttp = &mqtt, .panel = 0, .num_leds = 4, .fps = 2, .framep = NULL, .rt_prio = -1, .memlock = -1, }; while ((opt = getopt(argc, argv, "h:p:i:n:r:P:M")) != -1) { switch (opt) { case 'h': mqtt.host = optarg; break; case 'p': mqtt.port = atoi(optarg); break; case 'i': ctx.panel = atoi(optarg); break; case 'n': ctx.num_leds = atoi(optarg); break; case 'r': ctx.fps = atoi(optarg); break; case 'P': ctx.rt_prio = atoi(optarg); break; case 'M': ctx.memlock = 1; break; default: luzz_gen_usage(&ctx); goto finish; } } struct sched_param sched = { .sched_priority = ctx.rt_prio }; if (ctx.rt_prio > -1 && sched_setscheduler(0, SCHED_FIFO, &sched) == -1) { perror("rt_prio"); goto finish; } if (ctx.memlock > -1 && mlockall(MCL_CURRENT | MCL_FUTURE) == -1) { perror("memlock"); goto finish; } int i = 0; bool red = true; struct timespec ts_remain; struct timespec ts_request = { .tv_sec = 0, .tv_nsec = 1e9 / ctx.fps }; ctx.framep = calloc(ctx.num_leds, sizeof(luzz_grb_t)); if (ctx.framep == NULL) { goto oom; } mosquitto_lib_init(); if (uname(&ctx.uts) < 0) { perror("uname"); goto finish; } if ((rc = asprintf(&mqtt.id, LUZZ_GEN_ID_TPL, LUZZ_VERSION, ctx.uts.nodename, getpid())) < 0) { goto oom; } mosq = mosquitto_new(mqtt.id, mqtt.clean_session, &ctx); if (!mosq) { switch (errno) { case ENOMEM: rc = 1; goto oom; case EINVAL: fprintf(stderr, "mosq_new: Invalid id and/or clean_session.\n"); rc = 1; goto finish; } } mosquitto_connect(mosq, mqtt.host, mqtt.port, mqtt.keepalive); if ((rc = asprintf(&mqtt.topic, LUZZ_TOPIC_TPL, ctx.panel)) < 0) { goto oom; } while (true) { ((luzz_rgb_t *)ctx.framep + i++)->r = red ? 0xFF : 0x00; if (i == ctx.num_leds - 1) { i = 0; red = !red; } mosquitto_publish(mosq, mqtt.mid, mqtt.topic, sizeof(luzz_rgb_t) * ctx.num_leds, (const void *)ctx.framep, mqtt.qos, mqtt.retain); while ((rc = mosquitto_loop(mosq, mqtt.timeout, mqtt.max_packets)) != MOSQ_ERR_SUCCESS) { switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_loop: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_NOMEM: goto oom; case MOSQ_ERR_PROTOCOL: fprintf(stderr, "mosq_loop: MQTT Protocol error.\n"); goto finish; case MOSQ_ERR_ERRNO: perror("mosq_loop"); goto finish; case MOSQ_ERR_NO_CONN: fprintf(stderr, "mosq_loop: No broker connection.\n"); break; case MOSQ_ERR_CONN_LOST: fprintf(stderr, "mosq_loop: Connection to broker was lost.\n"); break; } sleep(1); mosquitto_reconnect(mosq); } nanosleep(&ts_request, &ts_remain); } oom: fprintf(stderr, "error: Out of memory.\n"); finish: mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return rc; }
int main(int, char**) { SDL_Window *window = 0; SDL_Renderer *renderer = 0; int code = 0; struct mosquitto* mosq; bool run = true; unsigned int t0 = 0, t1 = 0; mosquitto_lib_init(); if((mosq = mosquitto_new(0, true, 0)) == 0) { std::cout << "Failed to initialize mosquitto." << std::endl; code = 1; goto end; } mosquitto_connect_callback_set(mosq, connect_callback); mosquitto_message_callback_set(mosq, message_callback); //Init SDL if (SDL_Init(SDL_INIT_VIDEO) != 0){ std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl; code = 1; goto end; } //Create a window and renderer attached to it. window = SDL_CreateWindow("SDL Skeleton", 100, 100, 800, 600, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_PRESENTVSYNC); if(!window || !renderer) { std::cout << "SDL_CreateWindow or SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; code = 1; goto end; } SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); //Attempt mosquitto connection to local host. mosquitto_connect_async(mosq, "amee.interaktionsbyran.se", 1883, 60); //Start the mosquitto network thread. if(mosquitto_loop_start(mosq) != MOSQ_ERR_SUCCESS) { std::cout << "Failed mosquitto init " << mosq << std::endl; code = 1; goto end; } //Block untill the user closes the window SDL_Event e; while (run) { t1 = SDL_GetTicks(); float delta_time = (float)(t1 - t0)/1000.f; while( SDL_PollEvent( &e ) != 0) { if( e.type == SDL_QUIT ) { run = false; break; } } //Clear buffer. SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff); SDL_RenderClear(renderer); draw(renderer, delta_time); SDL_RenderPresent(renderer); t0 = t1; } end: SDL_Quit(); //Cleanup mqtt mosquitto_loop_stop(mosq, true); mosquitto_destroy(mosq); mosquitto_lib_cleanup(); return code; }
/** * Main! */ int main(int argc, char **argv) { struct sigaction sigact; char buf[BUF_SIZE]; int qos, retain, len; struct timeval recv_time, now; char topic[CONF_MAX_LENGTH_MQTT_TOPIC]; int i, ret; struct mqtt_player_status_msg status; if( config_init() ) { CRIT("Faild to initialize config."); } parse_args(argc, argv); if( !strlen(config.log_file) ) { fprintf(stderr, "ERROR: You have to provide a logfile.\n"); print_usage(*argv); exit(1); } config.fd = fopen(config.log_file, "r"); if( NULL == config.fd ) { CRIT("Could not open log file."); } memset(&sigact, 0, sizeof(struct sigaction)); sigact.sa_handler = sig_handler; if( sigaction(SIGINT, &sigact, NULL) ) { CRIT("Could not initialize signal handler."); } mosquitto_lib_init(); config.mosq = mosquitto_new(config.mqtt_client_id, config.mqtt_clean_session, NULL); if( NULL == config.mosq ) { CRIT("Could not create a mosquitto object."); } if( mosquitto_connect(config.mosq, config.mqtt_broker, config.mqtt_port, config.mqtt_keepalive) ) { CRIT("Could not connect MQTT broker."); } mosquitto_loop_start(config.mosq); do { if( gettimeofday(&config.start, NULL) ) { CRIT("Could not get time()."); } if( 0 != fseek(config.fd, SEEK_SET, 0) ) { CRIT("lseek faild."); } // read file config while(1) { if( fscanf(config.fd, "cnf time: %ld.%ld\n", &config.record_start_time.tv_sec, &config.record_start_time.tv_usec) ) { if( config.verbose ) { printf("record time: %3ld", config.record_start_time.tv_sec); printf(".%06ld\n", config.record_start_time.tv_usec); } } else { break; } } if( config.verbose ) { printf("-- start playing --\n"); } status.status = MQTT_PLAYER_BEGIN_PLAY; status.sec = hton64(config.record_start_time.tv_sec); status.usec = hton64(config.record_start_time.tv_usec); // post status mosquitto_publish(config.mosq, NULL, config.mqtt_topic, sizeof(struct mqtt_player_status_msg), &status, 2, 0); // read data while(1) { if( feof(config.fd) ) { break; } ret = fscanf(config.fd, "msg %ld.%ld %d %d %d %s\n", &recv_time.tv_sec, &recv_time.tv_usec, &qos, &retain, &len, topic); if( 0 == ret ) { break; } if( config.verbose ) { printf("time: %3ld", recv_time.tv_sec); printf(".%06ld ", recv_time.tv_usec); printf("qos: %d ", qos); printf("retain: %d ", retain); printf("len: %d ", len); printf("topic: %s\n", topic); } if( 0 > qos && 2 < qos ) { CRIT("Format error in '%s'.", config.log_file); } if( 0 > retain && 1 < retain ) { CRIT("Format error in '%s'.", config.log_file); } if( 0 < len ) { ret == fscanf(config.fd, "%02hhx", &buf[0]); if( 0 == ret ) { break; } } for( i = 1; i < len; i++ ) { ret == fscanf(config.fd, " %02hhx", &buf[i]); if( 0 == ret ) { break; } } ret == fscanf(config.fd, "\n"); if( 0 == ret ) { break; } if( !config.ignore_timing ) { timeradd(&recv_time, &config.start, &recv_time); if( gettimeofday(&now, NULL) ) { CRIT("Could not get time."); } if( timercmp(&now, &recv_time, <) ) { timersub(&recv_time, &now, &recv_time); usleep(recv_time.tv_sec * 1000000 + recv_time.tv_usec); } } mosquitto_publish(config.mosq, NULL, topic, len, buf, qos, retain); } }while( config.repeat && feof(config.fd) ); mosquitto_disconnect(config.mosq); mosquitto_loop_stop(config.mosq, false); mosquitto_destroy(config.mosq); mosquitto_lib_cleanup(); fclose(config.fd); return 0; }