summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2020-06-19 16:42:22 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2020-06-19 16:42:22 +0200
commit804679db852b13664ca351e0ee302cc70e96bf67 (patch)
tree1b13eefc5f8f49dd30d277ccf5974a50e037e610
parente70410eabf0115819624acd5ca1bd6fb24150887 (diff)
Recursively create a nodes subtree when being created as consequence of a move command.
-rw-r--r--src/message.h53
-rw-r--r--src/messagehandler.cc56
-rw-r--r--src/messageparser.cc17
3 files changed, 61 insertions, 65 deletions
diff --git a/src/message.h b/src/message.h
index d5e0590..3a6afdc 100644
--- a/src/message.h
+++ b/src/message.h
@@ -29,12 +29,12 @@
#include <list>
#include <string>
+#include <vector>
#include "node.h"
-namespace cmd
+enum class cmd
{
-typedef enum {
login,
logout,
subscribe,
@@ -45,58 +45,63 @@ typedef enum {
move,
update,
error,
-} cmd_t;
};
-typedef struct
+using cmd_t = cmd;
+
+struct login_t
{
std::string user;
std::string password;
-} login_t;
+};
-typedef struct
+struct logout_t
{
-} logout_t;
+};
-typedef struct
+struct subscribe_t
{
nodeid_t id;
-} subscribe_t;
+};
-typedef struct
+struct unsubscribe_t
{
nodeid_t id;
-} unsubscribe_t;
+};
-typedef struct
+struct create_t
{
nodeid_t id;
nodeid_t parentid;
nodeid_t insertbeforeid;
-} create_t;
-typedef struct
+ // for create_with_attributes
+ std::map<std::string, std::string> attributes;
+ std::vector<struct create_t> children;
+};
+
+struct remove_t
{
nodeid_t id;
-} remove_t;
+};
-typedef struct
+struct move_t
{
nodeid_t id;
nodeid_t parentid;
nodeid_t insertbeforeid;
-} move_t;
+};
-typedef struct
+struct update_t
{
nodeid_t id;
std::string attribute;
std::string value;
-} update_t;
+};
-typedef struct
+struct message_t
{
- cmd::cmd_t cmd;
+ cmd_t cmd;
login_t login;
logout_t logout;
@@ -107,13 +112,11 @@ typedef struct
move_t move;
update_t update;
- std::map<std::string, std::string> attributes; // for create_with_attributes
-
NodeIdList nodes;
// target node id (subscription node id) used for transmissions only.
nodeid_t tid;
-} message_t;
+};
-typedef std::list<message_t> MessageList;
+using MessageList = std::list<message_t>;
diff --git a/src/messagehandler.cc b/src/messagehandler.cc
index 6986d21..e4db5e7 100644
--- a/src/messagehandler.cc
+++ b/src/messagehandler.cc
@@ -118,50 +118,28 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi)
node_manager.moveNode(m.move.id, m.move.parentid, m.move.insertbeforeid);
node_t createnode = node_manager.node(m.move.id);
-// NodeIdList commonAncestors;
-// NodeIdList removeAncestors;
-// NodeIdList createAncestors;
-//
-// // find command ancestors and fill ancestors for remove command
-// for(NodeIdList::iterator it_remove = tilpair.first.begin();
-// it_remove != tilpair.first.end(); it_remove++)
-// {
-// nodeid_t removeid = *it;
-// bool common = false;
-// for(NodeIdList::iterator it_create = tilpair.second.begin();
-// it_create = != tilpair.second.end(); it_create++)
-// {
-// nodeid_t createid = *id;
-// if(removeid == createid)
-// {
-// commandAncestors.push_back(removeid);
-// common = true;
-// }
-// }
-// if(!common)
-// {
-// removeAncestors.push_back(removeid);
-// }
-// }
-//
-// // fill ancestors for create command
-// for(NodeIdList::iterator it_create = tilpair.second.begin();
-// it_create = != tilpair.second.end(); it_create++)
-// {
-// nodeid_t createid = *id;
-// if(removeid == createid)
-// {
-// commandAncestors.push_back(removeid);
-// common = true;
-// }
-// }
-
message_t removemsg = create_msg_remove(removenode);
removemsg.nodes = tilpair.first;
message_t createmsg =
create_msg_create_with_attributes(createnode,
m.move.insertbeforeid);
- createmsg.attributes = createnode.attributes;
+ createmsg.create.attributes = createnode.attributes;
+
+ for(const auto& child_node_id : node_manager.subNodes(m.move.id))
+ {
+ if(child_node_id == m.move.id)
+ {
+ continue;
+ }
+ auto child_node = node_manager.node(child_node_id);
+ create_t child;
+ child.id = child_node.id;
+ child.parentid = child_node.parentid;
+ child.insertbeforeid = -1;
+ child.attributes = child_node.attributes;
+ createmsg.create.children.push_back(child);
+ }
+
createmsg.nodes = tilpair.second;
outmsgs.push_back(removemsg);
diff --git a/src/messageparser.cc b/src/messageparser.cc
index 0f1844e..7393c5c 100644
--- a/src/messageparser.cc
+++ b/src/messageparser.cc
@@ -352,13 +352,28 @@ std::vector<std::string> msg_tostring(message_t m)
std::to_string(m.create.parentid) + " " +
std::to_string(m.create.insertbeforeid) + ";";
msgs.push_back(msg);
- for(const auto& attribute : m.attributes)
+ for(const auto& attribute : m.create.attributes)
{
msg = "update " +
std::to_string(m.create.id) + " \"" +
attribute.first + "\" \"" + attribute.second + "\";";
msgs.push_back(msg);
}
+ for(const auto &child : m.create.children)
+ {
+ msg = "create " +
+ std::to_string(child.id) + " " +
+ std::to_string(child.parentid) + " " +
+ std::to_string(child.insertbeforeid) + ";";
+ msgs.push_back(msg);
+ for(const auto& attribute : child.attributes)
+ {
+ msg = "update " +
+ std::to_string(child.id) + " \"" +
+ attribute.first + "\" \"" + attribute.second + "\";";
+ msgs.push_back(msg);
+ }
+ }
break;
case cmd::remove:
msg = "remove " + std::to_string(m.remove.id) + ";";