/** * Main broker startup function * @param argc number of elements of argv * @param argv command line strings * @return system return code */ int main(int argc, char* argv[]) { int rc = 0; #define BUILD_TIMESTAMP __DATE__ " " __TIME__ /* __TIMESTAMP__ */ #define BROKER_VERSION "1.2.0.12" /* __VERSION__ */ #define PRODUCT_NAME "MQTT Daemon for Devices" static char* broker_version_eye = NULL; static char* broker_timestamp_eye = NULL; FUNC_ENTRY_NOLOG; broker_timestamp_eye = "AMQTDD_Timestamp " BUILD_TIMESTAMP; broker_version_eye = "AMQTDD_Version " BROKER_VERSION; BrokerState.version = BROKER_VERSION; BrokerState.timestamp = BUILD_TIMESTAMP; srand(time(NULL)); Heap_initialize(); Log_initialize(); Log(LOG_INFO, 9999, PRODUCT_NAME); Log(LOG_INFO, 9998, "Part of Project Mosquitto in Eclipse\n(" "http://projects.eclipse.org/projects/technology.mosquitto)"); getopts(argc, argv); if (Messages_initialize(&BrokerState) != 0) goto no_messages; Log(LOG_INFO, 53, NULL, BrokerState.version, BrokerState.timestamp); Log(LOG_INFO, 54, "%s %s", Messages_get(54, LOG_INFO), "" #if !defined(NO_BRIDGE) " bridge" #endif #if defined(MQTTMP) " MQTTMP" #endif #if defined(MQTTS) " MQTTS" #endif ); if ((rc = Broker_startup()) == 0) { SubscriptionEngines_setRetained(BrokerState.se, "$SYS/broker/version", 0, BrokerState.version, strlen(BrokerState.version)); SubscriptionEngines_setRetained(BrokerState.se, "$SYS/broker/timestamp", 0, BrokerState.timestamp, strlen(BrokerState.timestamp)); while (BrokerState.state == BROKER_RUNNING) { Protocol_timeslice(); #if !defined(NO_BRIDGE) Bridge_timeslice(&BrokerState.bridge); #endif } Log(LOG_INFO, 46, NULL); #if !defined(NO_BRIDGE) Bridge_stop(&BrokerState.bridge); #endif while (BrokerState.state == BROKER_STOPPING) { Protocol_timeslice(); #if !defined(NO_BRIDGE) Bridge_timeslice(&BrokerState.bridge); #endif } } Broker_shutdown(rc); Log(LOG_INFO, 47, NULL); no_messages: Messages_terminate(); Log_terminate(); Heap_terminate(); /*FUNC_EXIT_NOLOG(rc); would anyone ever see this? */ return rc; }
int MQTTClient_create(MQTTClient* handle, char* serverURI, char* clientId, int persistence_type, void* persistence_context) { int rc = 0; MQTTClients *m = NULL; FUNC_ENTRY; rc = Thread_lock_mutex(mqttclient_mutex); if (serverURI == NULL || clientId == NULL) { rc = MQTTCLIENT_NULL_PARAMETER; goto exit; } if (!UTF8_validateString(clientId)) { rc = MQTTCLIENT_BAD_UTF8_STRING; goto exit; } if (!initialized) { #if defined(HEAP_H) Heap_initialize(); #endif Log_initialize(); bstate->clients = ListInitialize(); Socket_outInitialize(); handles = ListInitialize(); initialized = 1; } m = malloc(sizeof(MQTTClients)); *handle = m; memset(m, '\0', sizeof(MQTTClients)); if (strncmp(URI_TCP, serverURI, strlen(URI_TCP)) == 0) serverURI += strlen(URI_TCP); m->serverURI = malloc(strlen(serverURI)+1); strcpy(m->serverURI, serverURI); ListAppend(handles, m, sizeof(MQTTClients)); m->c = malloc(sizeof(Clients)); memset(m->c, '\0', sizeof(Clients)); m->c->outboundMsgs = ListInitialize(); m->c->inboundMsgs = ListInitialize(); m->c->messageQueue = ListInitialize(); m->c->clientID = malloc(strlen(clientId)+1); strcpy(m->c->clientID, clientId); m->connect_sem = Thread_create_sem(); m->connack_sem = Thread_create_sem(); m->suback_sem = Thread_create_sem(); m->unsuback_sem = Thread_create_sem(); #if !defined(NO_PERSISTENCE) rc = MQTTPersistence_create(&(m->c->persistence), persistence_type, persistence_context); if (rc == 0) rc = MQTTPersistence_initialize(m->c, m->serverURI); #endif ListAppend(bstate->clients, m->c, sizeof(Clients) + 3*sizeof(List)); exit: if (Thread_unlock_mutex(mqttclient_mutex) != 0) /* FFDC? */; FUNC_EXIT_RC(rc); return rc; }
int MQTTClient_create(MQTTClient* handle, const char* serverURI, const char* clientId, int persistence_type, void* persistence_context) { int rc = 0; MQTTClients *m = NULL; FUNC_ENTRY; rc = Thread_lock_mutex(mqttclient_mutex); if (serverURI == NULL || clientId == NULL) { rc = MQTTCLIENT_NULL_PARAMETER; goto exit; } if (!UTF8_validateString(clientId)) { rc = MQTTCLIENT_BAD_UTF8_STRING; goto exit; } if (!initialized) { #if defined(HEAP_H) Heap_initialize(); #endif Log_initialize((Log_nameValue*)MQTTClient_getVersionInfo()); bstate->clients = ListInitialize(); Socket_outInitialize(); Socket_setWriteCompleteCallback(MQTTClient_writeComplete); handles = ListInitialize(); #if defined(OPENSSL) SSLSocket_initialize(); #endif initialized = 1; } m = malloc(sizeof(MQTTClients)); *handle = m; memset(m, '\0', sizeof(MQTTClients)); if (strncmp(URI_TCP, serverURI, strlen(URI_TCP)) == 0) serverURI += strlen(URI_TCP); #if defined(OPENSSL) else if (strncmp(URI_SSL, serverURI, strlen(URI_SSL)) == 0) { serverURI += strlen(URI_SSL); m->ssl = 1; } #endif m->serverURI = MQTTStrdup(serverURI); ListAppend(handles, m, sizeof(MQTTClients)); m->c = malloc(sizeof(Clients)); memset(m->c, '\0', sizeof(Clients)); m->c->context = m; m->c->outboundMsgs = ListInitialize(); m->c->inboundMsgs = ListInitialize(); m->c->messageQueue = ListInitialize(); m->c->clientID = MQTTStrdup(clientId); m->connect_sem = Thread_create_sem(); m->connack_sem = Thread_create_sem(); m->suback_sem = Thread_create_sem(); m->unsuback_sem = Thread_create_sem(); #if !defined(NO_PERSISTENCE) rc = MQTTPersistence_create(&(m->c->persistence), persistence_type, persistence_context); if (rc == 0) { rc = MQTTPersistence_initialize(m->c, m->serverURI); if (rc == 0) MQTTPersistence_restoreMessageQueue(m->c); } #endif ListAppend(bstate->clients, m->c, sizeof(Clients) + 3*sizeof(List)); exit: Thread_unlock_mutex(mqttclient_mutex); FUNC_EXIT_RC(rc); return rc; }
/** * Main broker startup function * @param argc number of elements of argv * @param argv command line strings * @return system return code */ int main(int argc, char* argv[]) { int rc = 0; #define BUILD_TIMESTAMP __DATE__ " " __TIME__ /* __TIMESTAMP__ */ #define BROKER_VERSION "1.3.0.2" /* __VERSION__ */ #define PRODUCT_NAME "Really Small Message Broker" static char* broker_version_eye ATTR_UNUSED = NULL; static char* broker_timestamp_eye ATTR_UNUSED = NULL; FUNC_ENTRY_NOLOG; broker_timestamp_eye = "RSMB_Timestamp " BUILD_TIMESTAMP; broker_version_eye = "RSMB_Version " BROKER_VERSION; BrokerState.version = BROKER_VERSION; BrokerState.timestamp = BUILD_TIMESTAMP; Heap_initialize(); Log_initialize(); Log(LOG_INFO, 9999, PRODUCT_NAME); Log(LOG_INFO, 9998, "Part of Project Mosquitto in Eclipse\n(" "http://projects.eclipse.org/projects/technology.mosquitto)"); getopts(argc, argv); if (Messages_initialize(&BrokerState) != 0) goto no_messages; Log(LOG_INFO, 53, "Version %s, %s", BrokerState.version, BrokerState.timestamp); Log(LOG_INFO, 54, "Features included: %s", features); Log(LOG_INFO, 9993, "Authors: Ian Craggs ([email protected]), Nicholas O'Leary"); if ((rc = Broker_startup()) == 0) { SubscriptionEngines_setRetained(BrokerState.se, "$SYS/broker/version", 0, BrokerState.version, strlen(BrokerState.version)); SubscriptionEngines_setRetained(BrokerState.se, "$SYS/broker/timestamp", 0, BrokerState.timestamp, strlen(BrokerState.timestamp)); while (BrokerState.state == BROKER_RUNNING) { Protocol_timeslice(); #if !defined(NO_BRIDGE) Bridge_timeslice(&BrokerState.bridge); #endif } Log(LOG_INFO, 46, NULL); #if !defined(NO_BRIDGE) Bridge_stop(&BrokerState.bridge); #endif while (BrokerState.state == BROKER_STOPPING) { Protocol_timeslice(); #if !defined(NO_BRIDGE) Bridge_timeslice(&BrokerState.bridge); #endif } } Broker_shutdown(rc); Log(LOG_INFO, 47, NULL); no_messages: Messages_terminate(); Log_terminate(); Heap_terminate(); /*FUNC_EXIT_NOLOG(rc); would anyone ever see this? */ return rc; }