int main(int argc, char *argv[]) { int rc; int tmp; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("prop-test", true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_publish_callback_set(mosq, on_publish); tmp = MQTT_PROTOCOL_V5; mosquitto_opts_set(mosq, MOSQ_OPT_PROTOCOL_VERSION, &tmp); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ rc = mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
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 main(int argc, char *argv[]) { int port, client_id, buffer_size; int rc; struct mosquitto *mosq; void* buffer; char line[1024]; const char *name; //prepare arguments if(argc < 3) { printf("usage: %s host port [id]\n", argv[0]); return 0; } port = atoi(argv[2]); client_id = -1; if(argc > 3) { client_id = atoi(argv[3]); } pthread_mutex_init(&mutex, NULL); name = init(client_id); //start mosquitto stuff mosquitto_lib_init(); mosq = mosquitto_new(name, true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_subscribe_callback_set(mosq, on_subscribe); mosquitto_message_callback_set(mosq, on_message); rc = mosquitto_connect_async(mosq, argv[1], port, 60); mosquitto_loop_start(mosq); while(1) { fgets(line, 1024, stdin); pthread_mutex_lock(&mutex); buffer = message_entered(line, &buffer_size); pthread_mutex_unlock(&mutex); int sent_mid = -1; mosquitto_publish(mosq, &sent_mid, topic_name, buffer_size, buffer, 0, false); message_sent(buffer, buffer_size); } mosquitto_disconnect(mosq); mosquitto_loop_stop(mosq, false); mosquitto_lib_cleanup(); 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; }
bool MosquittoHandler::init(std::string id) { int minor, major, revision; mosquitto_lib_version(&major, &minor, &revision); if(numOfInstances == 0) { mosquitto_lib_init(); } m_libInit = true; numOfInstances++; void* obj = (void*) this; m_mosquittoStruct = mosquitto_new(id.c_str(), obj); if(!m_mosquittoStruct) { m_lastErrorString = "Cannot create new Mosquitto instance"; return false; } mosquitto_log_init(m_mosquittoStruct, MOSQ_LOG_ERR || MOSQ_LOG_WARNING, MOSQ_LOG_STDOUT); mosquitto_connect_callback_set(m_mosquittoStruct, MosquittoHandler::onConnectWrapper); mosquitto_disconnect_callback_set(m_mosquittoStruct, MosquittoHandler::onDisconnectWrapper); mosquitto_message_callback_set(m_mosquittoStruct, MosquittoHandler::onMessageWrapper); return true; }
/* {{{ Mosquitto\Client::onConnect() */ PHP_METHOD(Mosquitto_Client, onConnect) { mosquitto_client_object *object; zend_fcall_info connect_callback = empty_fcall_info; zend_fcall_info_cache connect_callback_cache = empty_fcall_info_cache; PHP_MOSQUITTO_ERROR_HANDLING(); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f!", &connect_callback, &connect_callback_cache) == FAILURE) { PHP_MOSQUITTO_RESTORE_ERRORS(); return; } PHP_MOSQUITTO_RESTORE_ERRORS(); object = (mosquitto_client_object *) mosquitto_client_object_get(getThis() TSRMLS_CC); if (!ZEND_FCI_INITIALIZED(connect_callback)) { zend_throw_exception(mosquitto_ce_exception, "Need a valid callback", 0 TSRMLS_CC); } object->connect_callback = connect_callback; object->connect_callback_cache = connect_callback_cache; Z_ADDREF_P(connect_callback.function_name); if (connect_callback.object_ptr != NULL) { Z_ADDREF_P(connect_callback.object_ptr); } mosquitto_connect_callback_set(object->client, php_mosquitto_connect_callback); }
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 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; }
/* Register the callbacks that the mosquitto connection will use. */ static bool set_callbacks(struct mosquitto *m) { mosquitto_connect_callback_set(m, on_connect); mosquitto_publish_callback_set(m, on_publish); mosquitto_subscribe_callback_set(m, on_subscribe); mosquitto_message_callback_set(m, on_message); return true; }
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; }
mosquittopp::mosquittopp(const char *id) { mosq = mosquitto_new(id, this); mosquitto_connect_callback_set(mosq, on_connect_wrapper); mosquitto_disconnect_callback_set(mosq, on_disconnect_wrapper); mosquitto_publish_callback_set(mosq, on_publish_wrapper); mosquitto_message_callback_set(mosq, on_message_wrapper); mosquitto_subscribe_callback_set(mosq, on_subscribe_wrapper); mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe_wrapper); }
mosquittopp::mosquittopp(const char *id, bool clean_session) { m_mosq = mosquitto_new(id, clean_session, this); mosquitto_connect_callback_set(m_mosq, on_connect_wrapper); mosquitto_disconnect_callback_set(m_mosq, on_disconnect_wrapper); mosquitto_publish_callback_set(m_mosq, on_publish_wrapper); mosquitto_message_callback_set(m_mosq, on_message_wrapper); mosquitto_subscribe_callback_set(m_mosq, on_subscribe_wrapper); mosquitto_unsubscribe_callback_set(m_mosq, on_unsubscribe_wrapper); mosquitto_log_callback_set(m_mosq, on_log_wrapper); }
int MqttHal_Init() { int rc = 0; //struct mosq_config mosq_cfg; senhub_info_t *pshinfo; g_SensorHubList = NULL; //ADV_INFO("%s: \n", __func__); rc = mqtt_client_config_load(&g_mosq_cfg, CLIENT_SUB, 1, NULL); if(rc){ mqtt_client_config_cleanup(&g_mosq_cfg); return 1; } mosquitto_lib_init(); if(mqtt_client_id_generate(&g_mosq_cfg, "advmqttcli")){ return 1; } g_mosq = mosquitto_new(g_mosq_cfg.id, g_mosq_cfg.clean_session, &g_mosq_cfg); if(!g_mosq){ switch(errno){ case ENOMEM: if(!g_mosq_cfg.quiet) fprintf(stderr, "Error: Out of memory.\n"); break; case EINVAL: if(!g_mosq_cfg.quiet) fprintf(stderr, "Error: Invalid id and/or clean_session.\n"); break; } mosquitto_lib_cleanup(); return 1; } if(mqtt_client_opts_set(g_mosq, &g_mosq_cfg)){ return 1; } mosquitto_connect_callback_set(g_mosq, MqttHal_Connect_Callback); mosquitto_message_callback_set(g_mosq, MqttHal_Message_Callback); mosquitto_disconnect_callback_set(g_mosq, MqttHal_Disconnect_Callback); mosquitto_publish_callback_set(g_mosq, MqttHal_Publish_Callback); // Create senhub root pshinfo = malloc(sizeof(senhub_info_t)); memset(pshinfo, 0, sizeof(senhub_info_t)); sprintf(pshinfo->macAddress, "0000%s" , g_GWInfMAC); pshinfo->jsonNode = NULL; pshinfo->id = senhub_list_newId(g_SensorHubList); //printf("%s: list add id=%d\n", __func__, pshinfo->id); g_SensorHubList = SENHUB_LIST_ADD(g_SensorHubList, pshinfo); return rc; }
static int mosq_callback_set(lua_State *L) { mosq_t *ctx = mosq_get(L, 1); enum callback_types callback_type = luaL_checkinteger(L, 2); if (!lua_isfunction(L, 3)) { return luaL_argerror(L, 3, "expecting a function"); } int ref = luaL_ref(L, LUA_REGISTRYINDEX); switch (callback_type) { case CONNECT: ctx->connect_ref = ref; mosquitto_connect_callback_set(ctx->mosq, mosq_connect_f); break; case DISCONNECT: ctx->disconnect_ref = ref; mosquitto_disconnect_callback_set(ctx->mosq, mosq_disconnect_f); break; case PUBLISH: ctx->publish_ref = ref; mosquitto_publish_callback_set(ctx->mosq, mosq_publish_f); break; case MESSAGE: ctx->message_ref = ref; mosquitto_message_callback_set(ctx->mosq, mosq_message_f); break; case SUBSCRIBE: ctx->subscribe_ref = ref; mosquitto_subscribe_callback_set(ctx->mosq, mosq_subscribe_f); break; case UNSUBSCRIBE: ctx->unsubscribe_ref = ref; mosquitto_unsubscribe_callback_set(ctx->mosq, mosq_unsubscribe_f); break; default: luaL_unref(L, LUA_REGISTRYINDEX, ref); luaL_argerror(L, 2, "unknown mosquitto callback type"); break; } return make_mosq_status_result(L, MOSQ_ERR_SUCCESS); }
bool connect() { struct mosquitto *m = mosquitto_new(options.clientId, true, this); this->connection = m; mosquitto_connect_callback_set(m, on_connect); mosquitto_publish_callback_set(m, on_publish); mosquitto_subscribe_callback_set(m, on_subscribe); mosquitto_message_callback_set(m, on_message); const int res = mosquitto_connect(m, options.brokerHostname, options.brokerPort, options.keepaliveSeconds); return res == MOSQ_ERR_SUCCESS; }
// 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; }
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; }
int auth_mqtt_init(void) { mosquitto_lib_init(); auth_mqtt_mosq = mosquitto_new("auth_client_XXXXXXXXXXXX", true, NULL); if (!auth_mqtt_mosq) { mosquitto_lib_cleanup(); return -1; } mosquitto_username_pw_set(auth_mqtt_mosq, "#qmsw2..5#", "@oawifi15%"); mosquitto_connect_callback_set(auth_mqtt_mosq, connect_callback); mosquitto_message_callback_set(auth_mqtt_mosq, message_callback); mosquitto_connect(auth_mqtt_mosq, "127.0.0.1", 1883, 60); return 0; }
int mosquittopp::reinitialise(const char *id, bool clean_session) { int rc; rc = mosquitto_reinitialise(m_mosq, id, clean_session, this); if(rc == MOSQ_ERR_SUCCESS){ mosquitto_connect_callback_set(m_mosq, on_connect_wrapper); mosquitto_disconnect_callback_set(m_mosq, on_disconnect_wrapper); mosquitto_publish_callback_set(m_mosq, on_publish_wrapper); mosquitto_message_callback_set(m_mosq, on_message_wrapper); mosquitto_subscribe_callback_set(m_mosq, on_subscribe_wrapper); mosquitto_unsubscribe_callback_set(m_mosq, on_unsubscribe_wrapper); mosquitto_log_callback_set(m_mosq, on_log_wrapper); } return rc; }
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; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("publish-qos0-test", true, NULL); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_publish_callback_set(mosq, on_publish); rc = mosquitto_connect(mosq, "localhost", 1888, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
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[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("08-ssl-connect-crt-auth-enc", true, NULL); mosquitto_tls_opts_set(mosq, 1, "tlsv1", NULL); mosquitto_tls_set(mosq, "../ssl/test-root-ca.crt", "../ssl/certs", "../ssl/client-encrypted.crt", "../ssl/client-encrypted.key", password_callback); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); rc = mosquitto_connect(mosq, "localhost", 1888, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int mqtt_open(mqtt_t inst, dispatch_t d, const char* name, const char* host, int port, int keepalive) { int rval = -1; int fd; inst->mosq = mosquitto_new(name, true, inst); if (inst->mosq == NULL) { error("Cannot create mqtt"); } else { inst->dispatch = d; mosquitto_log_callback_set(inst->mosq, on_log); mosquitto_connect_callback_set(inst->mosq, on_connect); mosquitto_disconnect_callback_set(inst->mosq, on_disconnect); mosquitto_publish_callback_set(inst->mosq, on_publish); mosquitto_message_callback_set(inst->mosq, on_message); mosquitto_subscribe_callback_set(inst->mosq, on_subscribe); mosquitto_unsubscribe_callback_set(inst->mosq, on_unsubscribe); inst->timer = dispatch_create_timer(d, 2000000, mqtt_misc, inst); if (inst->timer == NULL) { error("Cannot create timer"); } rval = mosquitto_connect(inst->mosq, host, port, keepalive); if (rval != MOSQ_ERR_SUCCESS) { error("Cannot connect mqtt"); } else { fd = mosquitto_socket(inst->mosq); rval = dispatch_register(d, fd, mqtt_read, mqtt_write, NULL, mqtt_close, inst); if (rval != 0) { error("Cannot register mqtt"); } } } return rval; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("unsubscribe-test", true, NULL); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_unsubscribe_callback_set(mosq, on_unsubscribe); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; mosquitto_lib_init(); mosq = mosquitto_new("08-tls-psk-pub", true, NULL); rc = mosquitto_tls_psk_set(mosq, "deadbeef", "psk-id", NULL); if(rc) return rc; mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_publish_callback_set(mosq, on_publish); rc = mosquitto_connect(mosq, "localhost", 1888, 60); if(rc) return rc; while(run == -1){ mosquitto_loop(mosq, -1, 1); } mosquitto_lib_cleanup(); return run; }
int main(int argc, char *argv[]) { int rc; struct mosquitto *mosq; int port = atoi(argv[1]); mosquitto_lib_init(); mosq = mosquitto_new("publish-qos2-test", true, &run); mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); mosquitto_connect_callback_set(mosq, on_connect); mosquitto_disconnect_callback_set(mosq, on_disconnect); mosquitto_message_callback_set(mosq, on_message); rc = mosquitto_connect(mosq, "localhost", port, 60); while(run == -1){ mosquitto_loop(mosq, 100, 1); } mosquitto_lib_cleanup(); return run; }
static int ctx_callback_set(lua_State *L) { ctx_t *ctx = ctx_check(L, 1); int callback_type; if (lua_isstring(L, 2)) { callback_type = callback_type_from_string(lua_tostring(L, 2)); } else { callback_type = luaL_checkinteger(L, 2); } if (!lua_isfunction(L, 3)) { return luaL_argerror(L, 3, "expecting a callback function"); } /* pop the function from the stack and store it in the registry */ int ref = luaL_ref(L, LUA_REGISTRYINDEX); switch (callback_type) { case CONNECT: ctx->on_connect = ref; mosquitto_connect_callback_set(ctx->mosq, ctx_on_connect); break; case DISCONNECT: ctx->on_disconnect = ref; mosquitto_disconnect_callback_set(ctx->mosq, ctx_on_disconnect); break; case PUBLISH: ctx->on_publish = ref; mosquitto_publish_callback_set(ctx->mosq, ctx_on_publish); break; case MESSAGE: /* store the reference into the context, to be retrieved by ctx_on_message */ ctx->on_message = ref; /* register C callback in mosquitto */ mosquitto_message_callback_set(ctx->mosq, ctx_on_message); break; case SUBSCRIBE: ctx->on_subscribe = ref; mosquitto_subscribe_callback_set(ctx->mosq, ctx_on_subscribe); break; case UNSUBSCRIBE: ctx->on_unsubscribe = ref; mosquitto_unsubscribe_callback_set(ctx->mosq, ctx_on_unsubscribe); break; case LOG: ctx->on_log = ref; mosquitto_log_callback_set(ctx->mosq, ctx_on_log); break; default: luaL_unref(L, LUA_REGISTRYINDEX, ref); luaL_argerror(L, 2, "not a proper callback type"); break; } return mosq__pstatus(L, MOSQ_ERR_SUCCESS); }
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; }
int main(int argc, char *argv[]) { char *id = NULL; char *id_prefix = NULL; int i; char *host = "localhost"; int port = 1883; int keepalive = 60; char buf[1024]; bool debug = false; struct mosquitto *mosq = NULL; int rc; int rc2; char hostname[256]; char err[1024]; int len; char *will_payload = NULL; long will_payloadlen = 0; int will_qos = 0; bool will_retain = false; char *will_topic = NULL; char *cafile = NULL; char *capath = NULL; char *certfile = NULL; char *keyfile = NULL; char *psk = NULL; char *psk_identity = NULL; for(i=1; i<argc; i++){ if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ if(i==argc-1){ fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); print_usage(); return 1; }else{ port = atoi(argv[i+1]); if(port<1 || port>65535){ fprintf(stderr, "Error: Invalid port given: %d\n", port); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--cafile")){ if(i==argc-1){ fprintf(stderr, "Error: --cafile argument given but no file specified.\n\n"); print_usage(); return 1; }else{ cafile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--capath")){ if(i==argc-1){ fprintf(stderr, "Error: --capath argument given but no directory specified.\n\n"); print_usage(); return 1; }else{ capath = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--cert")){ if(i==argc-1){ fprintf(stderr, "Error: --cert argument given but no file specified.\n\n"); print_usage(); return 1; }else{ certfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){ debug = true; }else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -f argument given but no file specified.\n\n"); print_usage(); return 1; }else{ if(load_file(argv[i+1])) return 1; } i++; }else if(!strcmp(argv[i], "--help")){ print_usage(); return 0; }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){ if(i==argc-1){ fprintf(stderr, "Error: -h argument given but no host specified.\n\n"); print_usage(); return 1; }else{ host = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){ if(id_prefix){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -i argument given but no id specified.\n\n"); print_usage(); return 1; }else{ id = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){ if(id){ fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n"); print_usage(); return 1; } if(i==argc-1){ fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n"); print_usage(); return 1; }else{ id_prefix = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--key")){ if(i==argc-1){ fprintf(stderr, "Error: --key argument given but no file specified.\n\n"); print_usage(); return 1; }else{ keyfile = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-l") || !strcmp(argv[i], "--stdin-line")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ mode = MSGMODE_STDIN_LINE; } }else if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--message")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else if(i==argc-1){ fprintf(stderr, "Error: -m argument given but no message specified.\n\n"); print_usage(); return 1; }else{ message = argv[i+1]; msglen = strlen(message); mode = MSGMODE_CMD; } i++; }else if(!strcmp(argv[i], "-n") || !strcmp(argv[i], "--null-message")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ mode = MSGMODE_NULL; } }else if(!strcmp(argv[i], "--psk")){ if(i==argc-1){ fprintf(stderr, "Error: --psk argument given but no key specified.\n\n"); print_usage(); return 1; }else{ psk = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--psk-identity")){ if(i==argc-1){ fprintf(stderr, "Error: --psk-identity argument given but no identity specified.\n\n"); print_usage(); return 1; }else{ psk_identity = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){ if(i==argc-1){ fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n"); print_usage(); return 1; }else{ qos = atoi(argv[i+1]); if(qos<0 || qos>2){ fprintf(stderr, "Error: Invalid QoS given: %d\n", qos); print_usage(); return 1; } } i++; }else if(!strcmp(argv[i], "--quiet")){ quiet = true; }else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--retain")){ retain = 1; }else if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--stdin-file")){ if(mode != MSGMODE_NONE){ fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n"); print_usage(); return 1; }else{ if(load_stdin()) return 1; } }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){ if(i==argc-1){ fprintf(stderr, "Error: -t argument given but no topic specified.\n\n"); print_usage(); return 1; }else{ topic = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){ if(i==argc-1){ fprintf(stderr, "Error: -u argument given but no username specified.\n\n"); print_usage(); return 1; }else{ username = argv[i+1]; } i++; }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){ if(i==argc-1){ fprintf(stderr, "Error: -P argument given but no password specified.\n\n"); print_usage(); return 1; }else{ password = argv[i+1]; } i++; }else if(!strcmp(argv[i], "--will-payload")){ if(i==argc-1){ fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n"); print_usage(); return 1; }else{ will_payload = argv[i+1]; will_payloadlen = strlen(will_payload); } i++; }else if(!strcmp(argv[i], "--will-qos")){ if(i==argc-1){ fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n"); print_usage(); return 1; }else{ will_qos = atoi(argv[i+1]); if(will_qos < 0 || will_qos > 2){ fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos); return 1; } } i++; }else if(!strcmp(argv[i], "--will-retain")){ will_retain = true; }else if(!strcmp(argv[i], "--will-topic")){ if(i==argc-1){ fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n"); print_usage(); return 1; }else{ will_topic = argv[i+1]; } i++; }else{ fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]); print_usage(); return 1; } } if(!topic || mode == MSGMODE_NONE){ fprintf(stderr, "Error: Both topic and message must be supplied.\n"); print_usage(); return 1; } if(will_payload && !will_topic){ fprintf(stderr, "Error: Will payload given, but no will topic given.\n"); print_usage(); return 1; } if(will_retain && !will_topic){ fprintf(stderr, "Error: Will retain given, but no will topic given.\n"); print_usage(); return 1; } if(password && !username){ if(!quiet) fprintf(stderr, "Warning: Not using password since username not set.\n"); } if((certfile && !keyfile) || (keyfile && !certfile)){ fprintf(stderr, "Error: Both certfile and keyfile must be provided if one of them is.\n"); print_usage(); return 1; } if((cafile || capath) && psk){ if(!quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n"); return 1; } if(psk && !psk_identity){ if(!quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n"); return 1; } mosquitto_lib_init(); if(id_prefix){ id = malloc(strlen(id_prefix)+10); if(!id){ if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid()); }else if(!id){ hostname[0] = '\0'; gethostname(hostname, 256); hostname[255] = '\0'; len = strlen("mosqpub/-") + 6 + strlen(hostname); id = malloc(len); if(!id){ if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); mosquitto_lib_cleanup(); return 1; } snprintf(id, len, "mosqpub/%d-%s", getpid(), hostname); if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH){ /* Enforce maximum client id length of 23 characters */ id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; } } mosq = mosquitto_new(id, true, NULL); if(!mosq){ switch(errno){ case ENOMEM: if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); break; case EINVAL: if(!quiet) fprintf(stderr, "Error: Invalid id.\n"); break; } mosquitto_lib_cleanup(); return 1; } if(debug){ mosquitto_log_callback_set(mosq, my_log_callback); } if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){ if(!quiet) fprintf(stderr, "Error: Problem setting will.\n"); mosquitto_lib_cleanup(); return 1; } if(username && mosquitto_username_pw_set(mosq, username, password)){ if(!quiet) fprintf(stderr, "Error: Problem setting username and password.\n"); mosquitto_lib_cleanup(); return 1; } if((cafile || capath) && mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL)){ if(!quiet) fprintf(stderr, "Error: Problem setting TLS options.\n"); mosquitto_lib_cleanup(); return 1; } if(psk && mosquitto_tls_psk_set(mosq, psk, psk_identity, NULL)){ if(!quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n"); mosquitto_lib_cleanup(); return 1; } mosquitto_connect_callback_set(mosq, my_connect_callback); mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); mosquitto_publish_callback_set(mosq, my_publish_callback); rc = mosquitto_connect(mosq, host, port, keepalive); if(rc){ if(!quiet){ if(rc == MOSQ_ERR_ERRNO){ #ifndef WIN32 strerror_r(errno, err, 1024); #else FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL); #endif fprintf(stderr, "Error: %s\n", err); }else{ fprintf(stderr, "Unable to connect (%d).\n", rc); } } mosquitto_lib_cleanup(); return rc; } if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_start(mosq); } do{ if(mode == MSGMODE_STDIN_LINE){ if(status == STATUS_CONNACK_RECVD){ if(fgets(buf, 1024, stdin)){ buf[strlen(buf)-1] = '\0'; rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, retain); if(rc2){ if(!quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2); mosquitto_disconnect(mosq); } }else if(feof(stdin)){ last_mid = mid_sent; status = STATUS_WAITING; } }else if(status == STATUS_WAITING){ #ifdef WIN32 Sleep(1000); #else usleep(1000000); #endif } rc = MOSQ_ERR_SUCCESS; }else{ rc = mosquitto_loop(mosq, -1, 1); } }while(rc == MOSQ_ERR_SUCCESS && connected); if(mode == MSGMODE_STDIN_LINE){ mosquitto_loop_stop(mosq, false); } if(message && mode == MSGMODE_FILE){ free(message); } mosquitto_destroy(mosq); mosquitto_lib_cleanup(); if(rc){ if(rc == MOSQ_ERR_ERRNO){ fprintf(stderr, "Error: %s\n", strerror(errno)); }else{ fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc)); } } return rc; }