From 48cd878e5a0cfaedae93fc515148e784e1534fbd Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Fri, 24 Feb 2012 17:17:27 +0100 Subject: Removed message parsing code to msgparser. --- src/task_proto.cc | 177 +++++++++++++++++++++++------------------------------- 1 file changed, 76 insertions(+), 101 deletions(-) (limited to 'src/task_proto.cc') diff --git a/src/task_proto.cc b/src/task_proto.cc index d4d9a70..f1d22d1 100644 --- a/src/task_proto.cc +++ b/src/task_proto.cc @@ -34,6 +34,7 @@ #include #include "task.h" +#include "msgparser.h" static void dump_handshake_info(struct lws_tokens *lwst) { @@ -75,8 +76,6 @@ static void dump_handshake_info(struct lws_tokens *lwst) std::map > msgqueue; -static int id_count = 0; - int callback_lws_task(struct libwebsocket_context * context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, @@ -99,7 +98,7 @@ int callback_lws_task(struct libwebsocket_context * context, std::string init_str; TaskList::iterator it; for(it = tasklist.begin(); it != tasklist.end(); it++) { - struct task t = *it; + task_t t = *it; sprintf(buf, "add %d %s %s %d %d;", t.id, t.title.c_str(), t.desc.c_str(), t.x, t.y); @@ -161,115 +160,91 @@ int callback_lws_task(struct libwebsocket_context * context, printf("%s\n", (char*)in); std::string data; data.append((char*)in, len); - + /* - struct a_message &msg = ringbuffer[ringbuffer_head]; - if(msg.payload) { - free(msg.payload); - msg.payload = NULL; - } + struct a_message &msg = ringbuffer[ringbuffer_head]; + if(msg.payload) { + free(msg.payload); + msg.payload = NULL; + } - */ +*/ char buf[1024]; size_t buf_len = 0; - std::string cmd = data.substr(0, data.find(' ')); - printf("Cmd: %s\n", cmd.c_str()); - - if(cmd == "add") { - printf("Handling add cmd:\n"); - int offset = cmd.length() + 1; - std::string title = data.substr(offset, data.find(' ', offset) - offset); - offset += title.length() + 1; - std::string desc = data.substr(offset, data.find(' ', offset) - offset); - offset += desc.length() + 1; - std::string x_str = data.substr(offset, data.find(' ', offset) - offset); - int x = atoi(x_str.c_str()); - offset += x_str.length() + 1; - std::string y_str = data.substr(offset, data.find(' ', offset) - offset); - int y = atoi(y_str.c_str()); - - struct task t; - t.x = x; - t.y = y; - t.title = title; - t.desc = desc; - t.id = id_count; id_count++; - tasklist.push_back(t); - - buf_len = sprintf(buf, "add %d %s %s %d %d;", - t.id, t.title.c_str(), t.desc.c_str(), - t.x, t.y); - - printf("Adding task: %s\n", buf); - - } else if(cmd == "del") { - printf("Delete\n"); - int offset = cmd.length() + 1; - std::string id_str = data.substr(offset, data.find(' ', offset) - offset); - int id = atoi(id_str.c_str()); - printf("Deleting task with id %d\n", id); - - bool id_found = false; - TaskList::iterator it; - for(it = tasklist.begin(); it != tasklist.end(); it++) { - struct task t = *it; - if(t.id == id) { - id_found = true; - tasklist.erase(it); - break; - } + msg_t m = parse_msg(data)[0]; + + switch(m.cmd) { + case cmd::add: { + printf("Handling add cmd:\n"); + + task_t t =create_task(m.add.title, m.add.desc, + m.add.x, m.add.y); + tasklist.push_back(t); + buf_len = sprintf(buf, "add %d %s %s %d %d;", + t.id, t.title.c_str(), t.desc.c_str(), + t.x, t.y); + + printf("Adding task: %s\n", buf); + break; } + case cmd::del: { + printf("Delete\n"); + printf("Deleting task with id %d\n", m.del.id); + + bool id_found = false; + TaskList::iterator it; + for(it = tasklist.begin(); it != tasklist.end(); it++) { + task_t t = *it; + if(t.id == m.del.id) { + id_found = true; + tasklist.erase(it); + break; + } + } + + if(!id_found) { + printf("\t!!!Could not locate task with id %d\n", m.del.id); + } - if(!id_found) { - printf("\t!!!Could not locate task with id %d\n", id); + buf_len = sprintf(buf, "del %d;", m.del.id); + printf("Deleting task: %s\n", buf); + break; } + case cmd::move: { + printf("Move\n"); + + printf("Moving task with id %d to (%d,%d)\n", m.move.id, m.move.x, m.move.y); + + bool id_found = false; + TaskList::iterator it; + + int x = m.move.x / 300 * 300; + + for(it = tasklist.begin(); it != tasklist.end(); it++) { + task_t t = *it; + if(t.id == m.move.id) { + id_found = true; + t.x = x; + t.y = m.move.y; + break; + } + } - buf_len = sprintf(buf, "del %d;", id); - printf("Deleting task: %s\n", buf); - - } else if(cmd == "move") { - printf("Move\n"); - - int offset = cmd.length() + 1; - std::string s_id = data.substr(offset, data.find(' ', offset) - offset); - int id = atoi(s_id.c_str()); - offset += s_id.length() + 1; - std::string x_str = data.substr(offset, data.find(' ', offset) - offset); - int x = atoi(x_str.c_str()); - offset += x_str.length() + 1; - std::string y_str = data.substr(offset, data.find(' ', offset) - offset); - int y = atoi(y_str.c_str()); - - printf("Moving task with id %d to (%d,%d)\n", id, x, y); - - bool id_found = false; - TaskList::iterator it; - - x = x / 300 * 300; - - for(it = tasklist.begin(); it != tasklist.end(); it++) { - struct task &t = *it; - if(t.id == id) { - id_found = true; - t.x = x; - t.y = y; - break; + if(!id_found) { + printf("\t!!!Could not locate task with id %d\n", m.move.id); } - } - - if(!id_found) { - printf("\t!!!Could not locate task with id %d\n", id); - } - buf_len = sprintf(buf, "move %d %d %d;", id, x, y); - printf("Moving task: %s\n", buf); - } else if(cmd == "update") { - printf("Update\n"); - } - else { // unknown command - printf("Unknown command :(\n"); - break; + buf_len = sprintf(buf, "move %d %d %d;", m.move.id, x, m.move.y); + printf("Moving task: %s\n", buf); + break; + } + case cmd::update: + printf("Update\n"); + break; + default: + printf("Wrong command :(\n"); + break; } // msg.payload = malloc(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING); -- cgit v1.2.3