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 | 44 | const char *, size_t); |
45 | 45 | HttpResponse httpResponse404(); |
46 | 46 | HttpResponse httpResponseMe(); |
47 | -HttpResponse httpResponseRandval(int); | |
47 | +HttpResponse httpResponseRandval(time_t, int); | |
48 | 48 | HttpResponse httpResponseAsset( |
49 | 49 | const char *, |
50 | 50 | const char *, size_t, | ... | ... |
... | ... | @@ -25,6 +25,7 @@ |
25 | 25 | #define __HTTP_WORKER_H__ |
26 | 26 | |
27 | 27 | #include <sys/types.h> |
28 | +#include <time.h> | |
28 | 29 | |
29 | 30 | #include "class.h" |
30 | 31 | #include "http/parser.h" |
... | ... | @@ -40,10 +41,15 @@ |
40 | 41 | #define FALSE ((void *)0) |
41 | 42 | #endif |
42 | 43 | |
44 | +struct randval { | |
45 | + time_t timestamp; | |
46 | + int value; | |
47 | +}; | |
48 | + | |
43 | 49 | |
44 | 50 | CLASS(HttpWorker) { |
45 | - char * id; | |
46 | - int * val; | |
51 | + char * id; | |
52 | + struct randval * val; | |
47 | 53 | |
48 | 54 | Cbuf pbuf; |
49 | 55 | Cbuf wbuf; | ... | ... |
... | ... | @@ -42,8 +42,6 @@ |
42 | 42 | "<title>My own little Web-App</title>" \ |
43 | 43 | "<style type=\"text/css\">" \ |
44 | 44 | "div#randval {" \ |
45 | - "width: 120px;" \ | |
46 | - "height: 30px;" \ | |
47 | 45 | "left: 200px;" \ |
48 | 46 | "top: 100px;" \ |
49 | 47 | "position: fixed;" \ |
... | ... | @@ -52,7 +50,10 @@ |
52 | 50 | "border: 1px solid black;" \ |
53 | 51 | "}" \ |
54 | 52 | "div.hide#randval {" \ |
55 | - "top: -50px;" \ | |
53 | + "top: -500px;" \ | |
54 | + "}" \ | |
55 | + ".small {" \ | |
56 | + "font-size: small;" \ | |
56 | 57 | "}" \ |
57 | 58 | "</style>" \ |
58 | 59 | "<script type=\"text/javascript\" src=\"/jquery/\"></script>" \ | ... | ... |
... | ... | @@ -33,16 +33,20 @@ |
33 | 33 | #include "http/message.h" |
34 | 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 | 40 | HttpResponse |
40 | -httpResponseRandval(int value) | |
41 | +httpResponseRandval(time_t ctime, int value) | |
41 | 42 | { |
43 | + char timebuf[200]; | |
42 | 44 | char buffer[200]; |
43 | 45 | HttpResponse response; |
44 | 46 | HttpMessage message; |
45 | 47 | size_t nbuf; |
48 | + struct tm * tmp; | |
49 | + time_t remaining; | |
46 | 50 | |
47 | 51 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
48 | 52 | message = (HttpMessage)response; |
... | ... | @@ -55,9 +59,17 @@ httpResponseRandval(int value) |
55 | 59 | sizeof("text/html")-1)); |
56 | 60 | |
57 | 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 | 74 | nbuf = sprintf(buffer, "%d", message->nbody); |
63 | 75 | ... | ... |
... | ... | @@ -21,12 +21,12 @@ httpWorkerCtor(void * _this, va_list * params) |
21 | 21 | { |
22 | 22 | HttpWorker this = _this; |
23 | 23 | char * id = va_arg(*params, char *); |
24 | - int * val = va_arg(*params, int *); | |
25 | 24 | char cbuf_id[100]; |
26 | 25 | |
27 | 26 | this->id = malloc(strlen(id) + 1); |
28 | 27 | strcpy(this->id, id); |
29 | - this->val = val; | |
28 | + | |
29 | + this->val = va_arg(*params, struct randval *); | |
30 | 30 | |
31 | 31 | sprintf(cbuf_id, "%s_%s", "parser", id); |
32 | 32 | this->pbuf = new(Cbuf, cbuf_id, PARSER_MAX_BUF); | ... | ... |
... | ... | @@ -59,7 +59,9 @@ httpWorkerProcess(HttpWorker this, int fd) |
59 | 59 | } |
60 | 60 | |
61 | 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 | 67 | if (0 == strcmp("/image/", request->uri)) { | ... | ... |
... | ... | @@ -42,10 +42,10 @@ |
42 | 42 | |
43 | 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 | 47 | //#define DEFAULT_SECS 1 |
48 | -//#define DEFAULT_USECS 0 | |
48 | +#define DEFAULT_USECS 0 | |
49 | 49 | |
50 | 50 | void nullhandler() {} |
51 | 51 | |
... | ... | @@ -54,11 +54,11 @@ void daemonize(void); |
54 | 54 | int |
55 | 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 | 63 | struct rlimit limit = {RLIM_INFINITY, RLIM_INFINITY}; |
64 | 64 | setrlimit(RLIMIT_CPU, &limit); |
... | ... | @@ -82,9 +82,10 @@ main() |
82 | 82 | struct sigaction s; |
83 | 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 | 86 | MAP_SHARED, shm, 0); |
87 | - *value = 0; | |
87 | + value->timestamp = 0; | |
88 | + value->value = 0; | |
88 | 89 | |
89 | 90 | close(shm); |
90 | 91 | |
... | ... | @@ -112,7 +113,8 @@ main() |
112 | 113 | |
113 | 114 | // child |
114 | 115 | while(!doShutdown) { |
115 | - *value = rand() % 100; | |
116 | + value->timestamp = time(NULL); | |
117 | + value->value = rand() % 100; | |
116 | 118 | sigsuspend(&pause_mask); |
117 | 119 | } |
118 | 120 | ... | ... |
Please
register
or
login
to post a comment