Commit d4b1c3fd3a74779de65a3e13c33c9b0da781a243

Authored by Georg Hopp
1 parent 69cfdd07

now the session livetime is shown with a bar, as well as the current session id.…

… These are updated on reload or ajax call.
@@ -7,18 +7,36 @@ @@ -7,18 +7,36 @@
7 <link rel="stylesheet" type="text/css" href="/assets/style/common"> 7 <link rel="stylesheet" type="text/css" href="/assets/style/common">
8 <script type="text/javascript" src="/assets/js/jquery"></script> 8 <script type="text/javascript" src="/assets/js/jquery"></script>
9 <script type="text/javascript" src="/assets/js/serverval"></script> 9 <script type="text/javascript" src="/assets/js/serverval"></script>
  10 + <script type="text/javascript" src="/assets/js/session"></script>
10 <script> 11 <script>
11 //<![CDATA[ 12 //<![CDATA[
  13 + var sess = null;
  14 +
12 $(document).ready(function() { 15 $(document).ready(function() {
13 var sval = new ServerVal("#randval"); 16 var sval = new ServerVal("#randval");
14 17
  18 + sess = new Session("#sessinfo");
  19 + $.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess));
  20 +
15 $("ul#menu li:eq(0)").click(function() { 21 $("ul#menu li:eq(0)").click(function() {
16 sval.start(); 22 sval.start();
17 }); 23 });
18 24
  25 + $("ul#menu li:eq(1)").click(function() {
  26 + $("#login").removeClass("hide");
  27 + });
  28 +
19 $("#randval").click(function() { 29 $("#randval").click(function() {
20 sval.stop(); 30 sval.stop();
21 }); 31 });
  32 +
  33 + $("#login form").submit(function(event) {
  34 + event.preventDefault();
  35 + $.post("/login/",
  36 + $("#login form").serialize(),
  37 + $.proxy(sess.loadJSON, sess));
  38 + $("#login").addClass("hide");
  39 + });
22 }); 40 });
23 //]]> 41 //]]>
24 </script> 42 </script>
@@ -26,7 +44,17 @@ @@ -26,7 +44,17 @@
26 <body> 44 <body>
27 <ul id="menu"> 45 <ul id="menu">
28 <li>random Value</li> 46 <li>random Value</li>
  47 + <li>login</li>
29 </ul> 48 </ul>
  49 + <div id="sessinfo" class="x-small">
  50 + Session: <span></span><br />
  51 + <canvas width="100px" height="3px"></canvas>
  52 + </div>
  53 + <div id="login" class="hide">
  54 + <form>
  55 + <input type="text" name="username" />
  56 + </form>
  57 + </div>
30 <div id="randval" class="hide"> 58 <div id="randval" class="hide">
31 <span class=\"small"> 59 <span class=\"small">
32 Value created at: <br /> 60 Value created at: <br />
@@ -33,13 +33,17 @@ ServerVal.prototype.show = function() @@ -33,13 +33,17 @@ ServerVal.prototype.show = function()
33 33
34 ServerVal.prototype.start = function() 34 ServerVal.prototype.start = function()
35 { 35 {
36 - this.interval = setInterval($.proxy(this.process, this), 1000); 36 + if (null === this.interval) {
  37 + this.interval = setInterval($.proxy(this.process, this), 1000);
  38 + }
37 } 39 }
38 40
39 ServerVal.prototype.process = function() 41 ServerVal.prototype.process = function()
40 { 42 {
41 if (0 >= this.vnext) { 43 if (0 >= this.vnext) {
42 - $.getJSON("/randval/", $.proxy(this.loadJSON, this)); 44 + $.getJSON("/randval/", $.proxy(this.loadJSON, this))
  45 + .error($.proxy(function() {this.stop();}, this));
  46 + $.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess));
43 } 47 }
44 48
45 else { 49 else {
  1 +function Session(sId)
  2 +{
  3 + this.eSid = $(sId + " span");
  4 + this.canvas = $(sId + " canvas").get(0);
  5 + this.context = this.canvas.getContext("2d");
  6 +
  7 + this.id = "none"
  8 + this.timeout = 0;
  9 + this.timeleft = 0;
  10 + this.interval = null;
  11 +
  12 + this.draw();
  13 +}
  14 +
  15 +Session.prototype.loadJSON = function(data)
  16 +{
  17 + this.id = ("0" == data.id)? "none" : data.id;
  18 + this.timeout = data.timeout;
  19 + this.timeleft = data.timeleft;
  20 +
  21 + this.draw();
  22 + if (0 < this.timeleft)
  23 + this.start();
  24 +}
  25 +
  26 +Session.prototype.draw = function()
  27 +{
  28 + this.eSid.empty().append(this.id);
  29 +
  30 + this.context.fillStyle = "rgb(255, 0, 0)";
  31 + this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
  32 +
  33 + this.context.fillStyle = "rgb(0, 255, 0)";
  34 + this.context.fillRect(0, 0,
  35 + this.canvas.width / this.timeout * this.timeleft,
  36 + this.canvas.height);
  37 +}
  38 +
  39 +Session.prototype.start = function()
  40 +{
  41 + if (null === this.interval) {
  42 + this.interval = setInterval($.proxy(this.process, this), 1000);
  43 + }
  44 +}
  45 +
  46 +Session.prototype.process = function()
  47 +{
  48 + if (0 >= this.timeleft) {
  49 + this.stop();
  50 + }
  51 +
  52 + else {
  53 + this.timeleft--;
  54 + this.draw();
  55 + }
  56 +}
  57 +
  58 +Session.prototype.stop = function()
  59 +{
  60 + clearInterval(this.interval);
  61 + this.interval = null;
  62 + this.id = "none";
  63 + this.timeout = 0;
  64 + this.timeleft = 0;
  65 +
  66 + this.draw();
  67 +}
  68 +
  69 +// vim: set ts=4 sw=4:
@@ -8,14 +8,22 @@ div#randval { @@ -8,14 +8,22 @@ div#randval {
8 border-radius: 10px; 8 border-radius: 10px;
9 } 9 }
10 10
11 -div.hide#randval {  
12 - top: -500px; 11 +div#login {
  12 + position: fixed;
  13 +}
  14 +
  15 +div.hide {
  16 + top: -500px !important;
13 } 17 }
14 18
15 .small { 19 .small {
16 font-size: small; 20 font-size: small;
17 } 21 }
18 22
  23 +.x-small {
  24 + font-size: x-small;
  25 +}
  26 +
19 ul#menu { 27 ul#menu {
20 list-style: none inside; 28 list-style: none inside;
21 margin: 0px; 29 margin: 0px;
@@ -38,4 +46,9 @@ ul#menu li { @@ -38,4 +46,9 @@ ul#menu li {
38 float: left; 46 float: left;
39 margin-right: 1px; 47 margin-right: 1px;
40 } 48 }
  49 +
  50 +div#sessinfo canvas {
  51 + border: 1px solid black;
  52 +}
  53 +
41 /* vim: set st=4 sw=4: */ 54 /* vim: set st=4 sw=4: */
@@ -29,6 +29,7 @@ @@ -29,6 +29,7 @@
29 29
30 #include "class.h" 30 #include "class.h"
31 #include "http/message.h" 31 #include "http/message.h"
  32 +#include "session.h"
32 33
33 34
34 CLASS(HttpResponse) { 35 CLASS(HttpResponse) {
@@ -47,6 +48,7 @@ HttpResponse httpResponse403(); @@ -47,6 +48,7 @@ HttpResponse httpResponse403();
47 HttpResponse httpResponseMe(); 48 HttpResponse httpResponseMe();
48 HttpResponse httpResponseLoginForm(); 49 HttpResponse httpResponseLoginForm();
49 HttpResponse httpResponseRandval(time_t, int); 50 HttpResponse httpResponseRandval(time_t, int);
  51 +HttpResponse httpResponseSession(Session);
50 HttpResponse httpResponseAsset( 52 HttpResponse httpResponseAsset(
51 const char *, 53 const char *,
52 const char *, size_t, 54 const char *, size_t,
@@ -30,7 +30,8 @@ RESP = http/response.c \ @@ -30,7 +30,8 @@ RESP = http/response.c \
30 http/response/403.c \ 30 http/response/403.c \
31 http/response/login_form.c \ 31 http/response/login_form.c \
32 http/response/asset.c \ 32 http/response/asset.c \
33 - http/response/randval.c 33 + http/response/randval.c \
  34 + http/response/session.c
34 PARSER = http/parser.c \ 35 PARSER = http/parser.c \
35 http/parser/parse.c \ 36 http/parser/parse.c \
36 http/parser/new_message.c \ 37 http/parser/new_message.c \
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright (C) 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <stdlib.h>
  24 +#include <string.h>
  25 +#include <stdio.h>
  26 +#include <time.h>
  27 +#include <sys/types.h>
  28 +
  29 +#include "class.h"
  30 +#include "interface/class.h"
  31 +
  32 +#include "http/response.h"
  33 +#include "http/message.h"
  34 +#include "http/header.h"
  35 +#include "session.h"
  36 +
  37 +#include "utils/memory.h"
  38 +
  39 +#define RESP_DATA "{\"id\":\"%lu\",\"timeout\":%d,\"timeleft\":%ld}"
  40 +
  41 +HttpResponse
  42 +httpResponseSession(Session session)
  43 +{
  44 + char buffer[200];
  45 + HttpResponse response;
  46 + HttpMessage message;
  47 + size_t nbuf;
  48 +
  49 + response = new(HttpResponse, "HTTP/1.1", 200, "OK");
  50 + message = (HttpMessage)response;
  51 +
  52 + httpHeaderAdd(&(message->header),
  53 + new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
  54 +
  55 + message->type = HTTP_MESSAGE_BUFFERED;
  56 +
  57 + nbuf = sprintf(buffer, RESP_DATA,
  58 + (NULL != session)? session->id : 0,
  59 + (NULL != session)? SESSION_LIVETIME : 0,
  60 + (NULL != session)? session->livetime - time(NULL) : 0);
  61 +
  62 + message->nbody = nbuf;
  63 + message->body = malloc(nbuf);
  64 + memcpy(message->body, buffer, nbuf);
  65 +
  66 + return response;
  67 +}
  68 +
  69 +// vim: set ts=4 sw=4:
@@ -24,6 +24,7 @@ @@ -24,6 +24,7 @@
24 #include <stdio.h> 24 #include <stdio.h>
25 #include <stdlib.h> 25 #include <stdlib.h>
26 #include <time.h> 26 #include <time.h>
  27 +#include <sys/time.h>
27 28
28 #include "class.h" 29 #include "class.h"
29 #include "interface/class.h" 30 #include "interface/class.h"
@@ -91,7 +92,7 @@ httpWorkerProcess(HttpWorker this, Stream st) @@ -91,7 +92,7 @@ httpWorkerProcess(HttpWorker this, Stream st)
91 } 92 }
92 93
93 if (0 == strcmp("POST", request->method)) { 94 if (0 == strcmp("POST", request->method)) {
94 - if (0 == strcmp("/me/", request->uri)) { 95 + if (0 == strcmp("/login/", request->uri)) {
95 char * delim = memchr(rmessage->body, '=', rmessage->nbody); 96 char * delim = memchr(rmessage->body, '=', rmessage->nbody);
96 char * val; 97 char * val;
97 size_t nkey, nval; 98 size_t nkey, nval;
@@ -108,10 +109,7 @@ httpWorkerProcess(HttpWorker this, Stream st) @@ -108,10 +109,7 @@ httpWorkerProcess(HttpWorker this, Stream st)
108 new(Session, val, nval)); 109 new(Session, val, nval));
109 nbuf = sprintf(buffer, "sid=%lu;Path=/", this->session->id); 110 nbuf = sprintf(buffer, "sid=%lu;Path=/", this->session->id);
110 111
111 - response = httpWorkerGetAsset(  
112 - request,  
113 - "./assets/html/main.html",  
114 - CSTRA("text/html")); 112 + response = (HttpMessage)httpResponseSession(this->session);
115 113
116 httpHeaderAdd( 114 httpHeaderAdd(
117 &(response->header), 115 &(response->header),
@@ -121,10 +119,6 @@ httpWorkerProcess(HttpWorker this, Stream st) @@ -121,10 +119,6 @@ httpWorkerProcess(HttpWorker this, Stream st)
121 119
122 if (0 == strcmp("GET", request->method)) { 120 if (0 == strcmp("GET", request->method)) {
123 121
124 - if (0 == strcmp("/login/", request->uri)) {  
125 - response = (HttpMessage)httpResponseLoginForm();  
126 - }  
127 -  
128 if (0 == strcmp("/", request->uri)) { 122 if (0 == strcmp("/", request->uri)) {
129 response = httpWorkerGetAsset( 123 response = httpWorkerGetAsset(
130 request, 124 request,
@@ -132,14 +126,18 @@ httpWorkerProcess(HttpWorker this, Stream st) @@ -132,14 +126,18 @@ httpWorkerProcess(HttpWorker this, Stream st)
132 CSTRA("text/html")); 126 CSTRA("text/html"));
133 } 127 }
134 128
  129 + if (0 == strcmp("/sessinfo/", request->uri)) {
  130 + response = (HttpMessage)httpResponseSession(this->session);
  131 + }
  132 +
135 if (0 == strcmp("/randval/", request->uri)) { 133 if (0 == strcmp("/randval/", request->uri)) {
136 - //if (NULL != this->session) { 134 + if (NULL != this->session) {
137 response = (HttpMessage)httpResponseRandval( 135 response = (HttpMessage)httpResponseRandval(
138 this->val->timestamp, 136 this->val->timestamp,
139 this->val->value); 137 this->val->value);
140 - //} else {  
141 - // response = (HttpMessage)httpResponse403();  
142 - //} 138 + } else {
  139 + response = (HttpMessage)httpResponse403();
  140 + }
143 } 141 }
144 142
145 if (0 == strcmp("/image/me", request->uri)) { 143 if (0 == strcmp("/image/me", request->uri)) {
@@ -163,6 +161,13 @@ httpWorkerProcess(HttpWorker this, Stream st) @@ -163,6 +161,13 @@ httpWorkerProcess(HttpWorker this, Stream st)
163 CSTRA("text/javascript")); 161 CSTRA("text/javascript"));
164 } 162 }
165 163
  164 + if (0 == strcmp("/assets/js/session", request->uri)) {
  165 + response = httpWorkerGetAsset(
  166 + request,
  167 + "./assets/js/session.js",
  168 + CSTRA("text/javascript"));
  169 + }
  170 +
166 if (0 == strcmp("/assets/style/common", request->uri)) { 171 if (0 == strcmp("/assets/style/common", request->uri)) {
167 response = httpWorkerGetAsset( 172 response = httpWorkerGetAsset(
168 request, 173 request,
Please register or login to post a comment