diff options
author | Jonas Suhr Christensen <jsc@umbraculum.org> | 2012-05-17 12:10:57 +0200 |
---|---|---|
committer | Jonas Suhr Christensen <jsc@umbraculum.org> | 2012-05-17 12:10:57 +0200 |
commit | 69f1a4dfc0d3a7a1462d87a31b11197a638c5746 (patch) | |
tree | eb4be65262797896b928ab01bd5d1b12644cbe1b /src | |
parent | 2b542db3b553e73bdd58f54f657776715fd5472d (diff) | |
parent | c2def65a701a973cde305d8c221d01beeb33eb84 (diff) |
Merge branch 'master' of https://git.oftal.dk/munia
Conflicts:
src/taskmanager.cc
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/messagehandler.cc | 47 | ||||
-rw-r--r-- | src/messageparser.cc | 8 | ||||
-rw-r--r-- | src/messageparser.h | 1 | ||||
-rw-r--r-- | src/munia_proto.cc | 53 | ||||
-rw-r--r-- | src/muniacli.cc | 2 | ||||
-rw-r--r-- | src/taskmanager.cc | 19 | ||||
-rw-r--r-- | src/taskmanager.h | 5 | ||||
-rw-r--r-- | src/tasktree.cc | 3 | ||||
-rw-r--r-- | src/tasktree.h | 2 |
10 files changed, 122 insertions, 20 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 01ef438..0e59695 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,7 +27,7 @@ muniacli_LDADD = $(LIBWEBSOCKETS_LIBS) muniacli_CXXFLAGS = $(LIBWEBSOCKETS_CFLAGS) -muniad_SOURCES = \ +muniacli_SOURCES = \ muniacli.cc EXTRA_DIST = \ diff --git a/src/messagehandler.cc b/src/messagehandler.cc index cdc7246..a394f08 100644 --- a/src/messagehandler.cc +++ b/src/messagehandler.cc @@ -86,8 +86,51 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) { { INFO(messagehandler, "Handling move command\n"); try { - m.nodes = task_manager.moveTask(m.move.id, m.move.parentid); - outmsgs.push_back(m); + task_t removetask = task_manager.task(m.move.id); + TaskIdListPair tilpair = task_manager.moveTask(m.move.id, m.move.parentid); + task_t createtask = task_manager.task(m.move.id); + + /* + TaskIdList commonAncestors; + TaskIdList removeAncestors; + TaskIdList createAncestors; + + // find command ancestors and fill ancestors for remove command + for(TaskIdList::iterator it_remove = tilpair.first.begin(); + it_remove != tilpair.first.end(); it_remove++) { + taskid_t removeid = *it; + bool common = false; + for(TaskIdList::iterator it_create = tilpair.second.begin(); + it_create = != tilpair.second.end(); it_create++) { + taskid_t createid = *id; + if(removeid == createid) { + commandAncestors.push_back(removeid); + common = true; + } + } + if(!common) { + removeAncestors.push_back(removeid); + } + } + + // fill ancestors for create command + for(TaskIdList::iterator it_create = tilpair.second.begin(); + it_create = != tilpair.second.end(); it_create++) { + taskid_t createid = *id; + if(removeid == createid) { + commandAncestors.push_back(removeid); + common = true; + } + } + */ + + message_t removemsg = create_msg_remove(removetask); + removemsg.nodes = tilpair.first; + message_t createmsg = create_msg_create(createtask); + createmsg.nodes = tilpair.second; + + outmsgs.push_back(removemsg); + outmsgs.push_back(createmsg); } catch (std::exception& e) { DEBUG(messagehandler, "Error moving task\n"); diff --git a/src/messageparser.cc b/src/messageparser.cc index 804eb12..62923be 100644 --- a/src/messageparser.cc +++ b/src/messageparser.cc @@ -245,6 +245,14 @@ message_t create_msg_remove(task_t t) { return m; } +message_t create_msg_move(taskid_t id, taskid_t to) { + message_t m; + m.cmd = cmd::move; + m.move.id = id; + m.move.parentid = to; + return m; +} + #ifdef TEST_MSGPARSER //Additional dependency files //deps: diff --git a/src/messageparser.h b/src/messageparser.h index 6ebac3e..11b11cb 100644 --- a/src/messageparser.h +++ b/src/messageparser.h @@ -38,5 +38,6 @@ std::string msg_tostring(message_t msg); message_t create_msg_create(task_t task); message_t create_msg_update(task_t task); message_t create_msg_remove(task_t task); +message_t create_msg_move(taskid_t id, taskid_t to); #endif/*__MUNIA_MESSAGEPARSER_H__*/ diff --git a/src/munia_proto.cc b/src/munia_proto.cc index d9b068f..3022042 100644 --- a/src/munia_proto.cc +++ b/src/munia_proto.cc @@ -147,30 +147,62 @@ int callback_lws_task(struct libwebsocket_context * context, std::string msgstr; msgstr.append((size_t)LWS_SEND_BUFFER_PRE_PADDING, ' '); + int c = 0; + message_t prevmsg; + std::queue<message_t> messages; while(msgqueue[wsi].size() > 0) { message_t msg = msgqueue[wsi].front(); msgqueue[wsi].pop(); + + //Rewriting delete id followed by create id to move to_id from_id + // look at previous message from create commands + if(c > 0 && // look backward (skip first entry) + msg.cmd == cmd::create && // look from create commands + prevmsg.cmd == cmd::remove && // if previous msg is a remove command + msg.create.id == prevmsg.remove.id) { // and commands have same id + + printf("\tprevmsg: %s, msg: %s\n", msg_tostring(prevmsg).c_str(), + msg_tostring(msg).c_str()); + message_t movemsg = create_msg_move(msg.create.id, msg.create.parentid); + msg = movemsg; + } + else if(c > 0) { + messages.push(prevmsg); + } + + prevmsg = msg; + c++; + } + if(c > 0) { // Push last msg + messages.push(prevmsg); + } + + // create msg string + // while(msgqueue[wsi].size() > 0) { + while(messages.size() > 0) { + message_t msg = messages.front(); + messages.pop(); char buf[32]; sprintf(buf, "%d", msg.tid); + printf("msg: %s\n", msg_tostring(msg).c_str()); if(msgstr.size() > LWS_SEND_BUFFER_PRE_PADDING) msgstr += " "; msgstr += std::string(buf) + " " + msg_tostring(msg); } msgstr.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' '); - + int n = libwebsocket_write(wsi, (unsigned char *) - msgstr.c_str() + - LWS_SEND_BUFFER_PRE_PADDING, - msgstr.length() - - LWS_SEND_BUFFER_POST_PADDING - - LWS_SEND_BUFFER_PRE_PADDING, - LWS_WRITE_TEXT); + msgstr.c_str() + + LWS_SEND_BUFFER_PRE_PADDING, + msgstr.length() - + LWS_SEND_BUFFER_POST_PADDING - + LWS_SEND_BUFFER_PRE_PADDING, + LWS_WRITE_TEXT); if(n < 0) { fprintf(stderr, "ERROR writing to socket"); exit(1); - } - } - + } } + break; /* @@ -228,7 +260,6 @@ int callback_lws_task(struct libwebsocket_context * context, id++; } - } else { printf("%d nodes affected by command\n", omi->nodes.size()); diff --git a/src/muniacli.cc b/src/muniacli.cc index 3620a85..a84de65 100644 --- a/src/muniacli.cc +++ b/src/muniacli.cc @@ -34,7 +34,7 @@ #include <libwebsockets.h> enum demo_protocols { - PROTOCOL_TASK, + PROTOCOL_TASK }; static int callback_task(struct libwebsocket_context *me, diff --git a/src/taskmanager.cc b/src/taskmanager.cc index 60b01d6..89de0b3 100644 --- a/src/taskmanager.cc +++ b/src/taskmanager.cc @@ -39,6 +39,11 @@ TaskManager task_manager; #define PROJECTS_ID 4 #define FIRST_TASK_ID 10 +static bool isProtected(taskid_t id) +{ + return id < FIRST_TASK_ID; +} + TaskManager::TaskManager() { idCount = FIRST_TASK_ID; @@ -80,7 +85,10 @@ taskid_t TaskManager::createId() { TaskIdListPair TaskManager::moveTask(taskid_t id, taskid_t to) throw (std::exception) { + if(isProtected(id)) return TaskIdListPair(); + /* + TaskIdList affectedTasks; try { @@ -91,11 +99,11 @@ TaskIdListPair TaskManager::moveTask(taskid_t id, taskid_t to) } */ - task_t t = data(id); + task_t t = tree.data(id); t.parentid = to; - TaskIdListRemove tilremove = tree.remove(id); - TaskIdListCreate tilcreate = tree.create(to, id, t); + TaskIdList tilremove = tree.remove(id); + TaskIdList tilcreate = tree.insertAsChild(to, id, t); TaskIdListPair tilpair; tilpair.first = tilremove; @@ -106,6 +114,9 @@ TaskIdListPair TaskManager::moveTask(taskid_t id, taskid_t to) TaskIdList TaskManager::removeTask(taskid_t id) throw (std::exception) { + + if(isProtected(id)) return TaskIdList(); + TaskIdList affectedTasks; if(tree.bfs(id).size() > 1) throw std::exception(); @@ -123,6 +134,8 @@ TaskIdList TaskManager::removeTask(taskid_t id) TaskIdList TaskManager::updateTask(taskid_t id, task_t t) throw (std::exception) { + if(isProtected(id)) return TaskIdList(); + TaskIdList affectedTasks; try { diff --git a/src/taskmanager.h b/src/taskmanager.h index dc0cf1c..0f0d96e 100644 --- a/src/taskmanager.h +++ b/src/taskmanager.h @@ -36,6 +36,8 @@ #include "task.h" #include "tasktree.h" +typedef std::pair<TaskIdList, TaskIdList> TaskIdListPair; + class TaskManager { public: TaskManager(); @@ -44,7 +46,8 @@ public: TaskIdList createTask(taskid_t parentid, taskid_t *id) throw (std::exception); TaskIdList updateTask(taskid_t id, task_t task) throw (std::exception); TaskIdList removeTask(taskid_t id) throw (std::exception); - TaskIdList moveTask(taskid_t id, taskid_t newParent) throw (std::exception); + + TaskIdListPair moveTask(taskid_t id, taskid_t newParent) throw (std::exception); TaskIdList subTasks(taskid_t) throw (std::exception); diff --git a/src/tasktree.cc b/src/tasktree.cc index fc84efa..5c58e3c 100644 --- a/src/tasktree.cc +++ b/src/tasktree.cc @@ -124,6 +124,7 @@ TaskIdList TaskTree::remove(taskid_t id) return affectedNodes; } +/* TaskIdList TaskTree::move(taskid_t id, taskid_t toid) throw (std::exception) { @@ -156,7 +157,9 @@ TaskIdList TaskTree::move(taskid_t id, taskid_t toid) return affectedNodes; } +*/ + TaskIdList TaskTree::updateData(taskid_t id, task_t t) throw (std::exception) { diff --git a/src/tasktree.h b/src/tasktree.h index d82b9a4..41a29be 100644 --- a/src/tasktree.h +++ b/src/tasktree.h @@ -52,7 +52,7 @@ public: TaskIdList insertAsChild(taskid_t parentid, taskid_t id, task_t data) throw (std::exception); TaskIdList remove(taskid_t id) throw (std::exception); - TaskIdList move(taskid_t id, taskid_t newParentId) throw (std::exception); + // TaskIdList move(taskid_t id, taskid_t newParentId) throw (std::exception); TaskIdList updateData(taskid_t id, task_t t) throw (std::exception); task_t data(taskid_t id) throw (std::exception); |