Skip to content

Commit

Permalink
Move new() params to connect() (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdenCullen authored Jan 9, 2019
1 parent 2537e40 commit 87f2a23
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 27 deletions.
11 changes: 5 additions & 6 deletions include/aws/mqtt/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ void aws_mqtt_client_clean_up(struct aws_mqtt_client *client);
* \returns AWS_OP_SUCCESS on success, otherwise AWS_OP_ERR and aws_last_error() is set.
*/
AWS_MQTT_API
struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(
struct aws_mqtt_client *client,
const struct aws_byte_cursor *host_name,
uint16_t port,
struct aws_socket_options *socket_options,
struct aws_tls_connection_options *tls_options);
struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqtt_client *client);

/**
* Cleans up and destroys a connection object.
Expand Down Expand Up @@ -242,6 +237,10 @@ int aws_mqtt_client_connection_set_connection_interruption_handlers(
AWS_MQTT_API
int aws_mqtt_client_connection_connect(
struct aws_mqtt_client_connection *connection,
const struct aws_byte_cursor *host_name,
uint16_t port,
struct aws_socket_options *socket_options,
struct aws_tls_connection_options *tls_options,
const struct aws_byte_cursor *client_id,
bool clean_session,
uint16_t keep_alive_time,
Expand Down
19 changes: 9 additions & 10 deletions source/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,7 @@ static void s_outstanding_request_destroy(void *item) {
}
}

struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(
struct aws_mqtt_client *client,
const struct aws_byte_cursor *host_name,
uint16_t port,
struct aws_socket_options *socket_options,
struct aws_tls_connection_options *tls_options) {
struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(struct aws_mqtt_client *client) {

assert(client);

Expand All @@ -292,10 +287,6 @@ struct aws_mqtt_client_connection *aws_mqtt_client_connection_new(
AWS_ZERO_STRUCT(*connection);
connection->allocator = client->allocator;
connection->client = client;
connection->host_name = aws_string_new_from_array(connection->allocator, host_name->ptr, host_name->len);
connection->port = port;
connection->tls_options = tls_options;
connection->socket_options = socket_options;
connection->state = AWS_MQTT_CLIENT_STATE_DISCONNECTED;
connection->reconnect_timeouts.min = 1;
connection->reconnect_timeouts.max = 128;
Expand Down Expand Up @@ -484,6 +475,10 @@ int aws_mqtt_client_connection_set_connection_interruption_handlers(

int aws_mqtt_client_connection_connect(
struct aws_mqtt_client_connection *connection,
const struct aws_byte_cursor *host_name,
uint16_t port,
struct aws_socket_options *socket_options,
struct aws_tls_connection_options *tls_options,
const struct aws_byte_cursor *client_id,
bool clean_session,
uint16_t keep_alive_time,
Expand All @@ -494,6 +489,10 @@ int aws_mqtt_client_connection_connect(
return aws_raise_error(AWS_ERROR_MQTT_ALREADY_CONNECTED);
}

connection->host_name = aws_string_new_from_array(connection->allocator, host_name->ptr, host_name->len);
connection->port = port;
connection->tls_options = tls_options;
connection->socket_options = socket_options;
connection->state = AWS_MQTT_CLIENT_STATE_CONNECTING;
connection->clean_session = clean_session;
connection->keep_alive_time = keep_alive_time;
Expand Down
14 changes: 12 additions & 2 deletions tests/aws_iot_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,23 @@ int main(int argc, char **argv) {
aws_mqtt_client_init(&client, args.allocator, bootstrap);

struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(s_hostname);
args.connection = aws_mqtt_client_connection_new(&client, &host_name_cur, 8883, &socket_options, &tls_con_opt);
args.connection = aws_mqtt_client_connection_new(&client);

struct aws_byte_cursor will_cur = aws_byte_cursor_from_array(s_will_payload, WILL_PAYLOAD_LEN);
aws_mqtt_client_connection_set_will(args.connection, &subscribe_topic_cur, 1, false, &will_cur);

struct aws_byte_cursor client_id_cur = aws_byte_cursor_from_string(s_client_id);
aws_mqtt_client_connection_connect(args.connection, &client_id_cur, true, 0, s_mqtt_on_connection_complete, &args);
aws_mqtt_client_connection_connect(
args.connection,
&host_name_cur,
8883,
&socket_options,
&tls_con_opt,
&client_id_cur,
true,
0,
s_mqtt_on_connection_complete,
&args);

aws_mutex_lock(&mutex);
ASSERT_SUCCESS(aws_condition_variable_wait(&condition_variable, &mutex));
Expand Down
22 changes: 13 additions & 9 deletions tests/paho_client_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@
# include <unistd.h>
#endif

static struct aws_byte_cursor s_client_id_1 = {
static struct aws_byte_cursor s_client_id = {
.ptr = (uint8_t *)"MyClientId1",
.len = 11,
};
static struct aws_byte_cursor s_client_id_2 = {
.ptr = (uint8_t *)"MyClientId2",
.len = 11,
};
AWS_STATIC_STRING_FROM_LITERAL(s_subscribe_topic, "a/b");
AWS_STATIC_STRING_FROM_LITERAL(s_hostname, "localhost");

Expand Down Expand Up @@ -232,14 +228,23 @@ int main(int argc, char **argv) {
ASSERT_SUCCESS(aws_mqtt_client_init(&client, args.allocator, bootstrap));

struct aws_byte_cursor host_name_cur = aws_byte_cursor_from_string(s_hostname);
args.connection = aws_mqtt_client_connection_new(&client, &host_name_cur, 1883, &options, NULL);
args.connection = aws_mqtt_client_connection_new(&client);
ASSERT_NOT_NULL(args.connection);

aws_mqtt_client_connection_set_connection_interruption_handlers(
args.connection, s_mqtt_on_interrupted, NULL, s_mqtt_on_resumed, NULL);

ASSERT_SUCCESS(aws_mqtt_client_connection_connect(
args.connection, &s_client_id_1, true, 0, s_mqtt_on_connection_complete, &args));
args.connection,
&host_name_cur,
1883,
&options,
NULL,
&s_client_id,
true,
0,
s_mqtt_on_connection_complete,
&args));

/* Wait for connack */
aws_mutex_lock(&mutex);
Expand Down Expand Up @@ -268,8 +273,7 @@ int main(int argc, char **argv) {

printf("3 done\n");

ASSERT_SUCCESS(aws_mqtt_client_connection_connect(
args.connection, &s_client_id_2, true, 0, s_mqtt_on_connection_complete, &args));
ASSERT_SUCCESS(aws_mqtt_client_connection_reconnect(args.connection, s_mqtt_on_connection_complete, &args));

/* Wait for connack */
aws_mutex_lock(&mutex);
Expand Down

0 comments on commit 87f2a23

Please sign in to comment.