Skip to content

Commit

Permalink
ref-count and objectify stuff, see also boazsegev/facil.io#72
Browse files Browse the repository at this point in the history
  • Loading branch information
fbrausse committed Aug 31, 2019
1 parent 5f91bc3 commit ed0c905
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 137 deletions.
59 changes: 38 additions & 21 deletions ksc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ class socket(Structure):
pass

def __init__(self):
self._ksc = CDLL('libksc.so')
self._ksc = CDLL('./libksc.so')

ksc_log_p = POINTER(ksc_ffi.log)
ksc_ffi_log_p = POINTER(ksc_ffi.log)
ksc_envelope_p = POINTER(ksc_ffi.envelope)
ksc_data_p = POINTER(ksc_ffi.data)
ksc_p = POINTER(ksc_ffi.socket)

for k, v in {
'log_create': (ksc_log_p, (c_int, c_char_p)),
'log_destroy': (c_void_p, (ksc_log_p,)),
'log_restrict_context': (c_int, (ksc_log_p, c_char_p, c_char_p)),
'log_create': (ksc_ffi_log_p, (c_int, c_char_p)),
'log_unref': (c_void_p, (ksc_ffi_log_p,)),
'log_restrict_context': (c_int, (ksc_ffi_log_p, c_char_p, c_char_p)),
'envelope_get_source': (c_char_p, (ksc_envelope_p,)),
'envelope_get_source_device_id': (c_int64, (ksc_envelope_p,)),
'envelope_get_timestamp': (c_int64, (ksc_envelope_p,)),
Expand All @@ -45,7 +45,7 @@ def __init__(self):
CFUNCTYPE(c_int, ksc_p, ksc_envelope_p, ksc_data_p), # on_data
CFUNCTYPE(None, ksc_p), # on_open
CFUNCTYPE(None, c_void_p, c_void_p), # on_close
ksc_log_p,
ksc_ffi_log_p,
c_char_p, # server_cert_path
c_int, # on_close_do_reconnect
c_void_p)),
Expand All @@ -62,18 +62,27 @@ def __init__(self):
getattr(self._ksc, k2).argtypes) = v
setattr(self, k, getattr(self._ksc, k2))

class log:
def __init__(self, fd, level, restricted_contexts = {}):
self.fd = fd
self.level = level
self.restricted_contexts = restricted_contexts

def __del__(self):
print('log __del__')

class ksc:
def __init__(self):
self._ffi = ksc_ffi()

# level is one of 'none', 'error', 'warn', 'info', 'note', 'debug'
def log_create(self, fd, level):
def _log_create(self, fd, level):
return self._ffi.log_create(fd, level.encode())

def log_destroy(self, log):
self._ffi.log_destroy(log)
def _log_unref(self, log):
self._ffi.log_unref(log)

def log_restrict_context(self, log, desc, level):
def _log_restrict_context(self, log, desc, level):
return self._ffi.log_restrict_context(log, desc.encode(),
level.encode())

Expand All @@ -84,14 +93,23 @@ def _zero(obj):
def start(self, json_store_path, server_cert_path, log = None,
on_receipt = None, on_data = None, on_open = None,
on_close = None, on_close_do_reconnect = False, data = None):
return self._ffi.start(json_store_path.encode(),
self._ffi.start.argtypes[1](ksc._zero(on_receipt)),
self._ffi.start.argtypes[2](ksc._zero(on_data)),
self._ffi.start.argtypes[3](ksc._zero(on_open)),
self._ffi.start.argtypes[4](ksc._zero(on_close)),
log,
server_cert_path.encode(),
on_close_do_reconnect, data)
if log is not None:
ffi_log = self._log_create(log.fd, log.level)
for desc, level in log.restricted_contexts.items():
self._log_restrict_context(ffi_log, desc, level)
else:
ffi_log = None
r = self._ffi.start(json_store_path.encode(),
self._ffi.start.argtypes[1](ksc._zero(on_receipt)),
self._ffi.start.argtypes[2](ksc._zero(on_data)),
self._ffi.start.argtypes[3](ksc._zero(on_open)),
self._ffi.start.argtypes[4](ksc._zero(on_close)),
ffi_log,
server_cert_path.encode(),
on_close_do_reconnect, data)
if ffi_log is not None:
self._log_unref(ffi_log)
return r

def stop(self, k):
self._ffi.stop(k)
Expand All @@ -102,10 +120,9 @@ def send_message(self, k, recipient, body, end_session = False, on_response = No
data)

"""
from ksc import ksc
from ksc import ksc, log
k = ksc()
log = k.log_create(2, 'note')
sock = k.start(LOCAL_PATH, 'share/whisper.store.asn1', log = log)
sock = k.start(LOCAL_PATH, 'share/whisper.store.asn1', log = log(2, 'debug'))
k.send_message(sock, NUMBER, 'hi from Python')
k.stop(sock)
"""
49 changes: 30 additions & 19 deletions src/ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,35 @@

#include "SignalService.pb-c.h"

struct ksc_log * ksc_ffi_log_create(int fd, const char *level)
/* .log.context_lvls contains dynamically allocated .desc strings */
struct ksc_ffi_log {
struct ksc_log log;
};

struct ksc_ffi_log * ksc_ffi_log_create(int fd, const char *level)
{
struct ksc_log log = { .fd = fd };
if (!ksc_log_lvl_parse(level, &log.max_lvl))
struct ksc_ffi_log log = { { .fd = fd } };
if (!ksc_log_lvl_parse(level, &log.log.max_lvl))
return NULL;
REF_INIT(&log.log);
return ksc_memdup(&log, sizeof(log));
}

void ksc_ffi_log_destroy(struct ksc_log *log)
void ksc_ffi_log_destroy(struct ksc_ffi_log *log)
{
if (log)
ksc_log_fini(log);
ksc_free(log);
if (!log)
return;
KSC_DEBUG(DEBUG, "ffi: log-unref with count %zu\n", log->log.ref_counted.cnt);
if (!UNREF(&log->log)) {
for (struct ksc_log__context_lvl *c = log->log.context_lvls; c;
c = c->next)
free((char *)c->desc);
ksc_log_fini(&log->log);
ksc_free(log);
}
}

int ksc_ffi_log_restrict_context(struct ksc_log *log, const char *desc,
int ksc_ffi_log_restrict_context(struct ksc_ffi_log *log, const char *desc,
const char *level)
{
if (!log)
Expand All @@ -34,11 +47,11 @@ int ksc_ffi_log_restrict_context(struct ksc_log *log, const char *desc,
if (!ksc_log_lvl_parse(level, &max_lvl))
return -EINVAL;
struct ksc_log__context_lvl cl = {
.next = log->context_lvls,
.desc = desc,
.next = log->log.context_lvls,
.desc = strdup(desc),
.max_lvl = max_lvl,
};
log->context_lvls = ksc_memdup(&cl, sizeof(cl));
log->log.context_lvls = ksc_memdup(&cl, sizeof(cl));
return 0;
}

Expand Down Expand Up @@ -103,7 +116,7 @@ struct ksc_ffi {
ws_s *ws;
struct json_store *js;
struct ksc_ws *kws;
struct ksc_log *log;
struct ksc_ffi_log *log;
pthread_t thread;
int (*on_receipt)(const struct ksc_ffi *,
struct ksc_ffi_envelope *e);
Expand Down Expand Up @@ -159,9 +172,8 @@ static void ffi_on_open(ws_s *ws, struct ksc_ws *kws)

static void ffi_destroy(struct ksc_ffi *ffi)
{
ksc_ffi_log_destroy(ffi->log);
if (ffi->js)
json_store_destroy(ffi->js);
json_store_unref(ffi->js);
ksc_free(ffi);
}

Expand All @@ -187,7 +199,7 @@ struct ksc_ffi * ksc_ffi_start(const char *json_store_path,
struct ksc_ffi_data *c),
void (*on_open)(const struct ksc_ffi *),
void (*on_close)(intptr_t uuid, void *udata),
struct ksc_log *log,
struct ksc_ffi_log *log,
const char *server_cert_path,
int on_close_do_reconnect,
void *udata
Expand All @@ -204,19 +216,18 @@ struct ksc_ffi * ksc_ffi_start(const char *json_store_path,
struct ksc_ffi *ffi = ksc_memdup(&ffi_, sizeof(ffi_));
if (!ffi)
return NULL;
ffi->js = json_store_create(json_store_path, log);
ffi->js = json_store_create(json_store_path, &log->log);
if (!ffi->js)
goto error;
ffi->log = ksc_memdup(ffi->log ? ffi->log : &KSC_DEFAULT_LOG,
sizeof(*ffi->log));
ffi->log = log;
struct ksc_ws *kws = ksc_ws_connect_service(ffi->js,
.on_receipt = ffi_on_receipt,
.on_content = ffi_on_content,
.on_open = ffi_on_open,
.on_close = ffi_on_close,
.udata = ffi,
.signal_log_ctx = { "signal ctx", "95" /* bright magenta */ },
.log = log,
.log = &log->log,
.server_cert_path = server_cert_path,
.on_close_do_reconnect = on_close_do_reconnect,
);
Expand Down
14 changes: 7 additions & 7 deletions src/ffi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

/* logging */

struct ksc_log;
struct ksc_ffi_log;

struct ksc_log * ksc_ffi_log_create(int fd, const char *level);
void ksc_ffi_log_destroy(struct ksc_log *log);
int ksc_ffi_log_restrict_context(struct ksc_log *log,
const char *desc,
const char *level);
struct ksc_ffi_log * ksc_ffi_log_create(int fd, const char *level);
void ksc_ffi_log_destroy(struct ksc_ffi_log *log);
int ksc_ffi_log_restrict_context(struct ksc_ffi_log *log,
const char *desc,
const char *level);

/* service connection */

Expand Down Expand Up @@ -53,7 +53,7 @@ struct ksc_ffi * ksc_ffi_start(const char *json_store_path,
struct ksc_ffi_data *c),
void (*on_open)(const struct ksc_ffi *),
void (*on_close)(intptr_t uuid, void *udata),
struct ksc_log *log,
struct ksc_ffi_log *log,
const char *server_cert_path,
int on_close_do_reconnect,
void *udata
Expand Down
39 changes: 35 additions & 4 deletions src/json-store.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ static void kjson_object_remove(struct kjson_object *obj, struct kjson_object_en
}

struct json_store {
REF_COUNTED;
struct kjson_value cfg;
int fd;
char *path;
Expand Down Expand Up @@ -258,14 +259,41 @@ bool json_store_load(struct json_store *js)
return r;
}

void json_store_destroy(struct json_store *js)
static void json_store_destroy(struct json_store *js)
{
json_value_fini(&js->cfg);
ksc_free(js->path);
close(js->fd); /* also releases lockf(3p) lock */
assert(!js->ref_counted.cnt);
if (!UNREF(js->log))
ksc_log_fini(js->log);
ksc_free(js);
}

void json_store_ref(struct json_store *js) { REF(js); }

void json_store_unref(struct json_store *js)
{
if (UNREF(js))
return;

int r = json_store_save(js);
LOG_(r ? KSC_LOG_ERROR : KSC_LOG_DEBUG,
"json_store_save returned %d\n", r);
if (!r) {
r = json_store_load(js);
LOG_(r ? KSC_LOG_DEBUG : KSC_LOG_ERROR,
"json_store_load returned %d\n", r);
r = !r;
}
if (!r) {
r = json_store_save(js);
LOG_(r ? KSC_LOG_ERROR : KSC_LOG_DEBUG,
"json_store_save returned %d\n", r);
}
json_store_destroy(js);
}

struct json_store * json_store_create(const char *path, struct ksc_log *log)
{
struct json_store *js = NULL;
Expand All @@ -283,6 +311,7 @@ struct json_store * json_store_create(const char *path, struct ksc_log *log)
LOGL(ERROR, log, "calloc: %s\n", strerror(errno));
goto fail;
}
REF(log);
js->fd = fd;
js->log = log;
js->path = strdup(path);
Expand All @@ -292,14 +321,16 @@ struct json_store * json_store_create(const char *path, struct ksc_log *log)
}
if (!json_store_load(js)) {
LOG(ERROR, "json_store_load: failed\n");
json_store_destroy(js);
js = NULL;
goto fail_1;
}
REF_INIT(js);
return js;

fail_1:
ksc_free(js);
json_store_destroy(js);
js = NULL;
fail:
ksc_free(js);
close(fd);
return NULL;
}
Expand Down
4 changes: 3 additions & 1 deletion src/json-store.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ struct json_store;
struct ksc_log;

struct json_store * json_store_create(const char *path, struct ksc_log *log);
void json_store_destroy(struct json_store *);
// void json_store_destroy(struct json_store *);
int json_store_save(struct json_store *);
bool json_store_load(struct json_store *);
void json_store_ref(struct json_store *);
void json_store_unref(struct json_store *);

const char * json_store_get_username(const struct json_store *);
bool json_store_get_device_id(const struct json_store *, int32_t *ret);
Expand Down
Loading

0 comments on commit ed0c905

Please sign in to comment.