From d4b1c3fd3a74779de65a3e13c33c9b0da781a243 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 12 Mar 2012 01:21:45 +0100 Subject: [PATCH] now the session livetime is shown with a bar, as well as the current session id. These are updated on reload or ajax call. --- assets/html/main.html | 28 +++++++++++++++ assets/js/serverval.js | 8 +++-- assets/js/session.js | 69 +++++++++++++++++++++++++++++++++++++ assets/style/common.css | 17 +++++++-- include/http/response.h | 2 ++ src/Makefile.am | 3 +- src/http/response/session.c | 69 +++++++++++++++++++++++++++++++++++++ src/http/worker/process.c | 31 ++++++++++------- 8 files changed, 209 insertions(+), 18 deletions(-) create mode 100644 assets/js/session.js create mode 100644 src/http/response/session.c diff --git a/assets/html/main.html b/assets/html/main.html index 3a60027..b9d14d5 100644 --- a/assets/html/main.html +++ b/assets/html/main.html @@ -7,18 +7,36 @@ + @@ -26,7 +44,17 @@ +
+ Session:
+ +
+
+
+ +
+
Value created at:
diff --git a/assets/js/serverval.js b/assets/js/serverval.js index 3753157..201583d 100644 --- a/assets/js/serverval.js +++ b/assets/js/serverval.js @@ -33,13 +33,17 @@ ServerVal.prototype.show = function() ServerVal.prototype.start = function() { - this.interval = setInterval($.proxy(this.process, this), 1000); + if (null === this.interval) { + this.interval = setInterval($.proxy(this.process, this), 1000); + } } ServerVal.prototype.process = function() { if (0 >= this.vnext) { - $.getJSON("/randval/", $.proxy(this.loadJSON, this)); + $.getJSON("/randval/", $.proxy(this.loadJSON, this)) + .error($.proxy(function() {this.stop();}, this)); + $.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess)); } else { diff --git a/assets/js/session.js b/assets/js/session.js new file mode 100644 index 0000000..c195232 --- /dev/null +++ b/assets/js/session.js @@ -0,0 +1,69 @@ +function Session(sId) +{ + this.eSid = $(sId + " span"); + this.canvas = $(sId + " canvas").get(0); + this.context = this.canvas.getContext("2d"); + + this.id = "none" + this.timeout = 0; + this.timeleft = 0; + this.interval = null; + + this.draw(); +} + +Session.prototype.loadJSON = function(data) +{ + this.id = ("0" == data.id)? "none" : data.id; + this.timeout = data.timeout; + this.timeleft = data.timeleft; + + this.draw(); + if (0 < this.timeleft) + this.start(); +} + +Session.prototype.draw = function() +{ + this.eSid.empty().append(this.id); + + this.context.fillStyle = "rgb(255, 0, 0)"; + this.context.fillRect(0, 0, this.canvas.width, this.canvas.height); + + this.context.fillStyle = "rgb(0, 255, 0)"; + this.context.fillRect(0, 0, + this.canvas.width / this.timeout * this.timeleft, + this.canvas.height); +} + +Session.prototype.start = function() +{ + if (null === this.interval) { + this.interval = setInterval($.proxy(this.process, this), 1000); + } +} + +Session.prototype.process = function() +{ + if (0 >= this.timeleft) { + this.stop(); + } + + else { + this.timeleft--; + this.draw(); + } +} + +Session.prototype.stop = function() +{ + clearInterval(this.interval); + this.interval = null; + this.id = "none"; + this.timeout = 0; + this.timeleft = 0; + + this.draw(); +} + +// vim: set ts=4 sw=4: diff --git a/assets/style/common.css b/assets/style/common.css index 5f54fe0..9a933c6 100644 --- a/assets/style/common.css +++ b/assets/style/common.css @@ -8,14 +8,22 @@ div#randval { border-radius: 10px; } -div.hide#randval { - top: -500px; +div#login { + position: fixed; +} + +div.hide { + top: -500px !important; } .small { font-size: small; } +.x-small { + font-size: x-small; +} + ul#menu { list-style: none inside; margin: 0px; @@ -38,4 +46,9 @@ ul#menu li { float: left; margin-right: 1px; } + +div#sessinfo canvas { + border: 1px solid black; +} + /* vim: set st=4 sw=4: */ diff --git a/include/http/response.h b/include/http/response.h index 67b206e..4a3090b 100644 --- a/include/http/response.h +++ b/include/http/response.h @@ -29,6 +29,7 @@ #include "class.h" #include "http/message.h" +#include "session.h" CLASS(HttpResponse) { @@ -47,6 +48,7 @@ HttpResponse httpResponse403(); HttpResponse httpResponseMe(); HttpResponse httpResponseLoginForm(); HttpResponse httpResponseRandval(time_t, int); +HttpResponse httpResponseSession(Session); HttpResponse httpResponseAsset( const char *, const char *, size_t, diff --git a/src/Makefile.am b/src/Makefile.am index a466572..8834d3c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,7 +30,8 @@ RESP = http/response.c \ http/response/403.c \ http/response/login_form.c \ http/response/asset.c \ - http/response/randval.c + http/response/randval.c \ + http/response/session.c PARSER = http/parser.c \ http/parser/parse.c \ http/parser/new_message.c \ diff --git a/src/http/response/session.c b/src/http/response/session.c new file mode 100644 index 0000000..2075a62 --- /dev/null +++ b/src/http/response/session.c @@ -0,0 +1,69 @@ +/** + * \file + * + * \author Georg Hopp + * + * \copyright + * Copyright (C) 2012 Georg Hopp + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "class.h" +#include "interface/class.h" + +#include "http/response.h" +#include "http/message.h" +#include "http/header.h" +#include "session.h" + +#include "utils/memory.h" + +#define RESP_DATA "{\"id\":\"%lu\",\"timeout\":%d,\"timeleft\":%ld}" + +HttpResponse +httpResponseSession(Session session) +{ + char buffer[200]; + HttpResponse response; + HttpMessage message; + size_t nbuf; + + response = new(HttpResponse, "HTTP/1.1", 200, "OK"); + message = (HttpMessage)response; + + httpHeaderAdd(&(message->header), + new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json"))); + + message->type = HTTP_MESSAGE_BUFFERED; + + nbuf = sprintf(buffer, RESP_DATA, + (NULL != session)? session->id : 0, + (NULL != session)? SESSION_LIVETIME : 0, + (NULL != session)? session->livetime - time(NULL) : 0); + + message->nbody = nbuf; + message->body = malloc(nbuf); + memcpy(message->body, buffer, nbuf); + + return response; +} + +// vim: set ts=4 sw=4: diff --git a/src/http/worker/process.c b/src/http/worker/process.c index db010c3..14fb6dd 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "class.h" #include "interface/class.h" @@ -91,7 +92,7 @@ httpWorkerProcess(HttpWorker this, Stream st) } if (0 == strcmp("POST", request->method)) { - if (0 == strcmp("/me/", request->uri)) { + if (0 == strcmp("/login/", request->uri)) { char * delim = memchr(rmessage->body, '=', rmessage->nbody); char * val; size_t nkey, nval; @@ -108,10 +109,7 @@ httpWorkerProcess(HttpWorker this, Stream st) new(Session, val, nval)); nbuf = sprintf(buffer, "sid=%lu;Path=/", this->session->id); - response = httpWorkerGetAsset( - request, - "./assets/html/main.html", - CSTRA("text/html")); + response = (HttpMessage)httpResponseSession(this->session); httpHeaderAdd( &(response->header), @@ -121,10 +119,6 @@ httpWorkerProcess(HttpWorker this, Stream st) if (0 == strcmp("GET", request->method)) { - if (0 == strcmp("/login/", request->uri)) { - response = (HttpMessage)httpResponseLoginForm(); - } - if (0 == strcmp("/", request->uri)) { response = httpWorkerGetAsset( request, @@ -132,14 +126,18 @@ httpWorkerProcess(HttpWorker this, Stream st) CSTRA("text/html")); } + if (0 == strcmp("/sessinfo/", request->uri)) { + response = (HttpMessage)httpResponseSession(this->session); + } + if (0 == strcmp("/randval/", request->uri)) { - //if (NULL != this->session) { + if (NULL != this->session) { response = (HttpMessage)httpResponseRandval( this->val->timestamp, this->val->value); - //} else { - // response = (HttpMessage)httpResponse403(); - //} + } else { + response = (HttpMessage)httpResponse403(); + } } if (0 == strcmp("/image/me", request->uri)) { @@ -163,6 +161,13 @@ httpWorkerProcess(HttpWorker this, Stream st) CSTRA("text/javascript")); } + if (0 == strcmp("/assets/js/session", request->uri)) { + response = httpWorkerGetAsset( + request, + "./assets/js/session.js", + CSTRA("text/javascript")); + } + if (0 == strcmp("/assets/style/common", request->uri)) { response = httpWorkerGetAsset( request,