summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2012-03-30 16:04:03 +0200
committerJonas Suhr Christensen <jsc@umbraculum.org>2012-03-30 16:04:03 +0200
commitb320cc10871217d51e3458bf85a22e7d50ee4aec (patch)
tree6afc5a990d1b719ac178e8ebd09de3e7b3114ad2
parent85bc3a07c275ef60fb40bc55562b12ab2e21841d (diff)
Added a bit of debug and implemented messagehandler.
-rw-r--r--src/connectionhandler.cc6
-rw-r--r--src/messagehandler.cc51
-rw-r--r--src/saxparser.cc204
-rw-r--r--src/task_proto.cc10
-rw-r--r--src/taskmanager.cc7
-rw-r--r--src/taskmanager.h2
-rw-r--r--src/tasktree.cc14
7 files changed, 74 insertions, 220 deletions
diff --git a/src/connectionhandler.cc b/src/connectionhandler.cc
index 6fdcd51..9362965 100644
--- a/src/connectionhandler.cc
+++ b/src/connectionhandler.cc
@@ -27,6 +27,8 @@
*/
#include "connectionhandler.h"
+#include <stdio.h>
+
// Global ConnectionHandler.
ConnectionHandler connection_handler;
@@ -42,11 +44,13 @@ void ConnectionHandler::init(clientid_t clientid)
void ConnectionHandler::close(clientid_t clientid)
{
connlist.erase(clientid);
+ printf("Removed connection\n");
}
void ConnectionHandler::observe(clientid_t clientid, taskid_t taskid)
{
connlist[clientid].insert(taskid);
+ printf("Added observer of %d\n", taskid);
}
void ConnectionHandler::unobserve(clientid_t clientid, taskid_t taskid)
@@ -56,10 +60,12 @@ void ConnectionHandler::unobserve(clientid_t clientid, taskid_t taskid)
std::set<clientid_t> ConnectionHandler::observerlist(TaskIdList tasks)
{
+ printf("Observerlist request\n");
std::set<clientid_t> clients;
for(TaskIdList::iterator i = tasks.begin(); i != tasks.end(); i++) {
taskid_t tid = *i;
+ printf("Locating observers of node %d\n", tid);
for(ConnectionList::iterator ci = connlist.begin();
ci != connlist.end(); ci++) {
std::set<taskid_t>::iterator ti = ci->second.find(tid);
diff --git a/src/messagehandler.cc b/src/messagehandler.cc
index 90a89a3..5f3cafb 100644
--- a/src/messagehandler.cc
+++ b/src/messagehandler.cc
@@ -32,47 +32,74 @@
TaskManager taskman;
-MessageList handle_msg(MessageList inMsgList) {
+MessageList handle_msg(MessageList msgList) {
MessageList::iterator it;
- for(it = inMsgList.begin();
- it != inMsgList.end();
+ for(it = msgList.begin();
+ it != msgList.end();
it++) {
message_t m = *it;
switch(m.cmd) {
case cmd::add:
{
- INFO(msghandler, "Handling add command\n");
+ INFO(messagehandler, "Handling add command\n");
task_t t = taskman.createTask();
- // t.parentid = m.add.parentid;
t.title = m.add.title;
t.desc = m.add.desc;
+ try {
+ it->nodes = taskman.addTask(t, m.add.parentid);
+ }
+ catch (std::exception& e) {
+ DEBUG(messagehandler, "Error adding task\n");
+ }
}
break;
case cmd::del:
{
- INFO(msghandler, "Handling del command\n");
+ INFO(messagehandler, "Handling del command\n");
+ try {
+ it->nodes = taskman.deleteTask(m.del.id);
+ }
+ catch (std::exception& e) {
+ DEBUG(messagehandler, "Error deleting task\n");
+ }
}
break;
case cmd::move:
{
- INFO(msghandler, "Handling move command\n");
+ INFO(messagehandler, "Handling move command\n");
+ try {
+ it->nodes = taskman.moveTask(m.move.id, m.move.parentid);
+ }
+ catch (std::exception& e) {
+ DEBUG(messagehandler, "Error moving task\n");
+ }
}
break;
case cmd::update:
{
- INFO(msghandler, "Handling update command\n");
+ INFO(messagehandler, "Handling update command\n");
+ try {
+ task_t t;
+ t.title = m.update.title;
+ t.desc = m.update.desc;
+ it->nodes = taskman.updateTask(m.update.id, t);
+ }
+ catch (std::exception& e) {
+ DEBUG(messagehandler, "Error updating task\n");
+ }
}
break;
default:
- WARN(msghandler, "!!! Unknown command\n");
+ WARN(messagehandler, "!!! Unknown command\n");
break;
}
+ taskman.tree.toStdOut();
+ printf("%d affected nodes registered\n", m.nodes.size());
}
-
- MessageList msglist;
- return msglist;
+
+ return msgList;
}
#ifdef TEST_MSGHANDLER
diff --git a/src/saxparser.cc b/src/saxparser.cc
index 14f204c..065a9d8 100644
--- a/src/saxparser.cc
+++ b/src/saxparser.cc
@@ -198,215 +198,13 @@ void SAXParser::characterData(std::string &)
}
#ifdef TEST_SAXPARSER
-//deps: log.cc debug.cc exception.cc
+//deps:
//cflags: -I..
//libs: -lexpat
#include <test.h>
-#define XMLFILE "/tmp/saxparsertest.xml"
-
-#include "exception.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <string.h>
-#include <stdio.h>
-#include <memory.h>
-
-static char xml[] =
-"<?xml version='1.0' encoding='UTF-8'?>\n"
-"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n"
-" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\">\n"
-" <field value=\"Some docs\" name=\"referral.doctor\"/>\n"
-" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n"
-" <field value=\"Avs\" name=\"referral.diagnose\"/>\n"
-" </commit>\n"
-"</pracro>\n \t\n\r"
- ;
-
-static char xml_notrailingwhitespace[] =
-"<?xml version='1.0' encoding='UTF-8'?>\n"
-"<pracro version=\"1.0\" user=\"testuser\" cpr=\"1505050505\">\n"
-" <commit version=\"\" macro=\"referral\" template=\"amd_forunders\">\n"
-" <field value=\"Some docs\" name=\"referral.doctor\"/>\n"
-" <field value=\"DIMS\" name=\"referral.diagnosecode\"/>\n"
-" <field value=\"Avs\" name=\"referral.diagnose\"/>\n"
-" </commit>\n"
-"</pracro>"
- ;
-
-static char xml_fail[] =
-"<?xml version='1.0' encoding='UTF-8'?>\n"
-"<pracro version=\"1.0\" user\"testuser\" cpr=\"1505050505\">\n"
-" <request macro=\"test\" template=\"test\"/>\n"
-"</pracro>\n"
- ;
-
-static char xml_fail2[] =
-"<?xml version='1.0' encoding='UTF-8'?>\n"
-"<pracro version=\"1.0\" user\"testuser\" cpr=\"1505050505\">\n"
-" <request macro=\"test\" template=\"test\"/>\n"
-"</pracro>\n"
-"this is junk\n"
- ;
-
-class MyFileParser :public SAXParser {
-public:
- MyFileParser(const char *file) {
- fd = open(file, O_RDONLY);
- }
-
- int readData(char *data, size_t size) {
- return read(fd, data, size);
- }
-
- void startTag(std::string name, attributes_t &attributes)
- {
- //printf("<%s>\n", name.c_str());
- }
-
- void parseError(const char *buf, size_t len, std::string error, int lineno)
- {
- throw Exception(error);
- }
-
-private:
- int fd;
-};
-
-class MyBufferParser :public SAXParser {
-public:
- void startTag(std::string name, attributes_t &attributes)
- {
- //printf("<%s>\n", name.c_str());
- }
-
- void parseError(char *buf, size_t len, std::string error, int lineno)
- {
- throw Exception(error);
- }
-};
-
TEST_BEGIN;
-FILE *fp = fopen(XMLFILE, "w");
-TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
-if(!fp) TEST_FATAL("Could not write "XMLFILE);
-fprintf(fp, "%s", xml);
-fclose(fp);
-
-TEST_MSG("Test callback parser.");
-{
- MyFileParser parser(XMLFILE);
- parser.parse();
-}
-
-TEST_MSG("Test buffer parser.");
-for(size_t sz = 1; sz < 1000; sz++) {
- bool test = false;
- MyBufferParser parser;
- std::string buf = xml;
- size_t pos = 0;
- while(pos < buf.length()) {
- std::string substr = buf.substr(pos, sz);
-
- try {
- test |= parser.parse((char*)substr.c_str(), substr.length());
- } catch(Exception &e) {
- TEST_TRUE(true, "Buffer parser failed on size %d: %s [%s]",
- sz, e.what(), substr.c_str());
- }
- pos += sz;
- }
-
- TEST_TRUE(test, "Test buffer parser on %d bytes", sz);
- }
-
-fp = fopen(XMLFILE, "w");
-TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
-if(!fp) TEST_FATAL("Could not write "XMLFILE);
-fprintf(fp, "%s", xml_notrailingwhitespace);
-fprintf(fp, "%s", xml_notrailingwhitespace);
-fclose(fp);
-
-TEST_MSG("Test buffer parser with multiple documents in the same buffer.");
-{
- fp = fopen(XMLFILE, "r");
- TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be read.");
- if(!fp) TEST_FATAL("Could not read from "XMLFILE);
-
- for(size_t sz = 1; sz < 1000; sz++) {
- MyBufferParser *parser = NULL;
- rewind(fp);
- size_t numdocs = 0;
- char *buf = new char[sz + 1];
- memset(buf, 0, sz + 1);
- size_t size;
- while( (size = fread(buf, 1, sz, fp)) > 0) {
- while(size) {
- if(parser == NULL) {
- parser = new MyBufferParser();
- }
- if(parser->parse(buf, size)) {
-
- // Got one
- numdocs++;
-
- size = size - parser->usedBytes();
- strcpy(buf, buf + parser->usedBytes());
- delete parser; parser = NULL;
- } else {
- size = 0;
- memset(buf, 0, sz + 1);
- }
- }
- }
- TEST_EQUAL(numdocs, 2, "Test if 2 documents were parsed on docsize %d.", sz);
- if(parser) delete parser; parser = NULL;
- delete[] buf;
- }
- fclose(fp);
-}
-
-fp = fopen(XMLFILE, "w");
-TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
-if(!fp) TEST_FATAL("Could not write "XMLFILE);
-fprintf(fp, "%s", xml_fail);
-fclose(fp);
-
-TEST_MSG("Test failure");
-{
- MyFileParser parser(XMLFILE);
- try {
- parser.parse();
- } catch(Exception &e) {
- goto goon;
- }
- TEST_TRUE(false, "This test should fail...\n");
-}
-goon:
-
-fp = fopen(XMLFILE, "w");
-TEST_NOTEQUAL(fp, NULL, "Test if file \""XMLFILE"\" could be written.");
-if(!fp) TEST_FATAL("Could not write "XMLFILE);
-fprintf(fp, "%s", xml_fail2);
-fclose(fp);
-
-// Test failure
-{
- MyFileParser parser(XMLFILE);
- try {
- parser.parse();
- } catch(Exception &e) {
- goto goonagain;
- }
- TEST_TRUE(false, "This test should fail...\n");
-}
-goonagain:
-
-unlink(XMLFILE);
-
TEST_END;
#endif/*TEST_SAXPARSER*/
diff --git a/src/task_proto.cc b/src/task_proto.cc
index 304080d..24be3b0 100644
--- a/src/task_proto.cc
+++ b/src/task_proto.cc
@@ -133,17 +133,23 @@ int callback_lws_task(struct libwebsocket_context * context,
data.append((char*)in, len);
MessageList mlst = parse_msg(data);
+ printf("Handling %d incoming message\n", mlst.size());
MessageList omsgs = handle_msg(mlst);
+ printf("Handling %d outgoing messages\n", omsgs.size());
+
MessageList::iterator omi = omsgs.begin();
while(omi != omsgs.end()) {
std::string msg;
msg.append((size_t)LWS_SEND_BUFFER_PRE_PADDING, ' ');
msg.append(msg_tostring(*omi));
msg.append((size_t)LWS_SEND_BUFFER_POST_PADDING, ' ');
-
+
+ printf("%d nodes affected by command\n", omi->nodes.size());
+
std::set<clientid_t> clients =
connection_handler.observerlist(omi->nodes);
-
+ printf("Writing message to %d clients\n", clients.size());
+
std::set<clientid_t>::iterator ci = clients.begin();
while(ci != clients.end()) {
msgqueue[*ci].push(msg);
diff --git a/src/taskmanager.cc b/src/taskmanager.cc
index 02f31e1..a197a79 100644
--- a/src/taskmanager.cc
+++ b/src/taskmanager.cc
@@ -82,7 +82,7 @@ TaskIdList TaskManager::moveTask(taskid_t id, taskid_t to)
goto finish;
}
catch (std::exception& e) {
-
+ throw e;
}
finish:
@@ -122,10 +122,12 @@ TaskIdList TaskManager::updateTask(taskid_t id, task_t t)
return affectedTasks;
}
-TaskIdList TaskManager::addTask(task_t t, taskid_t id, taskid_t parentid)
+TaskIdList TaskManager::addTask(task_t t, taskid_t parentid)
throw (std::exception) {
TaskIdList affectedTasks;
+ taskid_t id = createId();
+
try {
affectedTasks = tree.insertAsChild(parentid, id, t);
goto finish;
@@ -135,6 +137,7 @@ TaskIdList TaskManager::addTask(task_t t, taskid_t id, taskid_t parentid)
}
finish:
+ printf("Returning %d nodes\n", affectedTasks.size());
return affectedTasks;
}
diff --git a/src/taskmanager.h b/src/taskmanager.h
index c737aba..d6a418b 100644
--- a/src/taskmanager.h
+++ b/src/taskmanager.h
@@ -42,7 +42,7 @@ public:
~TaskManager();
task_t createTask();
- TaskIdList addTask(task_t t, taskid_t, taskid_t parentid) throw (std::exception);
+ TaskIdList addTask(task_t t, taskid_t parentid) throw (std::exception);
TaskIdList updateTask(taskid_t id, task_t task) throw (std::exception);
TaskIdList deleteTask(taskid_t id) throw (std::exception);
TaskIdList moveTask(taskid_t id, taskid_t newParent) throw (std::exception);
diff --git a/src/tasktree.cc b/src/tasktree.cc
index 3aa13f4..0df23d6 100644
--- a/src/tasktree.cc
+++ b/src/tasktree.cc
@@ -70,6 +70,8 @@ TaskIdList TaskTree::insertAsChild(taskid_t parentid, taskid_t id, task_t data)
}
finish:
+ printf("Child %d added to %d, affecting %d nodes\n",
+ id, parentid, affectedNodes.size());
return affectedNodes;
}
@@ -79,6 +81,18 @@ TaskIdList TaskTree::remove(taskid_t id)
WARN(tasktree, "Feature not implemneted yet\n");
TaskIdList affectedNodes;
return affectedNodes;
+
+ /*
+ try {
+ node_t* node = id2node.at(id);
+
+ }
+ catch (std::exception& e) {
+ throw std::exception();
+ }
+
+ return affectedNodes;
+ */
}
TaskIdList TaskTree::move(taskid_t id, taskid_t toid)