Browse Source

now the session livetime is shown with a bar, as well as the current session id. These are updated on reload or ajax call.

master
Georg Hopp 14 years ago
parent
commit
d4b1c3fd3a
  1. 28
      assets/html/main.html
  2. 8
      assets/js/serverval.js
  3. 69
      assets/js/session.js
  4. 17
      assets/style/common.css
  5. 2
      include/http/response.h
  6. 3
      src/Makefile.am
  7. 69
      src/http/response/session.c
  8. 31
      src/http/worker/process.c

28
assets/html/main.html

@ -7,18 +7,36 @@
<link rel="stylesheet" type="text/css" href="/assets/style/common">
<script type="text/javascript" src="/assets/js/jquery"></script>
<script type="text/javascript" src="/assets/js/serverval"></script>
<script type="text/javascript" src="/assets/js/session"></script>
<script>
//<![CDATA[
var sess = null;
$(document).ready(function() {
var sval = new ServerVal("#randval");
sess = new Session("#sessinfo");
$.getJSON("/sessinfo/", $.proxy(sess.loadJSON, sess));
$("ul#menu li:eq(0)").click(function() {
sval.start();
});
$("ul#menu li:eq(1)").click(function() {
$("#login").removeClass("hide");
});
$("#randval").click(function() {
sval.stop();
});
$("#login form").submit(function(event) {
event.preventDefault();
$.post("/login/",
$("#login form").serialize(),
$.proxy(sess.loadJSON, sess));
$("#login").addClass("hide");
});
});
//]]>
</script>
@ -26,7 +44,17 @@
<body>
<ul id="menu">
<li>random Value</li>
<li>login</li>
</ul>
<div id="sessinfo" class="x-small">
Session: <span></span><br />
<canvas width="100px" height="3px"></canvas>
</div>
<div id="login" class="hide">
<form>
<input type="text" name="username" />
</form>
</div>
<div id="randval" class="hide">
<span class=\"small">
Value created at: <br />

8
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 {

69
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:

17
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: */

2
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,

3
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 \

69
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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <sys/types.h>
#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:

31
src/http/worker/process.c

@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#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,

Loading…
Cancel
Save