Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Q
qjs-wrapper
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nexedi
qjs-wrapper
Commits
75c4c3e1
Commit
75c4c3e1
authored
Mar 11, 2024
by
Léo-Paul Géneau
👾
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use a queue instead of a mutex to receive messages
parent
2a2d821d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
65 deletions
+75
-65
include/pubsub.h
include/pubsub.h
+11
-2
qjs_wrapper.c
qjs_wrapper.c
+64
-63
No files found.
include/pubsub.h
View file @
75c4c3e1
...
@@ -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
{
...
...
qjs_wrapper.c
View file @
75c4c3e1
...
@@ -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
m
essageQueue
=
{
static
StrQueue
sendM
essageQueue
=
{
.
head
=
NULL
,
.
head
=
NULL
,
.
tail
=
NULL
,
.
tail
=
NULL
,
};
};
UA_String
currentMessage
;
UA_String
current
Send
Message
;
static
StrQueue
l
ogQueue
=
{
static
StrQueue
sendL
ogQueue
=
{
.
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
,
q
ueue
);
addStrToQueue
(
str
,
pQ
ueue
);
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
],
&
m
essageQueue
);
return
add_jsstr_to_queue
(
ctx
,
argv
[
0
],
&
sendM
essageQueue
);
}
}
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
*
q
ueue
,
UA_String
currentStr
)
static
UA_String
get_StrQueue_content
(
StrQueue
*
pQ
ueue
,
UA_String
currentStr
)
{
{
struct
strNode
*
current
;
struct
strNode
*
current
;
current
=
q
ueue
->
head
;
current
=
pQ
ueue
->
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
?
(
q
ueue
->
tail
=
NULL
)
:
current
->
next
;
pQueue
->
head
=
current
->
next
==
NULL
?
(
pQ
ueue
->
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
,
current
Message
);
return
get_StrQueue_content
(
&
sendMessageQueue
,
currentSend
Message
);
}
}
UA_String
get_log
(
void
)
UA_String
get_log
(
void
)
{
{
return
get_StrQueue_content
(
&
logQueue
,
current
Log
);
return
get_StrQueue_content
(
&
sendLogQueue
,
currentSend
Log
);
}
}
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
(
""
);
current
Send
Message
=
UA_STRING
(
""
);
currentLog
=
UA_STRING
(
""
);
current
Send
Log
=
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
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment