summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2012-02-24 17:17:27 +0100
committerJonas Suhr Christensen <jsc@umbraculum.org>2012-02-24 17:17:27 +0100
commit48cd878e5a0cfaedae93fc515148e784e1534fbd (patch)
treeb66abf9a56bbd5c8545c6d02fd7f53d1415e44f0
parent3ab33f728f61b12f85a6067d02610c2b5142a4a5 (diff)
Removed message parsing code to msgparser.
-rw-r--r--src/Makefile.am6
-rw-r--r--src/msgparser.cc128
-rw-r--r--src/msgparser.h83
-rw-r--r--src/task.cc18
-rw-r--r--src/task.h10
-rw-r--r--src/task_proto.cc177
6 files changed, 316 insertions, 106 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 9b20d05..1aff748 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,16 +4,18 @@ bin_PROGRAMS = muniad
muniad_LDADD = $(LIBWEBSOCKETS_LIBS)
-muniad_CXXFLAGS = $(LIBWEBSOCKETS_CFLAGS)
+muniad_CXXFLAGS = $(LIBWEBSOCKETS_CFLAGS)
muniad_SOURCES = \
muniad.cc \
http.cc \
+ msgparser.cc \
task.cc \
task_proto.cc
EXTRA_DIST = \
http.h \
+ msgparser.h \
task.h \
task_proto.h
@@ -26,4 +28,4 @@ TEST_SCRIPT_DIR = $(top_srcdir)/tools
include ${TEST_SCRIPT_DIR}/Makefile.am.test
-include Makefile.am.test \ No newline at end of file
+include Makefile.am.test
diff --git a/src/msgparser.cc b/src/msgparser.cc
new file mode 100644
index 0000000..ec41b3b
--- /dev/null
+++ b/src/msgparser.cc
@@ -0,0 +1,128 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * msgparser.cc
+ *
+ * Fri Feb 24 14:59:34 CET 2012
+ * Copyright 2012 Jonas Suhr Christensen
+ * jsc@umbraculum.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Munia.
+ *
+ * Munia is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Munia is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Munia; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "msgparser.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+inline void parse_add(msg_t& m, std::string data) {
+ int offset = 0;
+ 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());
+
+ sprintf(m.add.title, "%s", desc.c_str());
+ sprintf(m.add.desc, "%s", desc.c_str());
+ m.add.x = x;
+ m.add.y = y;
+}
+
+inline void parse_del(msg_t& m, std::string data) {
+ int offset = 0;
+ std::string id_str = data.substr(offset, data.find(' ', offset) - offset);
+ int id = atoi(id_str.c_str());
+
+ m.del.id = id;
+}
+
+inline void parse_move(msg_t& m, std::string data) {
+ int offset = 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());
+
+ m.move.id = id;
+ m.move.x = x;
+ m.move.y = y;
+}
+
+MsgVector parse_msg(std::string data) {
+
+ //todo: explode on ';' and handle '"' and escaping
+
+ printf("parsing: %s\n", data.c_str());
+
+ msg_t m;
+
+ std::string cmd = data.substr(0, data.find(' '));
+
+ if(cmd == "add") m.cmd = cmd::add;
+ else if(cmd == "del") m.cmd = cmd::del;
+ else if(cmd == "move") m.cmd = cmd::move;
+ else if(cmd == "update") m.cmd = cmd::update;
+ else m.cmd = cmd::error;
+
+ data = data.substr(cmd.length()+1, std::string::npos);
+
+ switch(m.cmd) {
+ case cmd::add:
+ parse_add(m, data);
+ break;
+ case cmd::del:
+ parse_del(m, data);
+ break;
+ case cmd::move:
+ parse_move(m, data);
+ break;
+ case cmd::update:
+ break;
+ default:
+ break;
+ }
+
+ MsgVector v; v.push_back(m);
+ return v;
+}
+
+#ifdef TEST_MSGPARSER
+//Additional dependency files
+//deps:
+//Required cflags (autoconf vars may be used)
+//cflags:
+//Required link options (autoconf vars may be used)
+//libs:
+#include "test.h"
+
+TEST_BEGIN;
+
+// TODO: Put some testcode here (see test.h for usable macros).
+TEST_TRUE(false, "No tests yet!");
+
+TEST_END;
+
+#endif/*TEST_MSGPARSER*/
diff --git a/src/msgparser.h b/src/msgparser.h
new file mode 100644
index 0000000..07d67e9
--- /dev/null
+++ b/src/msgparser.h
@@ -0,0 +1,83 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * msgparser.h
+ *
+ * Fri Feb 24 14:59:34 CET 2012
+ * Copyright 2012 Jonas Suhr Christensen
+ * jsc@umbraculum.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Munia.
+ *
+ * Munia is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Munia is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Munia; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __MUNIA_MSGPARSER_H__
+#define __MUNIA_MSGPARSER_H__
+
+#include <vector>
+#include <string>
+
+namespace cmd {
+ enum cmd_t {
+ update,
+ move,
+ add,
+ del,
+ error
+ };
+};
+
+typedef struct {
+ int x;
+ int y;
+ char title[32];
+ char desc[32];
+} add_t;
+typedef struct {
+ int id;
+} del_t;
+typedef struct {
+ int id;
+ int x;
+ int y;
+} move_t;
+typedef struct {
+ int id;
+ char title[32];
+ char desc[32];
+} update_t;
+
+
+typedef struct msg_t {
+ cmd::cmd_t cmd;
+
+ union {
+ add_t add;
+ del_t del;
+ move_t move;
+ update_t update;
+ };
+
+
+} msg_types;
+
+
+typedef std::vector<msg_t> MsgVector;
+
+MsgVector parse_msg(std::string msg);
+
+#endif/*__MUNIA_MSGPARSER_H__*/
diff --git a/src/task.cc b/src/task.cc
index 0b948c5..24e94a1 100644
--- a/src/task.cc
+++ b/src/task.cc
@@ -28,3 +28,21 @@
#include "task.h"
TaskList tasklist;
+static int id_count = 0;
+
+int current_id_count() {
+ return id_count;
+}
+
+task_t create_task(std::string title, std::string desc,
+ int x, int y) {
+
+ task_t t;
+ t.x = x;
+ t.y = y;
+ t.title = title;
+ t.desc = desc;
+ t.id = id_count; id_count++;
+
+ return t;
+}
diff --git a/src/task.h b/src/task.h
index ffe93d7..4365c1c 100644
--- a/src/task.h
+++ b/src/task.h
@@ -51,14 +51,18 @@ x and y are integers as strings
id are an integer as a string
*/
-struct task {
+
+typedef struct {
int x, y;
int id;
std::string title;
std::string desc;
-};
+} task_t;
-typedef std::list<struct task> TaskList;
+typedef std::list<task_t> TaskList;
extern TaskList tasklist;
+task_t create_task(std::string title, std::string desc,
+ int x, int y);
+
#endif/*__MUNIA_TASK_H__*/
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 <map>
#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<struct libwebsocket *, std::queue<std::string> > 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);