root/src/stratcon_realtime_http.c

Revision 0181abdce529ba17b164ec0a0165a96b920821f2, 3.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

temp fixes to testing

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7 #include "eventer/eventer.h"
8 #include "noit_conf.h"
9 #include "utils/noit_hash.h"
10 #include "utils/noit_log.h"
11 #include "jlog/jlog.h"
12 #include "noit_jlog_listener.h"
13 #include "noit_listener.h"
14 #include "noit_http.h"
15
16 typedef struct {
17   int setup;
18 } realtime_context;
19
20 static realtime_context *alloc_realtime_context() {
21   realtime_context *ctx;
22   return calloc(sizeof(*ctx), 1);
23 }
24 int
25 stratcon_realtime_ticker(eventer_t old, int mask, void *closure,
26                          struct timeval *now) {
27   char buffer[1024];
28   noit_http_session_ctx *ctx = closure;
29
30   if(0) {
31     noit_http_response_end(ctx);
32     memset(ctx->dispatcher_closure, 0, sizeof(realtime_context));
33     if(ctx->conn.e) eventer_trigger(ctx->conn.e, EVENTER_WRITE);
34     return 0;
35   }
36
37   eventer_t e = eventer_alloc();
38   gettimeofday(&e->whence, NULL);
39
40   snprintf(buffer, sizeof(buffer), "<script>window.parent.plot_iframe_data('%lu','%s','%0.3f','%lu');</script>\n",
41            (unsigned long)1179, "allocator_requests", (float)(rand() % 100000) / 1000.0,
42            e->whence.tv_sec * 1000 + e->whence.tv_usec / 100);
43   noit_http_response_append(ctx, buffer, strlen(buffer));
44   noit_http_response_flush(ctx, noit_false);
45
46   e->mask = EVENTER_TIMER;
47   e->whence.tv_sec += 0;
48   e->whence.tv_usec += 500000;
49   e->callback = stratcon_realtime_ticker;
50   e->closure = closure;
51   eventer_add(e);
52   return 0;
53 }
54 int
55 stratcon_request_dispatcher(noit_http_session_ctx *ctx) {
56   const char *key, *value;
57   realtime_context *rc = ctx->dispatcher_closure;
58   int klen;
59   noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
60   noit_http_request *req = &ctx->req;
61
62   if(strcmp(ctx->req.uri_str, "/data")) {
63     noit_http_response_status_set(ctx, 404, "OK");
64     noit_http_response_option_set(ctx, NOIT_HTTP_CLOSE);
65     noit_http_response_end(ctx);
66     return 0;
67   }
68   if(!rc->setup) {
69     char c[1024];
70     noitL(noit_error, "http: %s %s %s\n",
71           req->method_str, req->uri_str, req->protocol_str);
72     while(noit_hash_next(&req->headers, &iter, &key, &klen, (void **)&value)) {
73       noitL(noit_error, "http: [%s: %s]\n", key, value);
74     }
75     noit_http_response_status_set(ctx, 200, "OK");
76     noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED);
77 /*    noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE); */
78     noit_http_response_header_set(ctx, "Content-Type", "text/html");
79
80     snprintf(c, sizeof(c), "<html><head><script>document.domain='omniti.com';</script></head><body>\n");
81     noit_http_response_append(ctx, c, strlen(c));
82
83     memset(c, ' ', 1024);
84     noit_http_response_append(ctx, c, sizeof(c));
85     noit_http_response_flush(ctx, noit_false);
86
87     stratcon_realtime_ticker(NULL, 0, ctx, NULL);
88     rc->setup = 1;
89   }
90   return EVENTER_EXCEPTION;
91 }
92
93 int
94 stratcon_realtime_http_handler(eventer_t e, int mask, void *closure,
95                                struct timeval *now) {
96   acceptor_closure_t *ac = closure;
97   noit_http_session_ctx *http_ctx = ac->service_ctx;
98   if(!http_ctx) {
99     http_ctx = ac->service_ctx =
100       noit_http_session_ctx_new(stratcon_request_dispatcher,
101                                 alloc_realtime_context(),
102                                 e);
103   }
104   return http_ctx->drive(e, mask, http_ctx, now);
105 }
106
107 void
108 stratcon_realtime_http_init(const char *toplevel) {
109   eventer_name_callback("stratcon_realtime_http",
110                         stratcon_realtime_http_handler);
111 }
Note: See TracBrowser for help on using the browser.