Browse Source

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

master
Georg Hopp 14 years ago
parent
commit
16fdf54de5
  1. 2
      include/http/response.h
  2. 10
      include/http/worker.h
  3. 7
      src/http/response/me.c
  4. 24
      src/http/response/randval.c
  5. 4
      src/http/worker.c
  6. 4
      src/http/worker/process.c
  7. 1
      src/server/run.c
  8. 24
      src/testserver.c

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

10
include/http/worker.h

@ -25,6 +25,7 @@
#define __HTTP_WORKER_H__
#include <sys/types.h>
#include <time.h>
#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;

7
src/http/response/me.c

@ -42,8 +42,6 @@
"<title>My own little Web-App</title>" \
"<style type=\"text/css\">" \
"div#randval {" \
"width: 120px;" \
"height: 30px;" \
"left: 200px;" \
"top: 100px;" \
"position: fixed;" \
@ -52,7 +50,10 @@
"border: 1px solid black;" \
"}" \
"div.hide#randval {" \
"top: -50px;" \
"top: -500px;" \
"}" \
".small {" \
"font-size: small;" \
"}" \
"</style>" \
"<script type=\"text/javascript\" src=\"/jquery/\"></script>" \

24
src/http/response/randval.c

@ -33,16 +33,20 @@
#include "http/message.h"
#include "http/header.h"
#define RESP_DATA "%02d"
#define RESP_DATA "<span class=\"small\">" \
"Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \
"<br />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);

4
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);

4
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)) {

1
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;
/**

24
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);
}

Loading…
Cancel
Save