From 16fdf54de506004bf90e6ed32572fb33b847eadb Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Fri, 2 Mar 2012 18:54:13 +0100 Subject: [PATCH] now the internal random value will be create only once every 30 seconds thus one can see that it is realy an internal state of the server --- include/http/response.h | 2 +- include/http/worker.h | 10 ++++++++-- src/http/response/me.c | 7 ++++--- src/http/response/randval.c | 24 ++++++++++++++++++------ src/http/worker.c | 4 ++-- src/http/worker/process.c | 4 +++- src/server/run.c | 1 - src/testserver.c | 24 +++++++++++++----------- 8 files changed, 49 insertions(+), 27 deletions(-) diff --git a/include/http/response.h b/include/http/response.h index e5724cc..a6588b8 100644 --- a/include/http/response.h +++ b/include/http/response.h @@ -44,7 +44,7 @@ HttpResponse httpResponse304( const char *, size_t); HttpResponse httpResponse404(); HttpResponse httpResponseMe(); -HttpResponse httpResponseRandval(int); +HttpResponse httpResponseRandval(time_t, int); HttpResponse httpResponseAsset( const char *, const char *, size_t, diff --git a/include/http/worker.h b/include/http/worker.h index 19bf1dd..669f9aa 100644 --- a/include/http/worker.h +++ b/include/http/worker.h @@ -25,6 +25,7 @@ #define __HTTP_WORKER_H__ #include +#include #include "class.h" #include "http/parser.h" @@ -40,10 +41,15 @@ #define FALSE ((void *)0) #endif +struct randval { + time_t timestamp; + int value; +}; + CLASS(HttpWorker) { - char * id; - int * val; + char * id; + struct randval * val; Cbuf pbuf; Cbuf wbuf; diff --git a/src/http/response/me.c b/src/http/response/me.c index 8c3b52d..62fd138 100644 --- a/src/http/response/me.c +++ b/src/http/response/me.c @@ -42,8 +42,6 @@ "My own little Web-App" \ "" \ "" \ diff --git a/src/http/response/randval.c b/src/http/response/randval.c index 722a81e..e11c128 100644 --- a/src/http/response/randval.c +++ b/src/http/response/randval.c @@ -33,16 +33,20 @@ #include "http/message.h" #include "http/header.h" - -#define RESP_DATA "%02d" +#define RESP_DATA "" \ + "Value created at:
%s
Next value in: %ld seconds
" \ + "
Value: %02d" HttpResponse -httpResponseRandval(int value) +httpResponseRandval(time_t ctime, int value) { + char timebuf[200]; char buffer[200]; HttpResponse response; HttpMessage message; size_t nbuf; + struct tm * tmp; + time_t remaining; response = new(HttpResponse, "HTTP/1.1", 200, "OK"); message = (HttpMessage)response; @@ -55,9 +59,17 @@ httpResponseRandval(int value) sizeof("text/html")-1)); message->type = HTTP_MESSAGE_BUFFERED; - message->nbody = sizeof(RESP_DATA)-1-2; - message->body = malloc(sizeof(RESP_DATA)-2); - sprintf(message->body, RESP_DATA, value); + + tmp = localtime(&ctime); + nbuf = strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %T %Z", tmp); + + remaining = 30 - (time(NULL) - ctime); + + nbuf = sprintf(buffer, RESP_DATA, timebuf, remaining, value); + + message->nbody = nbuf; + message->body = malloc(nbuf); + memcpy(message->body, buffer, nbuf); nbuf = sprintf(buffer, "%d", message->nbody); diff --git a/src/http/worker.c b/src/http/worker.c index 9f3fb2d..6ef098a 100644 --- a/src/http/worker.c +++ b/src/http/worker.c @@ -21,12 +21,12 @@ httpWorkerCtor(void * _this, va_list * params) { HttpWorker this = _this; char * id = va_arg(*params, char *); - int * val = va_arg(*params, int *); char cbuf_id[100]; this->id = malloc(strlen(id) + 1); strcpy(this->id, id); - this->val = val; + + this->val = va_arg(*params, struct randval *); sprintf(cbuf_id, "%s_%s", "parser", id); this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF); diff --git a/src/http/worker/process.c b/src/http/worker/process.c index b5246eb..a6ce588 100644 --- a/src/http/worker/process.c +++ b/src/http/worker/process.c @@ -59,7 +59,9 @@ httpWorkerProcess(HttpWorker this, int fd) } if (0 == strcmp("/randval/", request->uri)) { - response = (HttpMessage)httpResponseRandval(*(this->val)); + response = (HttpMessage)httpResponseRandval( + this->val->timestamp, + this->val->value); } if (0 == strcmp("/image/", request->uri)) { diff --git a/src/server/run.c b/src/server/run.c index 2f84fba..4741b1d 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -56,7 +56,6 @@ serverRun(Server this) } for (i=1; i < this->nfds; i++) { - int fd = (this->fds)[i].fd; int nreads = 10, nwrites = 10; /** diff --git a/src/testserver.c b/src/testserver.c index f1dd1f9..318358b 100644 --- a/src/testserver.c +++ b/src/testserver.c @@ -42,10 +42,10 @@ #include "utils/signalHandling.h" -#define DEFAULT_SECS 1 -#define DEFAULT_USECS (1000000 / HZ * 2) +#define DEFAULT_SECS 30 +//#define DEFAULT_USECS (1000000 / HZ * 2) //#define DEFAULT_SECS 1 -//#define DEFAULT_USECS 0 +#define DEFAULT_USECS 0 void nullhandler() {} @@ -54,11 +54,11 @@ void daemonize(void); int main() { - pid_t pid; - long psize = sysconf(_SC_PAGESIZE); - int status; - int shm; - int * value; + pid_t pid; + long psize = sysconf(_SC_PAGESIZE); + int status; + int shm; + struct randval * value; struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY}; setrlimit(RLIMIT_CPU, &limit); @@ -82,9 +82,10 @@ main() struct sigaction s; struct itimerval interval; - value = mmap (0, sizeof(int), PROT_READ|PROT_WRITE, + value = mmap (0, sizeof(struct randval), PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0); - *value = 0; + value->timestamp = 0; + value->value = 0; close(shm); @@ -112,7 +113,8 @@ main() // child while(!doShutdown) { - *value = rand() % 100; + value->timestamp = time(NULL); + value->value = rand() % 100; sigsuspend(&pause_mask); }