move utils into subfolder

manager thread (wip)
This commit is contained in:
Nicolas Dextraze
2018-03-19 16:39:17 -07:00
parent 8d81a8a55a
commit 9498cc74e0
17 changed files with 194 additions and 72 deletions

View File

@@ -5,16 +5,38 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#include "esc.h"
#include "socket.h"
#include "buffer.h"
#include "utils/socket.h"
#include "utils/buffer.h"
#include "proto.h"
#include "tcp_package.h"
#include "utils/thread.h"
#include "utils/mutex.h"
const char* string_copy(const char *src) {
char* dst = malloc(strlen(src)+1);
strcpy(dst, src);
return dst;
}
typedef int bool_t;
const bool_t BOOL_TRUE = 1;
const bool_t BOOL_FALSE = 0;
#define ERROR_BUFFER_SIZE 4096
esc_error_t error_create(int code, char* format, ...) {
va_list vl;
va_start(vl, format);
char buf[ERROR_BUFFER_SIZE];
size_t size = (size_t)snprintf(buf, ERROR_BUFFER_SIZE, format, vl);
char* msg = malloc(size+1);
strcpy(msg, buf);
esc_error_t res = {code, msg};
return res;
}
struct st_connection_settings {
bool_t use_ssl_connection;
};
@@ -40,10 +62,13 @@ typedef const node_endpoints_t* (*endpoint_discoverer_t)(const void* discover_da
struct st_connection {
esc_connection_settings_t settings;
const char* name;
void* discoverer_data;
endpoint_discoverer_t discover;
socket_t tcp_conn;
ProtobufCAllocator protobuf_c_allocator;
thread_t manager_thread;
mutex_t mutex_lock;
};
struct st_static_endpoint_discoverer {
@@ -134,9 +159,32 @@ const esc_connection_t* esc_connection_create(const esc_connection_settings_t* c
conn->protobuf_c_allocator.alloc = protobuf_c_alloc;
conn->protobuf_c_allocator.free = protobuf_c_free;
conn->protobuf_c_allocator.allocator_data = 0;
if (connection_name == 0 || strcmp(connection_name, "") == 0) {
const esc_uuid_t* uuid = esc_uuid_create();
char buf[40];
esc_uuid_format(uuid, buf, 40);
conn->name = string_copy(buf);
free(uuid);
} else {
conn->name = connection_name;
}
return conn;
}
void* connection_thread(void* arg) {
const esc_connection_t* conn = arg;
while(1) {
//mutex_lock(conn->mutex_lock);
//if (socket_readable(conn->tcp_conn)) {
//}
//mutex_unlock(conn->mutex_lock);
sleep(1);
}
}
// return 0 on success
// return non-zero on failure and sets last_error on connection
int esc_connection_connect(const esc_connection_t* conn) {
@@ -156,7 +204,7 @@ int esc_connection_connect(const esc_connection_t* conn) {
// build message
EventStore__Client__Messages__IdentifyClient identify_client;
event_store__client__messages__identify_client__init(&identify_client);
identify_client.connection_name = "abc123";
identify_client.connection_name = (char*)conn->name;
identify_client.version = 1;
size_t s = event_store__client__messages__identify_client__get_packed_size(&identify_client);
uint8_t buffer[s];
@@ -174,6 +222,12 @@ int esc_connection_connect(const esc_connection_t* conn) {
return -5;
}
_conn->mutex_lock = mutex_create();
_conn->manager_thread = thread_create(connection_thread, _conn);
if (thread_start(_conn->manager_thread)) {
return -6;
}
return 0;
}
@@ -184,12 +238,6 @@ const esc_credentials_t* esc_credentials_create(const char* username, const char
return creds;
}
const char* string_copy(const char *src) {
char* dst = malloc(strlen(src)+1);
strcpy(dst, src);
return dst;
}
const esc_recorded_event_t* recorded_event_create(EventStore__Client__Messages__EventRecord* msg) {
if (msg == 0) return 0;
esc_recorded_event_t* ev = malloc(sizeof(esc_recorded_event_t));
@@ -259,6 +307,10 @@ const esc_all_events_slice_t* esc_connection_read_all_forward(const esc_connecti
return result;
}
void esc_connection_close(const esc_connection_t* conn) {
socket_close(conn->tcp_conn);
}
const char* esc_position_format(const esc_position_t* position, char* buffer, size_t buf_size) {
snprintf(buffer, buf_size, "%lu/%lu", position->prepare_position, position->commit_position);
return buffer;