summaryrefslogtreecommitdiff
path: root/src/muniad.cc
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2012-02-23 10:34:23 +0100
committerJonas Suhr Christensen <jsc@umbraculum.org>2012-02-23 10:34:23 +0100
commit3cbbce9bbe2e63554d253bca8c313e27a16c9b12 (patch)
treebda12b8eb1164606baac9367fe720cc172c6a481 /src/muniad.cc
parent93890eace0b7a35cb597272e7449b1e0ee533a73 (diff)
Implemented add and remove.
Implemented task list on new connection.
Diffstat (limited to 'src/muniad.cc')
-rw-r--r--src/muniad.cc144
1 files changed, 127 insertions, 17 deletions
diff --git a/src/muniad.cc b/src/muniad.cc
index f5d4d6a..e349e7c 100644
--- a/src/muniad.cc
+++ b/src/muniad.cc
@@ -31,6 +31,7 @@
#include <getopt.h>
#include <string.h>
#include <sys/time.h>
+#include <list>
#include <libwebsockets.h>
@@ -71,14 +72,17 @@ static int callback_http(struct libwebsocket_context * context,
case LWS_CALLBACK_HTTP:
fprintf(stderr, "serving HTTP URI %s\n", (char *)in);
+// add favicon later
if(in && strcmp((const char *)in, "/favicon.ico") == 0) {
if(libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/favicon.ico", "image/x-icon"))
fprintf(stderr, "Failed to send favicon\n");
break;
}
+
/* send the script... when it runs it'll start websockets */
- if (libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/test.html", "text/html"))
+// if (libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/test.html", "text/html"))
+ if (libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/munia.html", "text/html"))
fprintf(stderr, "Failed to send HTTP file\n");
break;
@@ -184,21 +188,35 @@ Protocol:
Server -> client:
update [id] [title] [description];
move [id] [x] [y];
- add [title] [description];
+ add [id] [title] [description] [x] [y];
+ del [id]
Client -> server:
update [id] [title] [description];
move [id] [x] [y];
- add [id] [title] [description] [x] [y];
+ add [title] [description] [x] [y];
+ del [id]
title and description are " encapsulated utf-8 string with " escaped with a backslash.
x and y are integers as strings
id are an integer as a string
*/
+struct task {
+ int x, y;
+ int id;
+ std::string title;
+ std::string desc;
+};
+typedef std::list<struct libwebsocket*> ClientList;
+typedef std::list<struct task> TaskList;
+
+static TaskList tasklist;
+
static struct a_message ringbuffer[MAX_MESSAGE_QUEUE];
static int ringbuffer_head;
-
+static ClientList clientlist;
+static int id_count = 0;
static int callback_lws_mirror(struct libwebsocket_context * context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason,
@@ -207,6 +225,8 @@ static int callback_lws_mirror(struct libwebsocket_context * context,
int n;
struct per_session_data__lws_mirror *pss = (struct per_session_data__lws_mirror *)user;
+ printf("Current number of clients: %d\n", clientlist.size());
+
switch (reason) {
case LWS_CALLBACK_ESTABLISHED:
@@ -214,12 +234,35 @@ static int callback_lws_mirror(struct libwebsocket_context * context,
fprintf(stderr, "callback_lws_mirror: LWS_CALLBACK_ESTABLISHED\n");
pss->ringbuffer_tail = ringbuffer_head;
pss->wsi = wsi;
+ clientlist.push_back(wsi);
+
+ // send all current tasks
+ char buf[32];
+ std::string init_str;
+ TaskList::iterator it;
+ for(it = tasklist.begin(); it != tasklist.end(); it++) {
+ struct task 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);
+ }
+
+ libwebsocket_write(wsi, (unsigned char*) init_str.data(), init_str.length(), LWS_WRITE_TEXT);
+
}
break;
+ case LWS_CALLBACK_CLOSED:
+ {
+ printf("Closing connection\n");
+ clientlist.remove(wsi);
+ }
+
case LWS_CALLBACK_SERVER_WRITEABLE:
{
printf("LWS_CALLBACK_SERVER_WRITEABLE\n");
+
if(close_testing) break;
if(pss->ringbuffer_tail != ringbuffer_head) {
@@ -258,31 +301,98 @@ static int callback_lws_mirror(struct libwebsocket_context * context,
std::string data;
data.append((char*)in, len);
+
+ struct a_message &msg = ringbuffer[ringbuffer_head];
+ if(msg.payload) {
+ free(msg.payload);
+ msg.payload = NULL;
+ }
+
+ char* buf = (char*) malloc(1024);
+ int buf_len = 0;
+
std::string cmd = data.substr(0, data.find(' '));
printf("Cmd: %s\n", cmd.c_str());
if(cmd == "add") {
- printf("Add\n");
+ 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;
+ }
+ }
+
+ if(!id_found) {
+ printf("\t!!!Could not locate task with id %d\n", id);
+ }
+
+ buf_len = sprintf(buf, "del %d;", id);
+ printf("Deleting task: %s\n", buf);
} else if(cmd == "move") {
printf("Move\n");
} else if(cmd == "update") {
printf("Update\n");
- } else { // unknown command
-
+ } else if(cmd == "d") {
+ memcpy(buf, in, len);
+ buf_len = len;
}
-
- struct a_message &msg = ringbuffer[ringbuffer_head];
- if(msg.payload) {
- free(msg.payload);
- msg.payload = NULL;
+ else { // unknown command
+ printf("Unknown command :(\n");
+ break;
}
- msg.payload = malloc(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING);
- msg.len = len;
- memcpy((char *)msg.payload + LWS_SEND_BUFFER_PRE_PADDING, in, len);
+// msg.payload = malloc(LWS_SEND_BUFFER_PRE_PADDING + len + LWS_SEND_BUFFER_POST_PADDING);
+// msg.len = len;
+// memcpy((char *)msg.payload + LWS_SEND_BUFFER_PRE_PADDING, in, len);
+// if(ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) ringbuffer_head = 0;
+// else ringbuffer_head++;
+
+ msg.payload = malloc(LWS_SEND_BUFFER_PRE_PADDING + buf_len
+ + LWS_SEND_BUFFER_POST_PADDING);
+ msg.len = buf_len;
+ memcpy((char *)msg.payload + LWS_SEND_BUFFER_PRE_PADDING,
+ buf, buf_len);
if(ringbuffer_head == (MAX_MESSAGE_QUEUE - 1)) ringbuffer_head = 0;
else ringbuffer_head++;
-
+
+
if(((ringbuffer_head - pss->ringbuffer_tail) % MAX_MESSAGE_QUEUE) > (MAX_MESSAGE_QUEUE - 10))
libwebsocket_rx_flow_control(wsi, 0);
@@ -355,7 +465,7 @@ int main(int argc, char **argv)
char interface_name[128] = "";
const char * interface = NULL;
#ifdef LWS_NO_FORK
- unsigned int oldus = 0;
+// unsigned int oldus = 0;
#endif
fprintf(stderr, "libwebsockets test server\n"