From adab7fc5a061624ad931fee4f95de09f78ace91d Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 11 Jan 2013 15:52:08 +0100 Subject: Fix unobserve bug. Change more printf's into hugin syntax. --- proto.js | 46 +++++++++++++++++++++++++++++++++++++----- src/http.cc | 14 +++++++------ src/messagehandler.cc | 8 +++++++- src/messageparser.cc | 56 ++++++++++++++++++++++++++++++++++----------------- src/munia_proto.cc | 17 ++++++++-------- 5 files changed, 103 insertions(+), 38 deletions(-) diff --git a/proto.js b/proto.js index eb63afd..9be3101 100644 --- a/proto.js +++ b/proto.js @@ -162,8 +162,14 @@ try { } if(cmd == "remove") { - var task = document.getElementById(createId(observeid, id)); - task.parentNode.removeChild(task); + var task = document.getElementById(createId(observeid, id)); + var parent = task.parentNode; + parent.removeChild(task); + + var board = document.getElementById("board_" + observeid); + if(board.childNodes.length == 0) { + board.parentNode.removeChild(board); + } } else if(cmd == "move") { var parent_id = msg[3]; @@ -189,12 +195,30 @@ try { task.setAttribute("draggable", true); task.setAttribute("ondragstart", "drag(this, event)"); task.setAttribute("title", id); - + task.id = createId(observeid, id); + var observe_button = document.createElement("div"); + observe_button.name = "observe_button"; + observe_button.setAttribute("onclick", "observeMe(this, event)"); + observe_button.setAttribute("title", id); + observe_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid green 2px; cursor: pointer;"); + var txt_plus = document.createTextNode("+"); + observe_button.appendChild(txt_plus); + task.appendChild(observe_button); + + var unobserve_button = document.createElement("div"); + unobserve_button.name = "unobserve_button"; + unobserve_button.setAttribute("onclick", "unobserveMe(this, event)"); + unobserve_button.setAttribute("title", id); + unobserve_button.setAttribute("style", "float: left; display: inline-box; width:14px; height: 14px; border: solid red 2px; cursor: pointer;"); + var txt_minus = document.createTextNode("-"); + unobserve_button.appendChild(txt_minus); + task.appendChild(unobserve_button); + var txtdiv = document.createElement("div"); txtdiv.id = createId(observeid, id) + "_txt"; - var txt = document.createTextNode(""); + var txt = document.createTextNode(id + ": (missing title)"); txtdiv.appendChild(txt); task.appendChild(txtdiv); @@ -214,7 +238,7 @@ try { var txtdiv = document.getElementById(createId(observeid, id) + "_txt"); txtdiv.removeChild(txtdiv.firstChild); - var txt = document.createTextNode(value); + var txt = document.createTextNode(id + ": " + value); txtdiv.appendChild(txt); } if(name == "colour") { @@ -261,6 +285,18 @@ function drop(target, e) { socket_task.send("move " + idFromStr(id) + " " + idFromStr(target.id) + ";"); } +function observeMe(target, e) +{ + e.stopPropagation(); + socket_task.send("observe "+target.title+";"); +} + +function unobserveMe(target, e) +{ + e.stopPropagation(); + socket_task.send("unobserve "+target.title+";"); +} + function showHideChildren(target, e) { e.stopPropagation(); diff --git a/src/http.cc b/src/http.cc index fe37d76..8aa83c3 100644 --- a/src/http.cc +++ b/src/http.cc @@ -30,6 +30,8 @@ #include #include +#include "hugin.hpp" + int callback_http(struct libwebsocket_context * context, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user, @@ -40,14 +42,14 @@ int callback_http(struct libwebsocket_context * context, switch(reason) { case LWS_CALLBACK_HTTP: - fprintf(stderr, "serving HTTP URI %s\n", (char *)in); + DEBUG(httpd,"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"); + DEBUG(httpd,"Failed to send favicon\n"); break; } @@ -56,7 +58,7 @@ int callback_http(struct libwebsocket_context * context, if(libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/proto.js", "text/javascript")) - fprintf(stderr, "Failed to send javascript\n"); + DEBUG(httpd,"Failed to send javascript\n"); break; } @@ -65,7 +67,7 @@ int callback_http(struct libwebsocket_context * context, if(libwebsockets_serve_http_file(wsi, LOCAL_RESOURCE_PATH"/munia.html", "text/html")) - fprintf(stderr, "Failed to send HTTP file\n"); + DEBUG(httpd,"Failed to send HTTP file\n"); break; /* @@ -82,8 +84,8 @@ int callback_http(struct libwebsocket_context * context, sizeof(client_name), client_ip, sizeof(client_ip)); - fprintf(stderr, "Received network connect from %s (%s)\n", - client_name, client_ip); + DEBUG(httpd,"Received network connect from %s (%s)\n", + client_name, client_ip); /* if we returned non-zero from here, we kill the connection */ break; diff --git a/src/messagehandler.cc b/src/messagehandler.cc index 0178d76..d3c3420 100644 --- a/src/messagehandler.cc +++ b/src/messagehandler.cc @@ -149,6 +149,7 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) { case cmd::unobserve: //connection_handler.unobserve(wsi, m.observe.id); outmsgs.push_back(m); + DEBUG(handler, "unobserve: %d\n", m.unobserve.id); break; case cmd::update: @@ -164,8 +165,13 @@ MessageList handle_msg(MessageList msgList, clientid_t wsi) { } } break; + + case cmd::error: + ERR(messagehandler, "An error occurred.\n"); + break; + default: - WARN(messagehandler, "!!! Unknown command\n"); + WARN(messagehandler, "!!! Unknown command %d\n", m.cmd); break; } task_manager.tree.toStdOut(); diff --git a/src/messageparser.cc b/src/messageparser.cc index 9e5e7ab..4f047d9 100644 --- a/src/messageparser.cc +++ b/src/messageparser.cc @@ -50,34 +50,32 @@ inline static void parse_into_msg_tokens(std::string& data, switch(ch) { case '\"': - if(prev_ch != '\\') + if(prev_ch != '\\') { inside_quote = !inside_quote; - else { + } else { token += ch; } break; case ' ': if(inside_quote) { - //DEBUG(msgparser, "Appending %c\n", ch); + DEBUG(msgparser, "Appending %c\n", ch); token += ch; - } - else { + } else { if(token.empty()) continue; // skip multiple white spaces and pre white space -// DEBUG(msgparser, "Adding token %s\n", token.c_str()); + DEBUG(msgparser, "Adding token %s\n", token.c_str()); tokenVector.push_back(token); token.clear(); - } + } break; case ';': if(inside_quote) { token += ch; - } - else { + } else { tokenVector.push_back(token); msgTokensList.push_back(tokenVector); tokenVector.clear(); token.clear(); - } + } break; default: token += ch; @@ -86,15 +84,31 @@ inline static void parse_into_msg_tokens(std::string& data, prev_ch = ch; } -// if(!token.empty()) { + if(token != "") { tokenVector.push_back(token); token.clear(); -// } + } if(!tokenVector.empty()) { msgTokensList.push_back(tokenVector); tokenVector.clear(); } + + + // Debug: print tokens + DEBUG(parser, "tokenlists: %d\n", msgTokensList.size()); + MsgTokensList::iterator j = msgTokensList.begin(); + while(j != msgTokensList.end()) { + DEBUG(parser, " tokens: %d\n", j->size()); + int idx = 0; + TokenVector::iterator i = j->begin(); + while(i != j->end()) { + DEBUG(parser, " token[%d]: '%s'\n", idx++, i->c_str()); + i++; + } + j++; + } + } static taskid_t getId(std::map &sym, std::string token) @@ -104,7 +118,13 @@ static taskid_t getId(std::map &sym, std::string token) isnumeric &= (token[i] >= '0' && token[i] <= '9'); } - if(isnumeric) return atoi(token.c_str()); + DEBUG(getid, "======= %d %s\n", isnumeric, token.c_str()); + + if(isnumeric) { + taskid_t id = atoi(token.c_str()); + DEBUG(getid, "======= id: %d\n", id); + return id; + } if(sym.find(token) == sym.end()) sym[token] = task_manager.createId(); return sym[token]; @@ -129,7 +149,8 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, message_t m; - DEBUG(msgparser, "t[%d] : %s, running in clientmode: %d\n", origin, t[origin].c_str(), clientmode); + DEBUG(msgparser, "t[%d] : %s, running in clientmode: %d\n", + origin, t[origin].c_str(), clientmode); // DEBUG(msgparser, "%d\n", t.size()); @@ -141,8 +162,6 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, else if(t[origin] == "update") m.cmd = cmd::update; else m.cmd = cmd::error; -// DEBUG(msgparser, "Number of tokens %d\n", t.size()); - switch(m.cmd) { case cmd::observe: { if(t.size() != origin+1+1) { @@ -158,6 +177,7 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, continue; } m.unobserve.id = getId(sym, t[origin+1]);//atoi(t[origin+1].c_str()); + DEBUG(msgparser, "unobserve @ %d\n", m.unobserve.id); break; } case cmd::create: { @@ -214,7 +234,7 @@ inline static void create_msg_list(MsgTokensList& msgTokensList, } MessageList parse_msg(std::string data) { - DEBUG(msgparser, "Parsing: %s\n", data.c_str()); + DEBUG(msgparser, "Parsing: '%s'\n", data.c_str()); MsgTokensList msgTokensList; parse_into_msg_tokens(data, msgTokensList); @@ -226,7 +246,7 @@ MessageList parse_msg(std::string data) { } MessageList parse_msg_client(std::string data) { - DEBUG(msgparser, "Parsing: \n%s\n", data.c_str()); + DEBUG(msgparser, "Parsing: '%s'\n", data.c_str()); MsgTokensList msgTokensList; parse_into_msg_tokens(data, msgTokensList); diff --git a/src/munia_proto.cc b/src/munia_proto.cc index 2a42884..8311f90 100644 --- a/src/munia_proto.cc +++ b/src/munia_proto.cc @@ -76,9 +76,9 @@ static void dump_handshake_info(struct lws_tokens *lwst) for (n = 0; n < WSI_TOKEN_COUNT; n++) { if (lwst[n].token == NULL || lwst[n].token_len == 0) continue; - fprintf(stderr, " %s = ", token_names[n]); + DEBUG(proto," %s = ", token_names[n]); if(fwrite(lwst[n].token, 1, lwst[n].token_len, stderr)) {} - fprintf(stderr, "\n"); + DEBUG(proto,"\n"); } } #endif @@ -166,7 +166,7 @@ int callback_lws_task(struct libwebsocket_context * context, LWS_SEND_BUFFER_PRE_PADDING, LWS_WRITE_TEXT); if(n < 0) { - fprintf(stderr, "ERROR writing to socket"); + DEBUG(proto,"ERROR writing to socket"); exit(1); } } @@ -177,7 +177,7 @@ int callback_lws_task(struct libwebsocket_context * context, case LWS_CALLBACK_BROADCAST: printf("LWS_CALLBACK_BROADCAST\n"); n = libwebsocket_write(wsi, (unsigned char*)in, len, LWS_WRITE_TEXT); - if (n < 0) fprintf(stderr, "task write failed\n"); + if (n < 0) DEBUG(proto, "task write failed\n"); break; */ @@ -231,12 +231,12 @@ int callback_lws_task(struct libwebsocket_context * context, } } else if(omi->cmd == cmd::unobserve) { - connection_handler.unobserve(wsi, omi->observe.id); + connection_handler.unobserve(wsi, omi->unobserve.id); TaskIdList ids; try { - ids = task_manager.subTasks(omi->observe.id); + ids = task_manager.subTasks(omi->unobserve.id); } catch(...) { - DEBUG(proto, "No such node %d\n", omi->observe.id); + DEBUG(proto, "No such node %d\n", omi->unobserve.id); omi++; continue; } @@ -245,8 +245,9 @@ int callback_lws_task(struct libwebsocket_context * context, task_t task = task_manager.task(*id); message_t removemsg = create_msg_remove(task); - removemsg.tid = omi->observe.id; + removemsg.tid = omi->unobserve.id; msgqueue[wsi].push_back(removemsg); + DEBUG(unobserve, "remove id: %d\n", *id); id++; } -- cgit v1.2.3