summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJose Bollo <jose.bollo@iot.bzh>2019-11-20 12:03:08 +0100
committerJosé Bollo <jose.bollo@iot.bzh>2019-11-29 12:48:17 +0100
commiteaf5670e9d10b5d7c066043e7563706cf1e01bd5 (patch)
tree07d05b5b14e61cea5f4754515cf2ea9cfb6f4cae
parent0fd887b7ab896e47b2f7ffe78f41738f3824a962 (diff)
afb-evt: Use 16 bits for ids
16 bits are enough Bug-AGL: SPEC-2968 Change-Id: I0e8708c1d6a3934a342721a6ce5edb4676df6d1b Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/afb-api-dbus.c12
-rw-r--r--src/afb-evt.c40
-rw-r--r--src/afb-evt.h11
-rw-r--r--src/afb-export.c4
-rw-r--r--src/afb-stub-ws.c6
-rw-r--r--src/afb-ws-json1.c4
6 files changed, 42 insertions, 35 deletions
diff --git a/src/afb-api-dbus.c b/src/afb-api-dbus.c
index 76894caa..0ac4f499 100644
--- a/src/afb-api-dbus.c
+++ b/src/afb-api-dbus.c
@@ -636,9 +636,9 @@ error:
/******************* event structures for server part **********************************/
-static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid);
-static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid);
-static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object);
+static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid);
+static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid);
+static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t eventid, struct json_object *object);
static void afb_api_dbus_server_event_broadcast(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
/* the interface for events broadcasting */
@@ -904,17 +904,17 @@ end:
sd_bus_message_unref(msg);
}
-static void afb_api_dbus_server_event_add(void *closure, const char *event, int eventid)
+static void afb_api_dbus_server_event_add(void *closure, const char *event, uint16_t eventid)
{
afb_api_dbus_server_event_send(closure, '+', event, eventid, "", 0);
}
-static void afb_api_dbus_server_event_remove(void *closure, const char *event, int eventid)
+static void afb_api_dbus_server_event_remove(void *closure, const char *event, uint16_t eventid)
{
afb_api_dbus_server_event_send(closure, '-', event, eventid, "", 0);
}
-static void afb_api_dbus_server_event_push(void *closure, const char *event, int eventid, struct json_object *object)
+static void afb_api_dbus_server_event_push(void *closure, const char *event, uint16_t eventid, struct json_object *object)
{
const char *data = json_object_to_json_string_ext(object, JSON_C_TO_STRING_PLAIN|JSON_C_TO_STRING_NOSLASHESCAPE);
afb_api_dbus_server_event_send(closure, '!', event, eventid, data, 0);
diff --git a/src/afb-evt.c b/src/afb-evt.c
index f54f34e1..c0e43200 100644
--- a/src/afb-evt.c
+++ b/src/afb-evt.c
@@ -56,7 +56,7 @@ struct afb_evt_listener {
pthread_rwlock_t rwlock;
/* count of reference to the listener */
- int refcount;
+ uint16_t refcount;
};
/*
@@ -82,10 +82,10 @@ struct afb_evtid {
#endif
/* refcount */
- int refcount;
+ uint16_t refcount;
/* id of the event */
- int id;
+ uint16_t id;
/* has client? */
int has_client;
@@ -176,8 +176,8 @@ static struct afb_evt_listener *listeners = NULL;
/* handling id of events */
static pthread_rwlock_t events_rwlock = PTHREAD_RWLOCK_INITIALIZER;
static struct afb_evtid *evtids = NULL;
-static int event_id_counter = 0;
-static int event_id_wrapped = 0;
+static uint16_t event_genid = 0;
+static uint16_t event_count = 0;
/* head of uniqueness of events */
#if !defined(EVENT_BROADCAST_HOP_MAX)
@@ -545,6 +545,7 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname)
{
size_t len;
struct afb_evtid *evtid, *oevt;
+ uint16_t id;
/* allocates the event */
len = strlen(fullname);
@@ -554,15 +555,20 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname)
/* allocates the id */
pthread_rwlock_wrlock(&events_rwlock);
+ if (event_count == UINT16_MAX) {
+ pthread_rwlock_unlock(&events_rwlock);
+ free(evtid);
+ ERROR("Can't create more events");
+ return NULL;
+ }
+ event_count++;
do {
- if (++event_id_counter < 0) {
- event_id_wrapped = 1;
- event_id_counter = 1024; /* heuristic: small numbers are not destroyed */
- }
- if (!event_id_wrapped)
- break;
+ /* TODO add a guard (counting number of event created) */
+ id = ++event_genid;
+ if (!id)
+ id = event_genid = 1;
oevt = evtids;
- while(oevt != NULL && oevt->id != event_id_counter)
+ while(oevt != NULL && oevt->id != id)
oevt = oevt->next;
} while (oevt != NULL);
@@ -571,7 +577,7 @@ struct afb_evtid *afb_evt_evtid_create(const char *fullname)
evtid->next = evtids;
evtid->refcount = 1;
evtid->watchs = NULL;
- evtid->id = event_id_counter;
+ evtid->id = id;
evtid->has_client = 0;
pthread_rwlock_init(&evtid->rwlock, NULL);
evtids = evtid;
@@ -649,8 +655,10 @@ void afb_evt_evtid_unref(struct afb_evtid *evtid)
prv = &evtids;
while (*prv && !(found = (*prv == evtid)))
prv = &(*prv)->next;
- if (found)
+ if (found) {
*prv = evtid->next;
+ event_count--;
+ }
pthread_rwlock_unlock(&events_rwlock);
/* destroys the event */
@@ -720,7 +728,7 @@ const char *afb_evt_evtid_hooked_name(struct afb_evtid *evtid)
/*
* Returns the id of the 'event'
*/
-int afb_evt_evtid_id(struct afb_evtid *evtid)
+uint16_t afb_evt_evtid_id(struct afb_evtid *evtid)
{
return evtid->id;
}
@@ -943,7 +951,7 @@ const char *afb_evt_event_x2_fullname(struct afb_event_x2 *eventid)
/*
* Returns the id of the 'eventid'
*/
-int afb_evt_event_x2_id(struct afb_event_x2 *eventid)
+uint16_t afb_evt_event_x2_id(struct afb_event_x2 *eventid)
{
struct afb_evtid *evtid = afb_evt_event_x2_to_evtid(eventid);
return evtid ? evtid->id : 0;
diff --git a/src/afb-evt.h b/src/afb-evt.h
index 3e37df53..6e0297f6 100644
--- a/src/afb-evt.h
+++ b/src/afb-evt.h
@@ -28,10 +28,10 @@ struct afb_evt_listener;
struct afb_evt_itf
{
- void (*push)(void *closure, const char *event, int evtid, struct json_object *object);
+ void (*push)(void *closure, const char *event, uint16_t evtid, struct json_object *object);
void (*broadcast)(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
- void (*add)(void *closure, const char *event, int evtid);
- void (*remove)(void *closure, const char *event, int evtid);
+ void (*add)(void *closure, const char *event, uint16_t evtid);
+ void (*remove)(void *closure, const char *event, uint16_t evtid);
};
extern struct afb_evt_listener *afb_evt_listener_create(const struct afb_evt_itf *itf, void *closure);
@@ -50,7 +50,7 @@ extern struct afb_evtid *afb_evt_evtid_addref(struct afb_evtid *evtid);
extern void afb_evt_evtid_unref(struct afb_evtid *evtid);
extern const char *afb_evt_evtid_fullname(struct afb_evtid *evtid);
-extern int afb_evt_evtid_id(struct afb_evtid *evtid);
+extern uint16_t afb_evt_evtid_id(struct afb_evtid *evtid);
extern const char *afb_evt_evtid_name(struct afb_evtid *evtid);
@@ -62,11 +62,10 @@ extern int afb_evt_watch_add_evtid(struct afb_evt_listener *listener, struct afb
extern int afb_evt_watch_sub_evtid(struct afb_evt_listener *listener, struct afb_evtid *evtid);
-
extern struct afb_event_x2 *afb_evt_event_x2_create(const char *fullname);
extern struct afb_event_x2 *afb_evt_event_x2_create2(const char *prefix, const char *name);
extern const char *afb_evt_event_x2_fullname(struct afb_event_x2 *event);
-extern int afb_evt_event_x2_id(struct afb_event_x2 *eventid);
+extern uint16_t afb_evt_event_x2_id(struct afb_event_x2 *eventid);
extern struct afb_event_x2 *afb_evt_event_x2_addref(struct afb_event_x2 *eventid);
extern void afb_evt_event_x2_unref(struct afb_event_x2 *eventid);
diff --git a/src/afb-export.c b/src/afb-export.c
index 3cf1fc94..a9643f54 100644
--- a/src/afb-export.c
+++ b/src/afb-export.c
@@ -1206,7 +1206,7 @@ static const struct afb_api_x3_itf hooked_api_x3_itf = {
/*
* Propagates the event to the service
*/
-static void listener_of_events(void *closure, const char *event, int eventid, struct json_object *object)
+static void listener_of_events(void *closure, const char *event, uint16_t eventid, struct json_object *object)
{
const struct globset_handler *handler;
void (*callback)(void *, const char*, struct json_object*, struct afb_api_x3*);
@@ -1250,7 +1250,7 @@ static void listener_of_events(void *closure, const char *event, int eventid, st
json_object_put(object);
}
-static void listener_of_pushed_events(void *closure, const char *event, int eventid, struct json_object *object)
+static void listener_of_pushed_events(void *closure, const char *event, uint16_t eventid, struct json_object *object)
{
listener_of_events(closure, event, eventid, object);
}
diff --git a/src/afb-stub-ws.c b/src/afb-stub-ws.c
index 40addd0b..2a48745b 100644
--- a/src/afb-stub-ws.c
+++ b/src/afb-stub-ws.c
@@ -316,7 +316,7 @@ static struct json_object *client_api_describe_cb(void * closure)
/******************* server part: manage events **********************************/
-static void server_event_add_cb(void *closure, const char *event, int eventid)
+static void server_event_add_cb(void *closure, const char *event, uint16_t eventid)
{
struct afb_stub_ws *stubws = closure;
@@ -324,7 +324,7 @@ static void server_event_add_cb(void *closure, const char *event, int eventid)
afb_proto_ws_server_event_create(stubws->proto, event, eventid);
}
-static void server_event_remove_cb(void *closure, const char *event, int eventid)
+static void server_event_remove_cb(void *closure, const char *event, uint16_t eventid)
{
struct afb_stub_ws *stubws = closure;
@@ -332,7 +332,7 @@ static void server_event_remove_cb(void *closure, const char *event, int eventid
afb_proto_ws_server_event_remove(stubws->proto, event, eventid);
}
-static void server_event_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
+static void server_event_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object)
{
struct afb_stub_ws *stubws = closure;
diff --git a/src/afb-ws-json1.c b/src/afb-ws-json1.c
index a8461707..4f7991b6 100644
--- a/src/afb-ws-json1.c
+++ b/src/afb-ws-json1.c
@@ -47,7 +47,7 @@ struct afb_wsreq;
/* predeclaration of websocket callbacks */
static void aws_on_hangup_cb(void *closure, struct afb_wsj1 *wsj1);
static void aws_on_call_cb(void *closure, const char *api, const char *verb, struct afb_wsj1_msg *msg);
-static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object);
+static void aws_on_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object);
static void aws_on_broadcast_cb(void *closure, const char *event, struct json_object *object, const uuid_binary_t uuid, uint8_t hop);
/* predeclaration of wsreq callbacks */
@@ -235,7 +235,7 @@ static void aws_on_event(struct afb_ws_json1 *aws, const char *event, struct jso
afb_wsj1_send_event_j(aws->wsj1, event, afb_msg_json_event(event, object));
}
-static void aws_on_push_cb(void *closure, const char *event, int eventid, struct json_object *object)
+static void aws_on_push_cb(void *closure, const char *event, uint16_t eventid, struct json_object *object)
{
aws_on_event(closure, event, object);
}