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