Skip to content

Commit

Permalink
Implement library-clean-up (#81)
Browse files Browse the repository at this point in the history
* Implement library-clean-up

* Restrict atrocities
  • Loading branch information
ColdenCullen authored Aug 22, 2019
1 parent 56d8b52 commit 8dfb054
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 29 deletions.
3 changes: 3 additions & 0 deletions include/aws/mqtt/mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ void aws_mqtt_library_init(struct aws_allocator *allocator);
AWS_MQTT_API
void aws_mqtt_library_clean_up(void);

AWS_MQTT_API
void aws_mqtt_fatal_assert_library_initialized(void);

AWS_EXTERN_C_END

#endif /* AWS_MQTT_MQTT_H */
2 changes: 2 additions & 0 deletions source/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ int aws_mqtt_client_init(
struct aws_allocator *allocator,
struct aws_client_bootstrap *bootstrap) {

aws_mqtt_fatal_assert_library_initialized();

AWS_LOGF_DEBUG(AWS_LS_MQTT_CLIENT, "client=%p: Initalizing MQTT client", (void *)client);

AWS_ZERO_STRUCT(*client);
Expand Down
74 changes: 51 additions & 23 deletions source/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include <aws/io/logging.h>

#ifdef AWS_MQTT_WITH_WEBSOCKETS
# include <aws/http/http.h>
#endif

/*******************************************************************************
* Topic Validation
******************************************************************************/
Expand Down Expand Up @@ -96,17 +100,8 @@ bool aws_mqtt_is_valid_topic_filter(const struct aws_byte_cursor *topic_filter)
* Library Init
******************************************************************************/

void aws_mqtt_library_init(struct aws_allocator *allocator) {

(void)allocator;

static bool s_library_initialized = false;
if (!s_library_initialized) {

s_library_initialized = true;

#define AWS_DEFINE_ERROR_INFO_MQTT(C, ES) AWS_DEFINE_ERROR_INFO(C, ES, "libaws-c-mqtt")
/* clang-format off */
/* clang-format off */
static struct aws_error_info s_errors[] = {
AWS_DEFINE_ERROR_INFO_MQTT(
AWS_ERROR_MQTT_INVALID_RESERVED_BITS,
Expand Down Expand Up @@ -151,29 +146,62 @@ void aws_mqtt_library_init(struct aws_allocator *allocator) {
AWS_ERROR_MQTT_BUILT_WITHOUT_WEBSOCKETS,
"Library built without MQTT_WITH_WEBSOCKETS option."),
};
/* clang-format on */
/* clang-format on */
#undef AWS_DEFINE_ERROR_INFO_MQTT

static struct aws_error_info_list s_error_list = {
.error_list = s_errors,
.count = AWS_ARRAY_SIZE(s_errors),
};
aws_register_error_info(&s_error_list);
static struct aws_error_info_list s_error_list = {
.error_list = s_errors,
.count = AWS_ARRAY_SIZE(s_errors),
};

/* clang-format off */
/* clang-format off */
static struct aws_log_subject_info s_logging_subjects[] = {
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_GENERAL, "mqtt", "Misc MQTT logging"),
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_CLIENT, "mqtt-client", "MQTT client and connections"),
DEFINE_LOG_SUBJECT_INFO(AWS_LS_MQTT_TOPIC_TREE, "mqtt-topic-tree", "MQTT subscription tree"),
};
/* clang-format on */
/* clang-format on */

static struct aws_log_subject_info_list s_logging_subjects_list = {
.subject_list = s_logging_subjects,
.count = AWS_ARRAY_SIZE(s_logging_subjects),
};
static struct aws_log_subject_info_list s_logging_subjects_list = {
.subject_list = s_logging_subjects,
.count = AWS_ARRAY_SIZE(s_logging_subjects),
};

static bool s_mqtt_library_initialized = false;

void aws_mqtt_library_init(struct aws_allocator *allocator) {

(void)allocator;

if (!s_mqtt_library_initialized) {
s_mqtt_library_initialized = true;
aws_io_library_init(allocator);
#ifdef AWS_MQTT_WITH_WEBSOCKETS
aws_http_library_init(allocator);
#endif
aws_register_error_info(&s_error_list);
aws_register_log_subject_info_list(&s_logging_subjects_list);
}
}

void aws_mqtt_library_clean_up(void) {}
void aws_mqtt_library_clean_up(void) {
if (s_mqtt_library_initialized) {
s_mqtt_library_initialized = false;
aws_unregister_error_info(&s_error_list);
aws_unregister_log_subject_info_list(&s_logging_subjects_list);
#ifdef AWS_MQTT_WITH_WEBSOCKETS
aws_http_library_clean_up();
#endif
aws_io_library_clean_up();
}
}

void aws_mqtt_fatal_assert_library_initialized(void) {
if (!s_mqtt_library_initialized) {
AWS_LOGF_FATAL(
AWS_LS_MQTT_GENERAL,
"aws_mqtt_library_init() must be called before using any functionality in aws-c-mqtt.");

AWS_FATAL_ASSERT(s_mqtt_library_initialized);
}
}
8 changes: 2 additions & 6 deletions tests/aws_iot_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,6 @@ int main(int argc, char **argv) {
args.mutex = &mutex;
args.condition_variable = &condition_variable;

aws_tls_init_static_state(args.allocator);
aws_load_error_strings();
aws_io_load_error_strings();
aws_io_load_log_subject_strings();
aws_mqtt_library_init(args.allocator);

struct aws_logger_standard_options logger_options = {
Expand Down Expand Up @@ -340,10 +336,10 @@ int main(int argc, char **argv) {

aws_tls_ctx_destroy(tls_ctx);

aws_tls_clean_up_static_state();

aws_logger_clean_up(&logger);

aws_mqtt_library_clean_up();

ASSERT_UINT_EQUALS(iot_client_alloc_impl.freed, iot_client_alloc_impl.allocated);

return AWS_OP_SUCCESS;
Expand Down

0 comments on commit 8dfb054

Please sign in to comment.