Start of a public header.
This commit is contained in:
31
clients/c/cosmopolite.h
Normal file
31
clients/c/cosmopolite.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef _COSMOPOLITE_H
|
||||||
|
#define _COSMOPOLITE_H
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <jansson.h>
|
||||||
|
|
||||||
|
#define COSMO_UUID_SIZE 37
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *api_url;
|
||||||
|
char client_id[COSMO_UUID_SIZE];
|
||||||
|
char instance_id[COSMO_UUID_SIZE];
|
||||||
|
|
||||||
|
pthread_mutex_t lock;
|
||||||
|
pthread_cond_t cond;
|
||||||
|
bool shutdown;
|
||||||
|
json_t *command_queue;
|
||||||
|
uint64_t next_delay_ms;
|
||||||
|
|
||||||
|
pthread_t thread;
|
||||||
|
} cosmo;
|
||||||
|
|
||||||
|
void cosmo_generate_uuid(char *uuid);
|
||||||
|
|
||||||
|
cosmo *cosmo_create(const char *base_url, const char *client_id);
|
||||||
|
void cosmo_destroy(cosmo *instance);
|
||||||
|
|
||||||
|
json_t *cosmo_subject(const char *name, const char *readable_only_by, const char *writeable_only_by);
|
||||||
|
void cosmo_subscribe(cosmo *instance, const json_t *subject, const json_int_t messages, const json_int_t last_id);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <pthread.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -9,11 +8,11 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <jansson.h>
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <uuid/uuid.h>
|
#include <uuid/uuid.h>
|
||||||
|
|
||||||
#define COSMO_UUID_SIZE 37
|
#include "cosmopolite.h"
|
||||||
|
|
||||||
#define COSMO_CHECK_SECONDS 10
|
#define COSMO_CHECK_SECONDS 10
|
||||||
|
|
||||||
#define min(a, b) ((a) < (b) ? (a) : (b))
|
#define min(a, b) ((a) < (b) ? (a) : (b))
|
||||||
@@ -24,20 +23,6 @@
|
|||||||
#define DELAY_EXPONENT 1.1
|
#define DELAY_EXPONENT 1.1
|
||||||
#define DELAY_STAGGER_FACTOR 10
|
#define DELAY_STAGGER_FACTOR 10
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char *api_url;
|
|
||||||
char client_id[COSMO_UUID_SIZE];
|
|
||||||
char instance_id[COSMO_UUID_SIZE];
|
|
||||||
|
|
||||||
pthread_mutex_t lock;
|
|
||||||
pthread_cond_t cond;
|
|
||||||
bool shutdown;
|
|
||||||
json_t *command_queue;
|
|
||||||
uint64_t next_delay_ms;
|
|
||||||
|
|
||||||
pthread_t thread;
|
|
||||||
} cosmo;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char *send_buf;
|
char *send_buf;
|
||||||
size_t send_buf_len;
|
size_t send_buf_len;
|
||||||
@@ -262,7 +247,7 @@ void cosmo_generate_uuid(char *uuid) {
|
|||||||
uuid_unparse_lower(uu, uuid);
|
uuid_unparse_lower(uu, uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t *cosmo_subject(char *name, char *readable_only_by, char *writeable_only_by) {
|
json_t *cosmo_subject(const char *name, const char *readable_only_by, const char *writeable_only_by) {
|
||||||
json_t *ret = json_pack("{ss}", "name", name);
|
json_t *ret = json_pack("{ss}", "name", name);
|
||||||
if (readable_only_by) {
|
if (readable_only_by) {
|
||||||
json_object_set_new(ret, "readable_only_by", json_string(readable_only_by));
|
json_object_set_new(ret, "readable_only_by", json_string(readable_only_by));
|
||||||
@@ -273,7 +258,7 @@ json_t *cosmo_subject(char *name, char *readable_only_by, char *writeable_only_b
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cosmo_subscribe(cosmo *instance, json_t *subject, json_int_t messages, json_int_t last_id) {
|
void cosmo_subscribe(cosmo *instance, const json_t *subject, const json_int_t messages, const json_int_t last_id) {
|
||||||
json_t *arguments = json_pack("{sO}", "subject", subject);
|
json_t *arguments = json_pack("{sO}", "subject", subject);
|
||||||
if (messages) {
|
if (messages) {
|
||||||
json_object_set_new(arguments, "messages", json_integer(messages));
|
json_object_set_new(arguments, "messages", json_integer(messages));
|
||||||
@@ -284,7 +269,7 @@ void cosmo_subscribe(cosmo *instance, json_t *subject, json_int_t messages, json
|
|||||||
cosmo_send_command(instance, cosmo_command("subscribe", arguments));
|
cosmo_send_command(instance, cosmo_command("subscribe", arguments));
|
||||||
}
|
}
|
||||||
|
|
||||||
cosmo *cosmo_create(char *base_url, char *client_id) {
|
cosmo *cosmo_create(const char *base_url, const char *client_id) {
|
||||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||||
srandomdev();
|
srandomdev();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user