diff options
author | Jonas Suhr Christensen <jsc@umbraculum.org> | 2012-03-03 09:40:39 +0100 |
---|---|---|
committer | Jonas Suhr Christensen <jsc@umbraculum.org> | 2012-03-03 09:40:39 +0100 |
commit | bdbe7ab02d91634f2a35b652151f567c86b0318b (patch) | |
tree | 52e6e9632d1956b273f4656627d31535b6d5d205 | |
parent | 8d35024bf4d2298c22298493f9a8398f83366ccc (diff) |
Removed sprintfs from task_proto and moved the code to msgparser.
Removed heavy printf debugging from msgparser.
-rw-r--r-- | src/msgparser.cc | 102 | ||||
-rw-r--r-- | src/msgparser.h | 13 | ||||
-rw-r--r-- | src/task_proto.cc | 64 |
3 files changed, 139 insertions, 40 deletions
diff --git a/src/msgparser.cc b/src/msgparser.cc index c05a9ee..70c71c6 100644 --- a/src/msgparser.cc +++ b/src/msgparser.cc @@ -96,28 +96,28 @@ MsgVector parse_msg(std::string data) { if(prev_ch != '\\') inside_quote = !inside_quote; else { - printf("Appending %c\n", ch); +// printf("Appending %c\n", ch); token += ch; } break; case ' ': if(inside_quote) { - printf("Appending %c\n", ch); +// printf("Appending %c\n", ch); token += ch; break; } if(token.empty()) continue; // skip multiple white spaces and pre white space - printf("Adding token %s\n", token.c_str()); +// printf("Adding token %s\n", token.c_str()); tokens.push_back(token); token.clear(); break; case ';': if(inside_quote) { - printf("Appending %c\n", ch); +// printf("Appending %c\n", ch); token += ch; break; } - printf("Adding msg...\n"); +// printf("Adding msg...\n"); if(!token.empty()) { tokens.push_back(token); } @@ -126,7 +126,7 @@ MsgVector parse_msg(std::string data) { token.clear(); break; default: - printf("Appending %c\n", ch); +// printf("Appending %c\n", ch); token += ch; break; } @@ -143,7 +143,7 @@ MsgVector parse_msg(std::string data) { tokens.clear(); } - printf("Number of messages: %d\n", msglist.size()); +// printf("Number of messages: %d\n", msglist.size()); MsgVector v; MsgList::iterator it_msg; @@ -161,7 +161,7 @@ MsgVector parse_msg(std::string data) { else if(t[0] == "update") m.cmd = cmd::update; else m.cmd = cmd::error; - printf("Number of tokens %d\n", t.size()); +// printf("Number of tokens %d\n", t.size()); switch(m.cmd) { case cmd::add: { @@ -169,10 +169,11 @@ MsgVector parse_msg(std::string data) { printf("Wrong number of parameters\n"); continue; } - sprintf(m.add.title, "%s", t[0].c_str()); - sprintf(m.add.desc, "%s", t[1].c_str()); - m.add.x = atoi(t[2].c_str()); - m.add.y = atoi(t[3].c_str()); + sprintf(m.add.title, "%s", t[1].c_str()); + sprintf(m.add.desc, "%s", t[2].c_str()); + m.add.x = atoi(t[3].c_str()); + m.add.y = atoi(t[4].c_str()); +// printf("addcmd: %s %s %d %d\n", m.add.title, m.add.desc, m.add.x, m.add.y); break; } case cmd::del: { @@ -210,6 +211,83 @@ MsgVector parse_msg(std::string data) { return v; } +msg_t create_msg(cmd::cmd_t type, task_t t) { + msg_t m; + m.cmd = type; + switch(type) { + case cmd::add: { + m.add.id = t.id; + m.add.x = t.x; + m.add.y = t.y; +// m.add.title = t.title.c_str(); +// m.add.desc = t.desc.c_str(); + snprintf(m.add.title, sizeof(m.add.title), "%s", t.title.c_str()); + snprintf(m.add.desc, sizeof(m.add.desc), "%s", t.desc.c_str()); + break; + } + case cmd::del: { + m.del.id = t.id; + break; + } + case cmd::move: { + m.move.id = t.id; + m.move.x = t.x; + m.move.y = t.y; + break; + } + case cmd::update: { + m.update.id = t.id; +// m.update.title = t.title; +// m.update.desc = t.desc; + snprintf(m.update.title, sizeof(m.update.title), "%s", t.title.c_str()); + snprintf(m.update.desc, sizeof(m.update.desc), "%s", t.desc.c_str()); + break; + }; + default: + break; + } + return m; +} + +#define BUF_SIZE 4096 +std::string msg_tostring(msg_t m) { + char buf[4096]; + switch(m.cmd) { + case cmd::add: { + printf("msg: %d, %d, %d, %s, %s\n", m.add.id, m.add.x, m.add.y, m.add.title, m.add.desc); + snprintf(buf, BUF_SIZE, "add %d %s %s %d %d;", + m.add.id, + m.add.title, m.add.desc, + m.add.x, m.add.y); + break; + } + case cmd::del: { + snprintf(buf, BUF_SIZE, "del %d;", m.del.id); + break; + } + case cmd::move: { + snprintf(buf, BUF_SIZE, "move %d %d %d;", m.move.id, m.move.x, m.move.y); + break; + } + case cmd::update: { + //todo + break; + }; + default: + break; + } + + printf("buffer: %s\n", buf); + + if(buf) { + std::string str(buf); + return str; + } + else { + return ""; + } +} + #ifdef TEST_MSGPARSER //Additional dependency files //deps: diff --git a/src/msgparser.h b/src/msgparser.h index 07d67e9..fa853fa 100644 --- a/src/msgparser.h +++ b/src/msgparser.h @@ -31,6 +31,8 @@ #include <vector> #include <string> +#include "task.h" + namespace cmd { enum cmd_t { update, @@ -42,10 +44,11 @@ namespace cmd { }; typedef struct { + int id; int x; int y; - char title[32]; - char desc[32]; + char title[256]; + char desc[256]; } add_t; typedef struct { int id; @@ -57,8 +60,8 @@ typedef struct { } move_t; typedef struct { int id; - char title[32]; - char desc[32]; + char title[256]; + char desc[256]; } update_t; @@ -80,4 +83,6 @@ typedef std::vector<msg_t> MsgVector; MsgVector parse_msg(std::string msg); +msg_t create_msg(cmd::cmd_t msg_type, task_t task); +std::string msg_tostring(msg_t msg); #endif/*__MUNIA_MSGPARSER_H__*/ diff --git a/src/task_proto.cc b/src/task_proto.cc index 39232dd..9ed515e 100644 --- a/src/task_proto.cc +++ b/src/task_proto.cc @@ -94,15 +94,17 @@ int callback_lws_task(struct libwebsocket_context * context, pss->wsi = wsi; // send all current tasks - char buf[32]; +// char buf[512]; std::string init_str; TaskList::iterator it; for(it = tasklist.begin(); it != tasklist.end(); it++) { - task_t 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); - init_str.append(buf); +// sprintf(buf, "add %d %s %s %d %d;", +// t.id, t.title.c_str(), t.desc.c_str(), t.x, t.y); + +// init_str.append(task_cmd(t)); + init_str = msg_tostring(create_msg(cmd::add, t)); } std::string msg; @@ -169,8 +171,10 @@ int callback_lws_task(struct libwebsocket_context * context, } */ - char buf[1024]; - size_t buf_len = 0; +// char buf[1024]; +// size_t buf_len = 0; + + std::string buf_str; MsgVector msglist = parse_msg(data); MsgVector::iterator it_msg; @@ -178,28 +182,34 @@ int callback_lws_task(struct libwebsocket_context * context, msg_t m = *it_msg; switch(m.cmd) { case cmd::add: { - printf("Handling add cmd:\n"); + printf("Handling add cmd\n"); - task_t t =create_task(m.add.title, m.add.desc, - m.add.x, m.add.y); + 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); +// buf_len = sprintf(buf, "add %d %s %s %d %d;", +// t.id, t.title.c_str(), t.desc.c_str(), +// t.x, t.y); + +// msg_t tm = create_msg(cmd::add, t); +// std::string str = msg_tostring(tm); + buf_str = msg_tostring(create_msg(cmd::add, t)); - printf("Adding task: %s\n", buf); + printf("Adding task: %s\n", buf_str.c_str()); break; } case cmd::del: { printf("Delete\n"); printf("Deleting task with id %d\n", m.del.id); + task_t del_task; bool id_found = false; TaskList::iterator it; for(it = tasklist.begin(); it != tasklist.end(); it++) { - task_t t = *it; + task_t &t = *it; if(t.id == m.del.id) { id_found = true; + del_task = t; tasklist.erase(it); break; } @@ -208,9 +218,11 @@ int callback_lws_task(struct libwebsocket_context * context, if(!id_found) { printf("\t!!!Could not locate task with id %d\n", m.del.id); } - - buf_len = sprintf(buf, "del %d;", m.del.id); - printf("Deleting task: %s\n", buf); + else { + // buf_len = sprintf(buf, "del %d;", m.del.id); + buf_str = msg_tostring(create_msg(cmd::del, del_task)); + printf("Deleting task: %s\n", buf_str.c_str()); + } break; } case cmd::move: { @@ -222,13 +234,15 @@ int callback_lws_task(struct libwebsocket_context * context, TaskList::iterator it; int x = m.move.x / 300 * 300; - + + task_t moved_task; for(it = tasklist.begin(); it != tasklist.end(); it++) { - task_t t = *it; - if(t.id == m.move.id) { + task_t &t = *it; + if(it->id == m.move.id) { id_found = true; t.x = x; t.y = m.move.y; + moved_task = t; break; } } @@ -237,8 +251,9 @@ int callback_lws_task(struct libwebsocket_context * context, printf("\t!!!Could not locate task with id %d\n", m.move.id); } - buf_len = sprintf(buf, "move %d %d %d;", m.move.id, x, m.move.y); - printf("Moving task: %s\n", buf); +// buf_len = sprintf(buf, "move %d %d %d;", m.move.id, x, m.move.y); + buf_str = msg_tostring(create_msg(cmd::move, moved_task)); + printf("Moving task: %s\n", buf_str.c_str()); break; } case cmd::update: @@ -265,7 +280,8 @@ int callback_lws_task(struct libwebsocket_context * context, */ std::string msg; msg.append((size_t)LWS_SEND_BUFFER_PRE_PADDING, ' '); - msg.append(buf, buf_len); +// msg.append(buf, buf_len); + msg.append(buf_str); msg.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' '); std::map<struct libwebsocket *, std::queue<std::string> >::iterator it = msgqueue.begin(); |