From fe4231b1dbb41f9eced8005b2e46453a81f08fa9 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 9 Jun 2020 19:48:35 +0200 Subject: Make sure to send update messages for all create messages that was originally the result of a move message. --- src/message.h | 3 +++ src/messagehandler.cc | 6 ++++-- src/messageparser.cc | 39 +++++++++++++++++++++++++++++++++------ src/messageparser.h | 13 ++++++++++++- src/munia_proto.cc | 8 ++++++-- 5 files changed, 58 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/message.h b/src/message.h index 94b7165..d5e0590 100644 --- a/src/message.h +++ b/src/message.h @@ -40,6 +40,7 @@ typedef enum { subscribe, unsubscribe, create, + create_with_attributes, remove, move, update, @@ -106,6 +107,8 @@ typedef struct move_t move; update_t update; + std::map attributes; // for create_with_attributes + NodeIdList nodes; // target node id (subscription node id) used for transmissions only. diff --git a/src/messagehandler.cc b/src/messagehandler.cc index 2333838..6986d21 100644 --- a/src/messagehandler.cc +++ b/src/messagehandler.cc @@ -158,8 +158,10 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) message_t removemsg = create_msg_remove(removenode); removemsg.nodes = tilpair.first; - message_t createmsg = create_msg_create(createnode, - m.move.insertbeforeid); + message_t createmsg = + create_msg_create_with_attributes(createnode, + m.move.insertbeforeid); + createmsg.attributes = createnode.attributes; createmsg.nodes = tilpair.second; outmsgs.push_back(removemsg); diff --git a/src/messageparser.cc b/src/messageparser.cc index 1b5fd7f..0f1844e 100644 --- a/src/messageparser.cc +++ b/src/messageparser.cc @@ -332,8 +332,9 @@ MessageList parse_msg_client(std::string data) return msgList; } -std::string msg_tostring(message_t m) +std::vector msg_tostring(message_t m) { + std::vector msgs; std::string msg; switch(m.cmd) @@ -342,27 +343,45 @@ std::string msg_tostring(message_t m) msg = "create " + std::to_string(m.create.id) + " " + std::to_string(m.create.parentid) + " " + - std::to_string(m.create.insertbeforeid); + std::to_string(m.create.insertbeforeid) + ";"; + msgs.push_back(msg); + break; + case cmd::create_with_attributes: + msg = "create " + + std::to_string(m.create.id) + " " + + std::to_string(m.create.parentid) + " " + + std::to_string(m.create.insertbeforeid) + ";"; + msgs.push_back(msg); + for(const auto& attribute : m.attributes) + { + msg = "update " + + std::to_string(m.create.id) + " \"" + + attribute.first + "\" \"" + attribute.second + "\";"; + msgs.push_back(msg); + } break; case cmd::remove: - msg = "remove " + std::to_string(m.remove.id); + msg = "remove " + std::to_string(m.remove.id) + ";"; + msgs.push_back(msg); break; case cmd::move: msg = "move " + std::to_string(m.move.id) + " " + std::to_string(m.move.parentid) + " " + - std::to_string(m.move.insertbeforeid); + std::to_string(m.move.insertbeforeid) + ";"; + msgs.push_back(msg); break; case cmd::update: msg = "update " + std::to_string(m.update.id) + " \"" + - m.update.attribute + "\" \"" + m.update.value + "\""; + m.update.attribute + "\" \"" + m.update.value + "\";"; + msgs.push_back(msg); break; default: break; } - return msg + ";"; + return msgs; } message_t create_msg_create(node_t t, nodeid_t insertbeforeid) @@ -375,6 +394,14 @@ message_t create_msg_create(node_t t, nodeid_t insertbeforeid) return m; } +message_t create_msg_create_with_attributes(node_t node, + nodeid_t insertbeforeid) +{ + auto msg = create_msg_create(node, insertbeforeid); + msg.cmd = cmd::create_with_attributes; + return msg; +} + message_t create_msg_update(node_t t, const std::string &attr) { message_t m; diff --git a/src/messageparser.h b/src/messageparser.h index 85ce5cf..218f3a8 100644 --- a/src/messageparser.h +++ b/src/messageparser.h @@ -30,14 +30,25 @@ #include "message.h" #include +#include MessageList parse_msg(std::string msg); MessageList parse_msg_client(std::string msg); //message_t create_msg(cmd::cmd_t type, node_t node); -std::string msg_tostring(message_t msg); + +//! Create a (list of) string(s) from a single message. +//! Only the create_with_attributes message type produces more than one item in +//! the list, namely the create message and update messages for each attribute +//! in the node. +std::vector msg_tostring(message_t msg); message_t create_msg_create(node_t node, nodeid_t insertbeforeid); + +//! Same as create_msg_create, but with a different type. +message_t create_msg_create_with_attributes(node_t node, + nodeid_t insertbeforeid); + message_t create_msg_update(node_t node, const std::string &attr); message_t create_msg_remove(node_t node); message_t create_msg_move(nodeid_t id, nodeid_t to, nodeid_t insertbeforeid); diff --git a/src/munia_proto.cc b/src/munia_proto.cc index ef572e8..3349087 100644 --- a/src/munia_proto.cc +++ b/src/munia_proto.cc @@ -127,7 +127,7 @@ int callback_lws_node(struct lws *wsi, while(j != msgqueue[wsi].end()) { message_t m = *j; - if(m.cmd == cmd::create && // msg was a create msg? + if(m.cmd == cmd::create_with_attributes && // msg was a create msg? // created with same ids as we just removed? msg.remove.id == m.create.id && msg.tid == m.tid) @@ -166,7 +166,11 @@ int callback_lws_node(struct lws *wsi, { msgstr += " "; } - msgstr += std::to_string(msg.tid) + " " + msg_tostring(msg); + auto msgs = msg_tostring(msg); + for(const auto& msg_string : msgs) + { + msgstr += std::to_string(msg.tid) + " " + msg_string; + } } msgstr.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' '); -- cgit v1.2.3