initial commit: pulser
[detour.git] / pulser.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <jh.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <assert.h>
7
8 time_t real_seconds(void) {
9   struct timespec t;
10   int s = clock_gettime(CLOCK_REALTIME, &t);
11   assert(s==0);
12   return t.tv_sec;
13 }
14
15 /* Subtract the `struct timeval' values X and Y,
16    storing the result in RESULT.
17    Return 1 if the difference is negative, otherwise 0. */
18 int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
19   /* Perform the carry for the later subtraction by updating y. */
20   if (x->tv_usec < y->tv_usec) {
21     int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
22     y->tv_usec -= 1000000 * nsec;
23     y->tv_sec += nsec;
24   }
25   if (x->tv_usec - y->tv_usec > 1000000) {
26     int nsec = (x->tv_usec - y->tv_usec) / 1000000;
27     y->tv_usec += 1000000 * nsec;
28     y->tv_sec -= nsec;
29   }
30
31   /* Compute the time remaining to wait.
32      tv_usec is certainly positive. */
33   result->tv_sec = x->tv_sec - y->tv_sec;
34   result->tv_usec = x->tv_usec - y->tv_usec;
35
36   /* Return 1 if result is negative. */
37   return x->tv_sec < y->tv_sec;
38 }
39
40 void sleep_until(time_t dst_sec) {
41   struct timespec dst;
42   dst.tv_sec = dst_sec;
43   dst.tv_nsec = 0;
44   while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &dst, NULL)) /* nothing */;
45 }
46
47 time_t round_up(time_t t, int align) {
48   t = t - 1; /* counter bad +t for aligned things in last step */
49   t = t - t%align; /* round down to align */
50   t = t + align; /* add alignment to make it a round up */
51   return t;
52 }
53
54 void handle_connection(int s) {
55   dup2(s, 1);
56   dup2(s, 0);
57
58   char line_in[100];
59   if (!fgets(line_in, 100, stdin)) return;
60   char *p = line_in + strlen("GET /");
61
62   setbuf(stdout, NULL);
63   printf("HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<!--");
64
65   char *spaaaace = malloc(1024*64);
66   memset(spaaaace, ' ', 1024*64);
67   spaaaace[0] = 'A';
68
69   time_t t = round_up(real_seconds(), 4);
70   while (1) {
71     if (*p != '0' && *p != '1') return;
72     if (*p == '1') t+=2;
73     sleep_until(t);
74     if (fwrite(spaaaace, 1024, 64, stdout) <= 0) return;
75     t += (*p == '1') ? 2 : 4;
76     p++;
77   }
78 }
79
80 int main(void) {
81   int s = netopen_server(NULL, "4422", JH_TCP_HINTS);
82   while (1) {
83     int s_ = accept(s, NULL, NULL);
84     pid_t p = fork();
85     if (p == 0) {
86       handle_connection(s_);
87       return 0;
88     }
89     close(s_);
90   }
91 }