Skip to content

Commit

Permalink
Merge pull request #159 from LiamBindle/develop
Browse files Browse the repository at this point in the history
catching up on recent developments
  • Loading branch information
LiamBindle authored Mar 8, 2022
2 parents be12c34 + 2dedfcd commit 659d0e1
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
bin/
build/
.idea/
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ endif()
if(MQTT_C_OpenSSL_SUPPORT)
find_package(OpenSSL REQUIRED)
target_link_libraries(mqttc INTERFACE OpenSSL::SSL)
target_include_directories(mqttc PUBLIC ${OPENSSL_INCLUDE_DIR})
target_compile_definitions(mqttc PUBLIC MQTT_USE_BIO)
endif()

Expand Down
1 change: 1 addition & 0 deletions examples/bio_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <mqtt.h>
#include "templates/bio_sockets.h"
Expand Down
19 changes: 18 additions & 1 deletion examples/openssl_publisher.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <mqtt.h>
#include "templates/openssl_sockets.h"
Expand Down Expand Up @@ -41,6 +42,8 @@ int main(int argc, const char *argv[])
const char* port;
const char* topic;
const char* ca_file;
const char* cert_path;
const char* key_path;

/* Load OpenSSL */
SSL_load_error_strings();
Expand Down Expand Up @@ -79,8 +82,22 @@ int main(int argc, const char *argv[])
topic = "datetime";
}

/* get client cert */
if (argc > 5) {
cert_path = argv[5];
} else {
cert_path = NULL;
}

/* get client key */
if (argc > 6) {
key_path = argv[6];
} else {
key_path = NULL;
}

/* open the non-blocking TCP socket (connecting to the broker) */
open_nb_socket(&sockfd, &ssl_ctx, addr, port, ca_file, NULL);
open_nb_socket(&sockfd, &ssl_ctx, addr, port, ca_file, NULL, cert_path, key_path);

if (sockfd == NULL) {
exit_example(EXIT_FAILURE, sockfd, NULL);
Expand Down
45 changes: 23 additions & 22 deletions examples/reconnect_subscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <mqtt.h>
#include "templates/posix_sockets.h"

/**
* @brief A structure that I will use to keep track of some data needed
* @brief A structure that I will use to keep track of some data needed
* to setup the connection to the broker.
*
*
* An instance of this struct will be created in my \c main(). Then, whenever
* \ref reconnect_client is called, this instance will be passed.
* \ref reconnect_client is called, this instance will be passed.
*/
struct reconnect_state_t {
const char* hostname;
Expand All @@ -29,8 +30,8 @@ struct reconnect_state_t {


/**
* @brief My reconnect callback. It will reestablish the connection whenever
* an error occurs.
* @brief My reconnect callback. It will reestablish the connection whenever
* an error occurs.
*/
void reconnect_client(struct mqtt_client* client, void **reconnect_state_vptr);

Expand All @@ -40,22 +41,22 @@ void reconnect_client(struct mqtt_client* client, void **reconnect_state_vptr);
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void* client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, int sockfd, pthread_t *client_daemon);


int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -97,8 +98,8 @@ int main(int argc, const char *argv[])
/* setup a client */
struct mqtt_client client;

mqtt_init_reconnect(&client,
reconnect_client, &reconnect_state,
mqtt_init_reconnect(&client,
reconnect_client, &reconnect_state,
publish_callback
);

Expand All @@ -114,18 +115,18 @@ int main(int argc, const char *argv[])
printf("%s listening for '%s' messages.\n", argv[0], topic);
printf("Press ENTER to inject an error.\n");
printf("Press CTRL-D to exit.\n\n");

/* block */
while(fgetc(stdin) != EOF) {
printf("Injecting error: \"MQTT_ERROR_SOCKET_ERROR\"\n");
client.error = MQTT_ERROR_SOCKET_ERROR;
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, client.socketfd, &client_daemon);
}

Expand All @@ -140,7 +141,7 @@ void reconnect_client(struct mqtt_client* client, void **reconnect_state_vptr)

/* Perform error handling here. */
if (client->error != MQTT_ERROR_INITIAL_RECONNECT) {
printf("reconnect_client: called while client was in error state \"%s\"\n",
printf("reconnect_client: called while client was in error state \"%s\"\n",
mqtt_error_str(client->error)
);
}
Expand All @@ -153,7 +154,7 @@ void reconnect_client(struct mqtt_client* client, void **reconnect_state_vptr)
}

/* Reinitialize the client. */
mqtt_reinit(client, sockfd,
mqtt_reinit(client, sockfd,
reconnect_state->sendbuf, reconnect_state->sendbufsz,
reconnect_state->recvbuf, reconnect_state->recvbufsz
);
Expand All @@ -176,7 +177,7 @@ void exit_example(int status, int sockfd, pthread_t *client_daemon)
exit(status);
}

void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* note that published->topic_name is NOT null-terminated (here we'll change it to a c-string) */
char* topic_name = (char*) malloc(published->topic_name_size + 1);
Expand All @@ -190,10 +191,10 @@ void publish_callback(void** unused, struct mqtt_response_publish *published)

void* client_refresher(void* client)
{
while(1)
while(1)
{
mqtt_sync((struct mqtt_client*) client);
usleep(100000U);
}
return NULL;
}
}
31 changes: 16 additions & 15 deletions examples/simple_publisher.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@

/**
* @file
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <mqtt.h>
#include "templates/posix_sockets.h"


/**
* @brief The function that would be called whenever a PUBLISH is received.
*
* @note This function is not used in this example.
*
* @note This function is not used in this example.
*/
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void* client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, int sockfd, pthread_t *client_daemon);

/**
* A simple program to that publishes the current time whenever ENTER is pressed.
* A simple program to that publishes the current time whenever ENTER is pressed.
*/
int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -122,13 +123,13 @@ int main(int argc, const char *argv[])
fprintf(stderr, "error: %s\n", mqtt_error_str(client.error));
exit_example(EXIT_FAILURE, sockfd, &client_daemon);
}
}
}

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, sockfd, &client_daemon);
}

Expand All @@ -141,17 +142,17 @@ void exit_example(int status, int sockfd, pthread_t *client_daemon)



void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* not used in this example */
}

void* client_refresher(void* client)
{
while(1)
while(1)
{
mqtt_sync((struct mqtt_client*) client);
usleep(100000U);
}
return NULL;
}
}
27 changes: 14 additions & 13 deletions examples/simple_subscriber.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

#include <mqtt.h>
#include "templates/posix_sockets.h"
Expand All @@ -17,21 +18,21 @@
void publish_callback(void** unused, struct mqtt_response_publish *published);

/**
* @brief The client's refresher. This function triggers back-end routines to
* @brief The client's refresher. This function triggers back-end routines to
* handle ingress/egress traffic to the broker.
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
*
* @note All this function needs to do is call \ref __mqtt_recv and
* \ref __mqtt_send every so often. I've picked 100 ms meaning that
* client ingress/egress traffic will be handled every 100 ms.
*/
void* client_refresher(void* client);

/**
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
* @brief Safelty closes the \p sockfd and cancels the \p client_daemon before \c exit.
*/
void exit_example(int status, int sockfd, pthread_t *client_daemon);

int main(int argc, const char *argv[])
int main(int argc, const char *argv[])
{
const char* addr;
const char* port;
Expand Down Expand Up @@ -98,15 +99,15 @@ int main(int argc, const char *argv[])
/* start publishing the time */
printf("%s listening for '%s' messages.\n", argv[0], topic);
printf("Press CTRL-D to exit.\n\n");

/* block */
while(fgetc(stdin) != EOF);
while(fgetc(stdin) != EOF);

/* disconnect */
printf("\n%s disconnecting from %s\n", argv[0], addr);
sleep(1);

/* exit */
/* exit */
exit_example(EXIT_SUCCESS, sockfd, &client_daemon);
}

Expand All @@ -119,7 +120,7 @@ void exit_example(int status, int sockfd, pthread_t *client_daemon)



void publish_callback(void** unused, struct mqtt_response_publish *published)
void publish_callback(void** unused, struct mqtt_response_publish *published)
{
/* note that published->topic_name is NOT null-terminated (here we'll change it to a c-string) */
char* topic_name = (char*) malloc(published->topic_name_size + 1);
Expand All @@ -133,10 +134,10 @@ void publish_callback(void** unused, struct mqtt_response_publish *published)

void* client_refresher(void* client)
{
while(1)
while(1)
{
mqtt_sync((struct mqtt_client*) client);
usleep(100000U);
}
return NULL;
}
}
Loading

0 comments on commit 659d0e1

Please sign in to comment.