Commit 75c4c3e1 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Use a queue instead of a mutex to receive messages

parent 2a2d821d
......@@ -17,6 +17,15 @@
#define MAX_STR_SIZE 1024
struct strNode {
char *str;
struct strNode *next;
};
typedef struct {
struct strNode *head;
struct strNode *tail;
} StrQueue;
typedef struct {
UA_UInt16 id;
UA_Double latitude;
......@@ -27,8 +36,8 @@ typedef struct {
UA_Float yaw;
UA_Float speed;
UA_Float climbRate;
char message[MAX_STR_SIZE];
char log[MAX_STR_SIZE];
StrQueue receiveMessageQueue;
StrQueue receiveLogQueue;
} JSDroneData;
typedef struct {
......
......@@ -9,14 +9,6 @@
#define DRONE_VARIABLE_NB 4
#define SUBSCRIBER_VARIABLE_NB 1
struct strNode {
char *str;
struct strNode *next;
};
typedef struct {
struct strNode *head;
struct strNode *tail;
} StrQueue;
UA_Int64 positionArray[POSITION_ARRAY_SIZE] = { 0 };
UA_UInt32 positionArrayDims[] = {POSITION_ARRAY_SIZE};
......@@ -115,19 +107,16 @@ static UA_Boolean pubsubExited = true;
static UA_UInt32 nbDrone;
static UA_UInt32 nbSubscriber;
static JSValueConst *droneObjectIdList;
static StrQueue messageQueue = {
static StrQueue sendMessageQueue = {
.head = NULL,
.tail = NULL,
};
UA_String currentMessage;
static StrQueue logQueue = {
UA_String currentSendMessage;
static StrQueue sendLogQueue = {
.head = NULL,
.tail = NULL,
};
UA_String currentLog;
pthread_mutex_t mutex;
pthread_cond_t threadCond;
UA_String currentSendLog;
bool isADrone;
......@@ -201,9 +190,27 @@ static const JSCFunctionListEntry js_position_proto_funcs[] = {
// Drone class functions
static void delete_str_node(struct strNode *node) {
free(node->str);
free(node);
}
static void cleanQueue(StrQueue *pQueue)
{
struct strNode *current;
while (pQueue->head != NULL) {
current = pQueue->head;
pQueue->head = current->next;
delete_str_node(current);
}
}
static void js_drone_finalizer(JSRuntime *rt, JSValue val)
{
JSDroneData *s = (JSDroneData *) JS_GetOpaque(val, jsDroneClassId);
cleanQueue(&(s->receiveMessageQueue));
cleanQueue(&(s->receiveLogQueue));
js_free_rt(rt, s);
}
......@@ -221,6 +228,14 @@ static JSValue js_drone_ctor(JSContext *ctx, JSValueConst newTarget,
if (JS_ToUint32(ctx, &uint32, argv[0]))
goto fail;
s->id = (uint16_t)uint32;
s->receiveMessageQueue = (StrQueue){
.head = NULL,
.tail = NULL,
};
s->receiveLogQueue = (StrQueue){
.head = NULL,
.tail = NULL,
};
proto = JS_GetPropertyStr(ctx, newTarget, "prototype");
if (JS_IsException(proto))
......@@ -250,15 +265,19 @@ static JSValue js_drone_init(JSContext *ctx, JSValueConst thisVal,
return JS_UNDEFINED;
}
static JSValue readDroneDataStr(JSContext *ctx, char *str)
static JSValue readDroneDataStr(JSContext *ctx, StrQueue *pQueue)
{
JSValue res;
struct strNode *current;
pthread_mutex_lock(&mutex);
res = JS_NewString(ctx, str);
strcpy(str, "");
pthread_cond_signal(&threadCond);
pthread_mutex_unlock(&mutex);
current = pQueue->head;
if (current != NULL) {
res = JS_NewString(ctx, current->str);
pQueue->head = current->next == NULL ? (pQueue->tail = NULL) : current->next;
delete_str_node(current);
} else {
res = JS_NewString(ctx, "");
}
return res;
}
......@@ -285,9 +304,9 @@ static JSValue js_drone_get(JSContext *ctx, JSValueConst thisVal, int magic)
case 7:
return JS_NewFloat64(ctx, s->climbRate);
case 8:
return readDroneDataStr(ctx, s->message);
return readDroneDataStr(ctx, &(s->receiveMessageQueue));
case 9:
return readDroneDataStr(ctx, s->log);
return readDroneDataStr(ctx, &(s->receiveLogQueue));
case 10:
return JS_NewInt64(ctx, s->timestamp);
default:
......@@ -295,7 +314,7 @@ static JSValue js_drone_get(JSContext *ctx, JSValueConst thisVal, int magic)
}
}
static void addStrToQueue(const char* str, StrQueue* pQueue)
static void addStrToQueue(const char *str, StrQueue *pQueue)
{
struct strNode *newNode;
newNode = (struct strNode*)malloc(sizeof(struct strNode));
......@@ -310,7 +329,8 @@ static void addStrToQueue(const char* str, StrQueue* pQueue)
}
}
static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *queue)
static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr,
StrQueue *pQueue)
{
const char *str;
str = JS_ToCString(ctx, jsStr);
......@@ -319,7 +339,7 @@ static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *
return JS_EXCEPTION;
}
addStrToQueue(str, queue);
addStrToQueue(str, pQueue);
JS_FreeCString(ctx, str);
return JS_UNDEFINED;
}
......@@ -327,18 +347,13 @@ static JSValue add_jsstr_to_queue(JSContext *ctx, JSValueConst jsStr, StrQueue *
static JSValue js_drone_set_message(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return add_jsstr_to_queue(ctx, argv[0], &messageQueue);
return add_jsstr_to_queue(ctx, argv[0], &sendMessageQueue);
}
static JSValue js_drone_set_log(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
return add_jsstr_to_queue(ctx, argv[0], &logQueue);
}
static void delete_str_node(struct strNode *node) {
free(node->str);
free(node);
return add_jsstr_to_queue(ctx, argv[0], &sendLogQueue);
}
static UA_Boolean UA_String_isEmpty(const UA_String *s) {
......@@ -350,15 +365,15 @@ static void clear_str(UA_String *pstr) {
UA_String_clear(pstr);
}
static UA_String get_StrQueue_content(StrQueue *queue, UA_String currentStr)
static UA_String get_StrQueue_content(StrQueue *pQueue, UA_String currentStr)
{
struct strNode *current;
current = queue->head;
current = pQueue->head;
if (current != NULL) {
clear_str(&currentStr);
currentStr = UA_STRING_ALLOC(current->str);
queue->head = current->next == NULL ? (queue->tail = NULL) : current->next;
pQueue->head = current->next == NULL ? (pQueue->tail = NULL) : current->next;
delete_str_node(current);
}
return currentStr;
......@@ -366,12 +381,12 @@ static UA_String get_StrQueue_content(StrQueue *queue, UA_String currentStr)
UA_String get_message(void)
{
return get_StrQueue_content(&messageQueue, currentMessage);
return get_StrQueue_content(&sendMessageQueue, currentSendMessage);
}
UA_String get_log(void)
{
return get_StrQueue_content(&logQueue, currentLog);
return get_StrQueue_content(&sendLogQueue, currentSendLog);
}
static JSClassDef jsDroneClass = {
......@@ -460,17 +475,13 @@ VariableData pubsub_get_value(UA_String identifier) {
return varDetails;
}
static void setDroneDataStr(void *data, char *str)
static void setDroneDataStr(void *data, StrQueue* pQueue)
{
UA_String uaStr = *(UA_String*) data;
pthread_mutex_lock(&mutex);
while(strlen(str) != 0)
pthread_cond_wait(&threadCond, &mutex);
char str[MAX_STR_SIZE];
memcpy(str, uaStr.data, uaStr.length);
str[uaStr.length] = '\0';
pthread_mutex_unlock(&mutex);
addStrToQueue(str, pQueue);
}
static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool print)
......@@ -508,11 +519,11 @@ static void pubsub_update_variables(UA_UInt32 id, const UA_DataValue *var, bool
}
return;
} else if (pubsubIdsArray[i].message == id) {
setDroneDataStr(var->value.data, s->message);
setDroneDataStr(var->value.data, &(s->receiveMessageQueue));
return;
} else if (pubsubIdsArray[i].log == id) {
if (!isADrone) {
setDroneDataStr(var->value.data, s->log);
setDroneDataStr(var->value.data, &(s->receiveLogQueue));
}
return;
}
......@@ -610,24 +621,12 @@ static JSValue js_init_pubsub(JSContext *ctx, JSValueConst thisVal,
if (JS_ToUint32(ctx, &nbSubscriber, argv[1]))
return JS_EXCEPTION;
currentMessage = UA_STRING("");
currentLog = UA_STRING("");
currentSendMessage = UA_STRING("");
currentSendLog = UA_STRING("");
droneObjectIdList = (JSValue *) malloc((nbDrone + nbSubscriber) * sizeof(JSValueConst));
return JS_NewInt32(ctx, 0);
}
static void cleanQueue(StrQueue queue, UA_String current_str)
{
struct strNode *current;
while (queue.head != NULL) {
current = queue.head;
queue.head = current->next;
delete_str_node(current);
}
clear_str(&current_str);
}
static JSValue js_stop_pubsub(JSContext *ctx, JSValueConst thisVal,
int argc, JSValueConst *argv)
{
......@@ -636,8 +635,10 @@ static JSValue js_stop_pubsub(JSContext *ctx, JSValueConst thisVal,
sleep(1);
free(droneObjectIdList);
cleanQueue(messageQueue, currentMessage);
cleanQueue(logQueue, currentLog);
cleanQueue(&sendMessageQueue);
clear_str(&currentSendMessage);
cleanQueue(&sendLogQueue);
clear_str(&currentSendLog);
return JS_NewInt32(ctx, 0);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment