Commit 91c13bb4 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

software/js-drone: group messages to serialize

Serialize the list of messages to send on a loop execution in a single string.
parent f3aaaaf4
...@@ -46,4 +46,4 @@ md5sum = 3bbb0f80b644d86784aab99b03e88c2f ...@@ -46,4 +46,4 @@ md5sum = 3bbb0f80b644d86784aab99b03e88c2f
[worker] [worker]
_update_hash_filename_ = drone-scripts/worker.js.jinja2 _update_hash_filename_ = drone-scripts/worker.js.jinja2
md5sum = f3ad1e74bdb977c9444713f14a2ea6fe md5sum = deda0da2b39c256796d1fce021963505
...@@ -74,6 +74,7 @@ import { evalScript, fdopen, loadFile, open } from "std"; ...@@ -74,6 +74,7 @@ import { evalScript, fdopen, loadFile, open } from "std";
last_message_timestamp_list = [], last_message_timestamp_list = [],
parent = Worker.parent, parent = Worker.parent,
peer_dict = {}, peer_dict = {},
to_send_list = [],
user_me = { user_me = {
exit: exitWorker, exit: exitWorker,
getDroneDict: function () { return drone_dict; }, getDroneDict: function () { return drone_dict; },
...@@ -110,11 +111,7 @@ import { evalScript, fdopen, loadFile, open } from "std"; ...@@ -110,11 +111,7 @@ import { evalScript, fdopen, loadFile, open } from "std";
function sendMsg(msg, id) { function sendMsg(msg, id) {
if (id === undefined) { id = -1; } if (id === undefined) { id = -1; }
setMessage(JSON.stringify({ to_send_list.push({ content: msg, dest_id: id });
content: msg,
timestamp: Date.now(),
dest_id: id
}));
} }
function exitWorker(exit_code) { function exitWorker(exit_code) {
...@@ -252,7 +249,8 @@ import { evalScript, fdopen, loadFile, open } from "std"; ...@@ -252,7 +249,8 @@ import { evalScript, fdopen, loadFile, open } from "std";
} }
function handleMainMessage(evt) { function handleMainMessage(evt) {
var type = evt.data.type, message, parsed_message, peer_id, log; var type = evt.data.type, parsed_message, peer_id, peer_message,
log, res;
switch (type) { switch (type) {
...@@ -280,19 +278,21 @@ import { evalScript, fdopen, loadFile, open } from "std"; ...@@ -280,19 +278,21 @@ import { evalScript, fdopen, loadFile, open } from "std";
case "update": case "update":
Object.entries(peer_dict).forEach(function ([id, peer]) { Object.entries(peer_dict).forEach(function ([id, peer]) {
message = peer.message; peer_message = peer.message;
if (message.length > 0) { if (peer_message.length > 0) {
parsed_message = JSON.parse(message); parsed_message = JSON.parse(peer_message);
while (parsed_message.timestamp !== last_message_timestamp_list[id]) { while (parsed_message.timestamp !== last_message_timestamp_list[id]) {
if (user_me.hasOwnProperty("onGetMsg") parsed_message.message_list.forEach(function(message) {
&& [-1, user_me.id].includes(parsed_message.dest_id)) { if (user_me.hasOwnProperty("onGetMsg")
user_me.onGetMsg(parsed_message.content); && [-1, user_me.id].includes(message.dest_id)) {
} user_me.onGetMsg(message.content);
}
});
last_message_timestamp_list[id] = parsed_message.timestamp; last_message_timestamp_list[id] = parsed_message.timestamp;
message = peer.message; peer_message = peer.message;
if (message.length > 0) { if (peer_message.length > 0) {
parsed_message = JSON.parse(message); parsed_message = JSON.parse(peer_message);
} }
} }
} }
...@@ -303,6 +303,12 @@ import { evalScript, fdopen, loadFile, open } from "std"; ...@@ -303,6 +303,12 @@ import { evalScript, fdopen, loadFile, open } from "std";
user_me.onUpdate(evt.data.timestamp); user_me.onUpdate(evt.data.timestamp);
} }
setMessage(JSON.stringify({
message_list: to_send_list,
timestamp: Date.now()
}));
to_send_list = [];
if (evt.data.timestamp - last_log_timestamp >= 1000) { if (evt.data.timestamp - last_log_timestamp >= 1000) {
{% if isADrone -%} {% if isADrone -%}
updateLogAndProjection(); updateLogAndProjection();
......
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