summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2012-05-04 10:46:40 +0200
committerJonas Suhr Christensen <jsc@umbraculum.org>2012-05-04 10:46:40 +0200
commit3ad0a2d6f49c6f5371e8c3b0b5c502aea58aeb75 (patch)
treee9b7e4ca4d5a786abda065deb2868fe8132aec30 /src
parentd0bd8f44997150bcb06137258512654c75e79b49 (diff)
parentde7c3ed2ccadcd179449dee6b8e38f6d7d2df3c6 (diff)
Merge branch 'master' of https://git.oftal.dk/munia
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am2
-rw-r--r--src/connectionhandler.cc21
-rw-r--r--src/connectionhandler.h3
-rw-r--r--src/messagehandler.cc15
-rw-r--r--src/munia_proto.cc5
-rw-r--r--src/muniad.cc10
-rw-r--r--src/taskmanager.cc3
-rw-r--r--src/taskmanager.h4
-rw-r--r--src/testclient.cc208
-rw-r--r--src/testclient.h30
10 files changed, 273 insertions, 28 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 9fccb45..a56ae39 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -19,6 +19,7 @@ muniad_SOURCES = \
task.cc \
taskmanager.cc \
tasktree.cc \
+ testclient.cc \
xml_encode_decode.cc \
xmlparser.cc
@@ -35,6 +36,7 @@ EXTRA_DIST = \
task.h \
taskmanager.h \
tasktree.h \
+ testclient.h \
xml_encode_decode.h \
xmlparser.h
diff --git a/src/connectionhandler.cc b/src/connectionhandler.cc
index 56c465f..c8881a3 100644
--- a/src/connectionhandler.cc
+++ b/src/connectionhandler.cc
@@ -65,16 +65,13 @@ void ConnectionHandler::unobserve(clientid_t clientid, taskid_t taskid)
connlist[clientid].erase(taskid);
}
-std::list<std::pair<clientid_t, taskid_t> >
-ConnectionHandler::observerlist(TaskIdList tasks)
+ObserverList ConnectionHandler::observerlist(TaskIdList tasks)
{
printf("Observerlist request\n");
- std::list<std::pair<clientid_t, taskid_t> > clients;
+ ObserverList clients;
for(TaskIdList::iterator i = tasks.begin(); i != tasks.end(); i++) {
taskid_t tid = *i;
- // std::set<clientid_t> clientList;
- // std::map<clientid_t, taskid_t>
printf("Locating observers of node %d\n", tid);
for(ConnectionList::iterator ci = connlist.begin();
@@ -118,7 +115,7 @@ h.observe((clientid_t)3, (taskid_t)3);
{
TaskIdList tasks;
tasks.push_back((taskid_t)1);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_TRUE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_TRUE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -128,7 +125,7 @@ h.observe((clientid_t)3, (taskid_t)3);
{
TaskIdList tasks;
tasks.push_back((taskid_t)3);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_FALSE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_FALSE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -138,7 +135,7 @@ h.observe((clientid_t)3, (taskid_t)3);
{
TaskIdList tasks;
tasks.push_back((taskid_t)4);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_FALSE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_FALSE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -150,7 +147,7 @@ h.observe((clientid_t)3, (taskid_t)3);
tasks.push_back((taskid_t)1);
tasks.push_back((taskid_t)2);
tasks.push_back((taskid_t)3);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_TRUE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_TRUE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -163,7 +160,7 @@ h.close((clientid_t)1);
tasks.push_back((taskid_t)1);
tasks.push_back((taskid_t)2);
tasks.push_back((taskid_t)3);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_FALSE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_TRUE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -176,7 +173,7 @@ h.close((clientid_t)2);
tasks.push_back((taskid_t)1);
tasks.push_back((taskid_t)2);
tasks.push_back((taskid_t)3);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_FALSE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_FALSE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
@@ -189,7 +186,7 @@ h.close((clientid_t)3);
tasks.push_back((taskid_t)1);
tasks.push_back((taskid_t)2);
tasks.push_back((taskid_t)3);
- std::set<clientid_t> clst = h.observerlist(tasks);
+ ObserverList clst = h.observerlist(tasks);
TEST_FALSE(clst.find((clientid_t)1) != clst.end(), "Got client 1?");
TEST_FALSE(clst.find((clientid_t)2) != clst.end(), "Got client 2?");
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index b09e944..7779b1f 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -39,6 +39,7 @@
typedef struct libwebsocket* clientid_t;
typedef std::map<clientid_t, std::set<taskid_t> > ConnectionList;
+typedef std::list<std::pair<clientid_t, taskid_t> > ObserverList;
class ConnectionHandler {
public:
@@ -50,7 +51,7 @@ public:
void unobserve(clientid_t clientid, taskid_t taskid);
- std::list<std::pair<clientid_t, taskid_t> > observerlist(TaskIdList tasklist);
+ ObserverList observerlist(TaskIdList tasklist);
private:
ConnectionList connlist;
diff --git a/src/messagehandler.cc b/src/messagehandler.cc
index 89b476a..18d5fed 100644
--- a/src/messagehandler.cc
+++ b/src/messagehandler.cc
@@ -28,10 +28,9 @@
#include "messagehandler.h"
#include "debug.h"
+// Defines global task_manager object
#include "taskmanager.h"
-TaskManager taskman;
-
MessageList handle_msg(MessageList msgList, clientid_t wsi) {
MessageList::iterator it;
@@ -44,11 +43,11 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) {
case cmd::add:
{
INFO(messagehandler, "Handling add command\n");
- task_t t = taskman.createTask();
+ task_t t = task_manager.createTask();
t.title = m.add.title;
t.desc = m.add.desc;
try {
- it->nodes = taskman.addTask(t, m.add.parentid);
+ it->nodes = task_manager.addTask(t, m.add.parentid);
}
catch (std::exception& e) {
DEBUG(messagehandler, "Error adding task\n");
@@ -59,7 +58,7 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) {
{
INFO(messagehandler, "Handling del command\n");
try {
- it->nodes = taskman.deleteTask(m.del.id);
+ it->nodes = task_manager.deleteTask(m.del.id);
}
catch (std::exception& e) {
DEBUG(messagehandler, "Error deleting task\n");
@@ -70,7 +69,7 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) {
{
INFO(messagehandler, "Handling move command\n");
try {
- it->nodes = taskman.moveTask(m.move.id, m.move.parentid);
+ it->nodes = task_manager.moveTask(m.move.id, m.move.parentid);
}
catch (std::exception& e) {
DEBUG(messagehandler, "Error moving task\n");
@@ -90,7 +89,7 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) {
task_t t;
t.title = m.update.title;
t.desc = m.update.desc;
- it->nodes = taskman.updateTask(m.update.id, t);
+ it->nodes = task_manager.updateTask(m.update.id, t);
}
catch (std::exception& e) {
DEBUG(messagehandler, "Error updating task\n");
@@ -101,7 +100,7 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) {
WARN(messagehandler, "!!! Unknown command\n");
break;
}
- taskman.tree.toStdOut();
+ task_manager.tree.toStdOut();
printf("%d affected nodes registered\n", m.nodes.size());
}
diff --git a/src/munia_proto.cc b/src/munia_proto.cc
index b801009..a91c204 100644
--- a/src/munia_proto.cc
+++ b/src/munia_proto.cc
@@ -38,10 +38,9 @@
#include "messagehandler.h"
#include "connectionhandler.h"
+// Defines global task_manager object
#include "taskmanager.h"
-extern TaskManager taskman;
-
#if 0
static void dump_handshake_info(struct lws_tokens *lwst)
{
@@ -89,7 +88,7 @@ int callback_lws_task(struct libwebsocket_context * context,
enum libwebsocket_callback_reasons reason,
void *user, void *in, size_t len)
{
- taskman.tree.toStdOut();
+ task_manager.tree.toStdOut();
printf("Callback on %p\n", wsi);
diff --git a/src/muniad.cc b/src/muniad.cc
index 34f5c6a..741241a 100644
--- a/src/muniad.cc
+++ b/src/muniad.cc
@@ -76,9 +76,13 @@ int main(int argc, char **argv)
fprintf(stderr, "Munia test server\n"
"(C) Copyright 2012 Bent Bisballe Nyeng (deva@aasimon.org)\n"
- " & Jonas Suhr Christensen (jsc@umbraculum.org)\n"
- "Licensed under LGPL2.1\n");
-
+ " & Jonas Suhr Christensen (jsc@umbraculum.org)\n"
+ "License GPLv3+: GNU GPL version 3 or later "
+ "<http://gnu.org/licenses/gpl.html>\n"
+ "\n"
+ "This is free software; you are free to change and redistribute it.\n"
+ "There is NO WARRANTY, to the extent permitted by law.\n");
+
while(n >= 0) {
n = getopt_long(argc, argv, "ci:khsp:", options, NULL);
if(n < 0) continue;
diff --git a/src/taskmanager.cc b/src/taskmanager.cc
index a197a79..f757694 100644
--- a/src/taskmanager.cc
+++ b/src/taskmanager.cc
@@ -29,6 +29,9 @@
#include <stdio.h>
+// Global TaskManager object.
+TaskManager task_manager;
+
#define ROOT_ID 0
#define LOSTFOUND_ID 1
#define FINISHED_ID 2
diff --git a/src/taskmanager.h b/src/taskmanager.h
index d6a418b..47c6c10 100644
--- a/src/taskmanager.h
+++ b/src/taskmanager.h
@@ -46,7 +46,7 @@ public:
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);
-
+
TaskTree tree;
private:
@@ -55,6 +55,8 @@ private:
taskid_t idCount;
};
+extern TaskManager task_manager;
+
#if 0
/*
diff --git a/src/testclient.cc b/src/testclient.cc
new file mode 100644
index 0000000..5df0d56
--- /dev/null
+++ b/src/testclient.cc
@@ -0,0 +1,208 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * testclient.cc
+ *
+ * Thu May 3 10:06:49 CEST 2012
+ * Copyright 2012 Bent Bisballe Nyeng
+ * deva@aasimon.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 "testclient.h"
+
+#ifdef TEST_TESTCLIENT
+//deps:
+//cflags: $(LIBWEBSOCKETS_CFLAGS)
+//libs: $(LIBWEBSOCKETS_LIBS)
+#include "test.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <string.h>
+
+#include <libwebsockets.h>
+
+struct test {
+ const char *command;
+ const char *result;
+ bool success;
+};
+
+enum demo_protocols {
+ PROTOCOL_TASK,
+};
+
+struct test *current_test = NULL;
+
+static int callback_task(struct libwebsocket_context *me,
+ struct libwebsocket *wsi,
+ enum libwebsocket_callback_reasons reason,
+ void *user, void *in, size_t len)
+{
+ unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 +
+ LWS_SEND_BUFFER_POST_PADDING];
+ int l;
+
+ switch (reason) {
+
+ case LWS_CALLBACK_CLOSED:
+ fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
+ // wsi_mirror = NULL;
+ break;
+
+ case LWS_CALLBACK_CLIENT_ESTABLISHED:
+
+ /*
+ * start the ball rolling,
+ * LWS_CALLBACK_CLIENT_WRITEABLE will come next service
+ */
+
+ libwebsocket_callback_on_writable(me, wsi);
+ break;
+
+ case LWS_CALLBACK_CLIENT_RECEIVE:
+ fprintf(stderr, "rx %d '%s'\n", (int)len, (char *)in);
+ current_test->success = strncmp(current_test->result, (char*)in, len) == 0;
+ current_test++;
+ break;
+
+ case LWS_CALLBACK_CLIENT_WRITEABLE:
+
+
+ l = sprintf((char *)&buf[LWS_SEND_BUFFER_PRE_PADDING],
+ "%s", current_test->command);
+
+ libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING],
+ l, LWS_WRITE_TEXT);
+
+ if(current_test->result == NULL) current_test++;
+
+ libwebsocket_callback_on_writable(me, wsi);
+
+ /*
+ * without at least this delay, we choke the browser
+ * and the connection stalls, despite we now take care about
+ * flow control
+ */
+
+ //usleep(200);
+ sleep(1);
+ break;
+
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+static struct libwebsocket_protocols protocols[] = {
+ { "lws-task-protocol", callback_task, 0, },
+ { NULL, NULL, 0 }
+};
+
+int client(const char *address, int port, struct test tests[])
+{
+ current_test = &tests[0];
+
+ struct libwebsocket_context *context;
+ struct libwebsocket *wsi_task = NULL;
+ int ietf_version = -1; // latest
+
+ context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,
+ protocols,
+ libwebsocket_internal_extensions,
+ NULL, NULL, -1, -1, 0);
+ if (context == NULL) {
+ fprintf(stderr, "Creating libwebsocket context failed\n");
+ return 1;
+ }
+
+
+ /*
+ * sit there servicing the websocket context to handle incoming
+ * packets, and drawing random circles on the mirror protocol websocket
+ */
+ int n = 1;
+ while(n >= 0 && current_test->command) {
+ n = libwebsocket_service(context, 1000);
+
+ if (wsi_task == NULL) {
+ wsi_task = libwebsocket_client_connect(context, address, port,
+ 0, "/", address, address,
+ protocols[PROTOCOL_TASK].name,
+ ietf_version);
+
+ if (wsi_task == NULL) {
+ fprintf(stderr, "libwebsocket task connect failed\n");
+ return -1;
+ }
+
+ } else {
+
+ /*
+ fprintf(stderr, "closing mirror session\n");
+ libwebsocket_close_and_free_session(context,
+ wsi_mirror, LWS_CLOSE_STATUS_GOINGAWAY);
+ */
+ }
+ }
+
+ fprintf(stderr, "Exiting\n");
+
+ libwebsocket_context_destroy(context);
+
+ return 0;
+}
+
+TEST_BEGIN;
+
+#define BASE "0 create 0 -1; 0 update 0 \"root\"; "
+static struct test tests[] = {
+ { "observe 0", BASE"", false },
+ { "unobserve 0", "0 remove 0", false },
+ /*
+ { "create 0", "", false },
+ { "observe 0", BASE"0 create 0 5", false },
+ { "unobserve 0", NULL, false },
+ { "observe 0", BASE"0 add title description 5", false },
+ { "add title2 description 0", BASE"0 add title2 description 6", false },
+ { "unobserve 0", NULL, false },
+ { "observe 0", BASE"0 add title description 5; add title description 6", false },
+ { "unobserve 0", NULL, false },
+ */
+ { NULL, NULL, false }
+};
+
+client("holger", 10001, tests);
+
+struct test *t = &tests[0];
+while(t->command) {
+ TEST_TRUE(t->success, "%s", t->command);
+ t++;
+}
+// TODO: Put some testcode here (see test.h for usable macros).
+//TEST_TRUE(false, "No tests yet!");
+
+TEST_END;
+
+#endif/*TEST_TESTCLIENT*/
diff --git a/src/testclient.h b/src/testclient.h
new file mode 100644
index 0000000..d92bd9c
--- /dev/null
+++ b/src/testclient.h
@@ -0,0 +1,30 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * testclient.h
+ *
+ * Thu May 3 10:06:49 CEST 2012
+ * Copyright 2012 Bent Bisballe Nyeng
+ * deva@aasimon.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_TESTCLIENT_H__
+#define __MUNIA_TESTCLIENT_H__
+#endif/*__MUNIA_TESTCLIENT_H__*/