Commit 16fdf54de506004bf90e6ed32572fb33b847eadb
1 parent
08533a4b
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
Showing
8 changed files
with
49 additions
and
27 deletions
@@ -44,7 +44,7 @@ HttpResponse httpResponse304( | @@ -44,7 +44,7 @@ HttpResponse httpResponse304( | ||
44 | const char *, size_t); | 44 | const char *, size_t); |
45 | HttpResponse httpResponse404(); | 45 | HttpResponse httpResponse404(); |
46 | HttpResponse httpResponseMe(); | 46 | HttpResponse httpResponseMe(); |
47 | -HttpResponse httpResponseRandval(int); | 47 | +HttpResponse httpResponseRandval(time_t, int); |
48 | HttpResponse httpResponseAsset( | 48 | HttpResponse httpResponseAsset( |
49 | const char *, | 49 | const char *, |
50 | const char *, size_t, | 50 | const char *, size_t, |
@@ -25,6 +25,7 @@ | @@ -25,6 +25,7 @@ | ||
25 | #define __HTTP_WORKER_H__ | 25 | #define __HTTP_WORKER_H__ |
26 | 26 | ||
27 | #include <sys/types.h> | 27 | #include <sys/types.h> |
28 | +#include <time.h> | ||
28 | 29 | ||
29 | #include "class.h" | 30 | #include "class.h" |
30 | #include "http/parser.h" | 31 | #include "http/parser.h" |
@@ -40,10 +41,15 @@ | @@ -40,10 +41,15 @@ | ||
40 | #define FALSE ((void *)0) | 41 | #define FALSE ((void *)0) |
41 | #endif | 42 | #endif |
42 | 43 | ||
44 | +struct randval { | ||
45 | + time_t timestamp; | ||
46 | + int value; | ||
47 | +}; | ||
48 | + | ||
43 | 49 | ||
44 | CLASS(HttpWorker) { | 50 | CLASS(HttpWorker) { |
45 | - char * id; | ||
46 | - int * val; | 51 | + char * id; |
52 | + struct randval * val; | ||
47 | 53 | ||
48 | Cbuf pbuf; | 54 | Cbuf pbuf; |
49 | Cbuf wbuf; | 55 | Cbuf wbuf; |
@@ -42,8 +42,6 @@ | @@ -42,8 +42,6 @@ | ||
42 | "<title>My own little Web-App</title>" \ | 42 | "<title>My own little Web-App</title>" \ |
43 | "<style type=\"text/css\">" \ | 43 | "<style type=\"text/css\">" \ |
44 | "div#randval {" \ | 44 | "div#randval {" \ |
45 | - "width: 120px;" \ | ||
46 | - "height: 30px;" \ | ||
47 | "left: 200px;" \ | 45 | "left: 200px;" \ |
48 | "top: 100px;" \ | 46 | "top: 100px;" \ |
49 | "position: fixed;" \ | 47 | "position: fixed;" \ |
@@ -52,7 +50,10 @@ | @@ -52,7 +50,10 @@ | ||
52 | "border: 1px solid black;" \ | 50 | "border: 1px solid black;" \ |
53 | "}" \ | 51 | "}" \ |
54 | "div.hide#randval {" \ | 52 | "div.hide#randval {" \ |
55 | - "top: -50px;" \ | 53 | + "top: -500px;" \ |
54 | + "}" \ | ||
55 | + ".small {" \ | ||
56 | + "font-size: small;" \ | ||
56 | "}" \ | 57 | "}" \ |
57 | "</style>" \ | 58 | "</style>" \ |
58 | "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \ | 59 | "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \ |
@@ -33,16 +33,20 @@ | @@ -33,16 +33,20 @@ | ||
33 | #include "http/message.h" | 33 | #include "http/message.h" |
34 | #include "http/header.h" | 34 | #include "http/header.h" |
35 | 35 | ||
36 | - | ||
37 | -#define RESP_DATA "%02d" | 36 | +#define RESP_DATA "<span class=\"small\">" \ |
37 | + "Value created at:<br/>%s<br/>Next value in: %ld seconds</span>" \ | ||
38 | + "<br />Value: %02d" | ||
38 | 39 | ||
39 | HttpResponse | 40 | HttpResponse |
40 | -httpResponseRandval(int value) | 41 | +httpResponseRandval(time_t ctime, int value) |
41 | { | 42 | { |
43 | + char timebuf[200]; | ||
42 | char buffer[200]; | 44 | char buffer[200]; |
43 | HttpResponse response; | 45 | HttpResponse response; |
44 | HttpMessage message; | 46 | HttpMessage message; |
45 | size_t nbuf; | 47 | size_t nbuf; |
48 | + struct tm * tmp; | ||
49 | + time_t remaining; | ||
46 | 50 | ||
47 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); | 51 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
48 | message = (HttpMessage)response; | 52 | message = (HttpMessage)response; |
@@ -55,9 +59,17 @@ httpResponseRandval(int value) | @@ -55,9 +59,17 @@ httpResponseRandval(int value) | ||
55 | sizeof("text/html")-1)); | 59 | sizeof("text/html")-1)); |
56 | 60 | ||
57 | message->type = HTTP_MESSAGE_BUFFERED; | 61 | message->type = HTTP_MESSAGE_BUFFERED; |
58 | - message->nbody = sizeof(RESP_DATA)-1-2; | ||
59 | - message->body = malloc(sizeof(RESP_DATA)-2); | ||
60 | - sprintf(message->body, RESP_DATA, value); | 62 | + |
63 | + tmp = localtime(&ctime); | ||
64 | + nbuf = strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %T %Z", tmp); | ||
65 | + | ||
66 | + remaining = 30 - (time(NULL) - ctime); | ||
67 | + | ||
68 | + nbuf = sprintf(buffer, RESP_DATA, timebuf, remaining, value); | ||
69 | + | ||
70 | + message->nbody = nbuf; | ||
71 | + message->body = malloc(nbuf); | ||
72 | + memcpy(message->body, buffer, nbuf); | ||
61 | 73 | ||
62 | nbuf = sprintf(buffer, "%d", message->nbody); | 74 | nbuf = sprintf(buffer, "%d", message->nbody); |
63 | 75 |
@@ -21,12 +21,12 @@ httpWorkerCtor(void * _this, va_list * params) | @@ -21,12 +21,12 @@ httpWorkerCtor(void * _this, va_list * params) | ||
21 | { | 21 | { |
22 | HttpWorker this = _this; | 22 | HttpWorker this = _this; |
23 | char * id = va_arg(*params, char *); | 23 | char * id = va_arg(*params, char *); |
24 | - int * val = va_arg(*params, int *); | ||
25 | char cbuf_id[100]; | 24 | char cbuf_id[100]; |
26 | 25 | ||
27 | this->id = malloc(strlen(id) + 1); | 26 | this->id = malloc(strlen(id) + 1); |
28 | strcpy(this->id, id); | 27 | strcpy(this->id, id); |
29 | - this->val = val; | 28 | + |
29 | + this->val = va_arg(*params, struct randval *); | ||
30 | 30 | ||
31 | sprintf(cbuf_id, "%s_%s", "parser", id); | 31 | sprintf(cbuf_id, "%s_%s", "parser", id); |
32 | this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF); | 32 | this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF); |
@@ -59,7 +59,9 @@ httpWorkerProcess(HttpWorker this, int fd) | @@ -59,7 +59,9 @@ httpWorkerProcess(HttpWorker this, int fd) | ||
59 | } | 59 | } |
60 | 60 | ||
61 | if (0 == strcmp("/randval/", request->uri)) { | 61 | if (0 == strcmp("/randval/", request->uri)) { |
62 | - response = (HttpMessage)httpResponseRandval(*(this->val)); | 62 | + response = (HttpMessage)httpResponseRandval( |
63 | + this->val->timestamp, | ||
64 | + this->val->value); | ||
63 | } | 65 | } |
64 | 66 | ||
65 | if (0 == strcmp("/image/", request->uri)) { | 67 | if (0 == strcmp("/image/", request->uri)) { |
@@ -42,10 +42,10 @@ | @@ -42,10 +42,10 @@ | ||
42 | 42 | ||
43 | #include "utils/signalHandling.h" | 43 | #include "utils/signalHandling.h" |
44 | 44 | ||
45 | -#define DEFAULT_SECS 1 | ||
46 | -#define DEFAULT_USECS (1000000 / HZ * 2) | 45 | +#define DEFAULT_SECS 30 |
46 | +//#define DEFAULT_USECS (1000000 / HZ * 2) | ||
47 | //#define DEFAULT_SECS 1 | 47 | //#define DEFAULT_SECS 1 |
48 | -//#define DEFAULT_USECS 0 | 48 | +#define DEFAULT_USECS 0 |
49 | 49 | ||
50 | void nullhandler() {} | 50 | void nullhandler() {} |
51 | 51 | ||
@@ -54,11 +54,11 @@ void daemonize(void); | @@ -54,11 +54,11 @@ void daemonize(void); | ||
54 | int | 54 | int |
55 | main() | 55 | main() |
56 | { | 56 | { |
57 | - pid_t pid; | ||
58 | - long psize = sysconf(_SC_PAGESIZE); | ||
59 | - int status; | ||
60 | - int shm; | ||
61 | - int * value; | 57 | + pid_t pid; |
58 | + long psize = sysconf(_SC_PAGESIZE); | ||
59 | + int status; | ||
60 | + int shm; | ||
61 | + struct randval * value; | ||
62 | 62 | ||
63 | struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY}; | 63 | struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY}; |
64 | setrlimit(RLIMIT_CPU, &limit); | 64 | setrlimit(RLIMIT_CPU, &limit); |
@@ -82,9 +82,10 @@ main() | @@ -82,9 +82,10 @@ main() | ||
82 | struct sigaction s; | 82 | struct sigaction s; |
83 | struct itimerval interval; | 83 | struct itimerval interval; |
84 | 84 | ||
85 | - value = mmap (0, sizeof(int), PROT_READ|PROT_WRITE, | 85 | + value = mmap (0, sizeof(struct randval), PROT_READ|PROT_WRITE, |
86 | MAP_SHARED, shm, 0); | 86 | MAP_SHARED, shm, 0); |
87 | - *value = 0; | 87 | + value->timestamp = 0; |
88 | + value->value = 0; | ||
88 | 89 | ||
89 | close(shm); | 90 | close(shm); |
90 | 91 | ||
@@ -112,7 +113,8 @@ main() | @@ -112,7 +113,8 @@ main() | ||
112 | 113 | ||
113 | // child | 114 | // child |
114 | while(!doShutdown) { | 115 | while(!doShutdown) { |
115 | - *value = rand() % 100; | 116 | + value->timestamp = time(NULL); |
117 | + value->value = rand() % 100; | ||
116 | sigsuspend(&pause_mask); | 118 | sigsuspend(&pause_mask); |
117 | } | 119 | } |
118 | 120 |
Please
register
or
login
to post a comment