Autogenerate client IDs to make instantiation easier. Set us up for client ID rotation later.
This commit is contained in:
@@ -204,6 +204,13 @@ static void cosmo_handle_message(cosmo *instance, json_t *event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cosmo_handle_client_id_change(cosmo *instance) {
|
||||||
|
if (instance->callbacks.client_id_change) {
|
||||||
|
cosmo_log(instance, "callbacks.client_id_change()");
|
||||||
|
instance->callbacks.client_id_change(instance->passthrough, instance->client_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void cosmo_handle_connect(cosmo *instance) {
|
static void cosmo_handle_connect(cosmo *instance) {
|
||||||
if (instance->connect_state == CONNECTED) {
|
if (instance->connect_state == CONNECTED) {
|
||||||
return;
|
return;
|
||||||
@@ -569,12 +576,17 @@ cosmo *cosmo_create(const char *base_url, const char *client_id, const cosmo_cal
|
|||||||
|
|
||||||
instance->debug = getenv("COSMO_DEBUG");
|
instance->debug = getenv("COSMO_DEBUG");
|
||||||
|
|
||||||
strcpy(instance->client_id, client_id);
|
|
||||||
cosmo_uuid(instance->instance_id);
|
|
||||||
|
|
||||||
memcpy(&instance->callbacks, callbacks, sizeof(instance->callbacks));
|
memcpy(&instance->callbacks, callbacks, sizeof(instance->callbacks));
|
||||||
instance->passthrough = passthrough;
|
instance->passthrough = passthrough;
|
||||||
|
|
||||||
|
if (client_id) {
|
||||||
|
strcpy(instance->client_id, client_id);
|
||||||
|
} else {
|
||||||
|
cosmo_uuid(instance->client_id);
|
||||||
|
cosmo_handle_client_id_change(instance);
|
||||||
|
}
|
||||||
|
cosmo_uuid(instance->instance_id);
|
||||||
|
|
||||||
assert(!pthread_mutex_init(&instance->lock, NULL));
|
assert(!pthread_mutex_init(&instance->lock, NULL));
|
||||||
assert(!pthread_cond_init(&instance->cond, NULL));
|
assert(!pthread_cond_init(&instance->cond, NULL));
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#define COSMO_UUID_SIZE 37
|
#define COSMO_UUID_SIZE 37
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
void (*client_id_change)(void *, const char *);
|
||||||
void (*connect)(void *);
|
void (*connect)(void *);
|
||||||
void (*disconnect)(void *);
|
void (*disconnect)(void *);
|
||||||
void (*login)(void *);
|
void (*login)(void *);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "cosmopolite.h"
|
#include "cosmopolite.h"
|
||||||
@@ -16,12 +17,23 @@ typedef struct {
|
|||||||
pthread_cond_t cond;
|
pthread_cond_t cond;
|
||||||
|
|
||||||
const json_t *last_message;
|
const json_t *last_message;
|
||||||
|
const char *client_id;
|
||||||
|
bool client_id_change_fired;
|
||||||
bool logout_fired;
|
bool logout_fired;
|
||||||
bool connect_fired;
|
bool connect_fired;
|
||||||
bool disconnect_fired;
|
bool disconnect_fired;
|
||||||
} test_state;
|
} test_state;
|
||||||
|
|
||||||
|
|
||||||
|
void on_client_id_change(void *passthrough, const char *client_id) {
|
||||||
|
test_state *state = passthrough;
|
||||||
|
assert(!pthread_mutex_lock(&state->lock));
|
||||||
|
state->client_id_change_fired = true;
|
||||||
|
state->client_id = client_id;
|
||||||
|
assert(!pthread_cond_signal(&state->cond));
|
||||||
|
assert(!pthread_mutex_unlock(&state->lock));
|
||||||
|
}
|
||||||
|
|
||||||
void on_connect(void *passthrough) {
|
void on_connect(void *passthrough) {
|
||||||
test_state *state = passthrough;
|
test_state *state = passthrough;
|
||||||
assert(!pthread_mutex_lock(&state->lock));
|
assert(!pthread_mutex_lock(&state->lock));
|
||||||
@@ -56,6 +68,18 @@ void on_message(const json_t *message, void *passthrough) {
|
|||||||
assert(!pthread_mutex_unlock(&state->lock));
|
assert(!pthread_mutex_unlock(&state->lock));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wait_for_client_id_change(test_state *state) {
|
||||||
|
assert(!pthread_mutex_lock(&state->lock));
|
||||||
|
while (!state->client_id_change_fired) {
|
||||||
|
assert(!pthread_cond_wait(&state->cond, &state->lock));
|
||||||
|
}
|
||||||
|
|
||||||
|
state->client_id_change_fired = false;
|
||||||
|
assert(!pthread_mutex_unlock(&state->lock));
|
||||||
|
|
||||||
|
assert(strlen(state->client_id));
|
||||||
|
}
|
||||||
|
|
||||||
const json_t *wait_for_message(test_state *state) {
|
const json_t *wait_for_message(test_state *state) {
|
||||||
assert(!pthread_mutex_lock(&state->lock));
|
assert(!pthread_mutex_lock(&state->lock));
|
||||||
while (!state->last_message) {
|
while (!state->last_message) {
|
||||||
@@ -105,6 +129,8 @@ test_state *create_test_state() {
|
|||||||
assert(!pthread_mutex_init(&ret->lock, NULL));
|
assert(!pthread_mutex_init(&ret->lock, NULL));
|
||||||
assert(!pthread_cond_init(&ret->cond, NULL));
|
assert(!pthread_cond_init(&ret->cond, NULL));
|
||||||
ret->last_message = NULL;
|
ret->last_message = NULL;
|
||||||
|
ret->client_id = NULL;
|
||||||
|
ret->client_id_change_fired = false;
|
||||||
ret->logout_fired = false;
|
ret->logout_fired = false;
|
||||||
ret->connect_fired = false;
|
ret->connect_fired = false;
|
||||||
ret->disconnect_fired = false;
|
ret->disconnect_fired = false;
|
||||||
@@ -118,18 +144,15 @@ void destroy_test_state(test_state *state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cosmo *create_client(test_state *state) {
|
cosmo *create_client(test_state *state) {
|
||||||
char client_id[COSMO_UUID_SIZE];
|
|
||||||
cosmo_uuid(client_id);
|
|
||||||
|
|
||||||
cosmo_callbacks callbacks = {
|
cosmo_callbacks callbacks = {
|
||||||
|
.client_id_change = on_client_id_change,
|
||||||
.connect = on_connect,
|
.connect = on_connect,
|
||||||
.disconnect = on_disconnect,
|
.disconnect = on_disconnect,
|
||||||
.logout = on_logout,
|
.logout = on_logout,
|
||||||
.message = on_message,
|
.message = on_message,
|
||||||
};
|
};
|
||||||
|
|
||||||
cosmo *ret = cosmo_create("https://playground.cosmopolite.org/cosmopolite", client_id, &callbacks, state);
|
cosmo *ret = cosmo_create("https://playground.cosmopolite.org/cosmopolite", NULL, &callbacks, state);
|
||||||
// ret->debug = true;
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,6 +205,13 @@ bool test_message_round_trip(test_state *state) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool test_client_id_change_fires(test_state *state) {
|
||||||
|
cosmo *client = create_client(state);
|
||||||
|
wait_for_client_id_change(state);
|
||||||
|
cosmo_shutdown(client);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool test_connect_logout_fires(test_state *state) {
|
bool test_connect_logout_fires(test_state *state) {
|
||||||
cosmo *client = create_client(state);
|
cosmo *client = create_client(state);
|
||||||
wait_for_connect(state);
|
wait_for_connect(state);
|
||||||
@@ -278,6 +308,7 @@ bool test_complex_object(test_state *state) {
|
|||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
RUN_TEST(test_create_shutdown);
|
RUN_TEST(test_create_shutdown);
|
||||||
|
RUN_TEST(test_client_id_change_fires);
|
||||||
RUN_TEST(test_connect_logout_fires);
|
RUN_TEST(test_connect_logout_fires);
|
||||||
RUN_TEST(test_message_round_trip);
|
RUN_TEST(test_message_round_trip);
|
||||||
RUN_TEST(test_resubscribe);
|
RUN_TEST(test_resubscribe);
|
||||||
|
|||||||
Reference in New Issue
Block a user