Skip to content
This repository
Newer
Older
100644 125 lines (102 sloc) 3.421 kb
eeebb66f »
2009-08-21 initial import of pubsub
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "queue.h"
5b0770fd »
2009-08-25 Fixed args.
5 #include "simplehttp.h"
eeebb66f »
2009-08-21 initial import of pubsub
6
9eca1531 »
2009-08-25 Added BUFSIZE.
7 #define BUFSZ 1024
09802ddd »
2009-08-26 Adding support for multipart.
8 #define BOUNDARY "xXPubSubXx"
9eca1531 »
2009-08-25 Added BUFSIZE.
9
eeebb66f »
2009-08-21 initial import of pubsub
10 typedef struct cli {
11 struct evbuffer *buf;
12 struct evhttp_request *req;
13 TAILQ_ENTRY(cli) entries;
14 } cli;
15 TAILQ_HEAD(, cli) clients;
16
1604d507 »
2009-08-25 Added stats.
17 uint32_t totalConns = 0;
18 uint32_t currentConns = 0;
19 uint32_t msgRecv = 0;
20 uint32_t msgSent = 0;
21
22 void
38c69fdd »
2009-08-26 Moved stats to HTTP response headers.
23 stats_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
1604d507 »
2009-08-25 Added stats.
24 {
5b0770fd »
2009-08-25 Fixed args.
25 struct evkeyvalq args;
38c69fdd »
2009-08-26 Moved stats to HTTP response headers.
26 char *reset, *uri;
27 char buf[33];
1604d507 »
2009-08-25 Added stats.
28
29 uri = evhttp_decode_uri(req->uri);
30 evhttp_parse_query(uri, &args);
31 free(uri);
38c69fdd »
2009-08-26 Moved stats to HTTP response headers.
32
33 sprintf(buf, "%ld", totalConns);
34 evhttp_add_header(req->output_headers, "X-PUBSUB-TOTAL-CONNECTIONS", buf);
35 sprintf(buf, "%ld", currentConns);
36 evhttp_add_header(req->output_headers, "X-PUBSUB-CURRENT-CONNECTIONS", buf);
37 sprintf(buf, "%ld", msgRecv);
38 evhttp_add_header(req->output_headers, "X-PUBSUB-MESSAGES-RECEIVED", buf);
39 sprintf(buf, "%ld", msgSent);
40 evhttp_add_header(req->output_headers, "X-PUBSUB-MESSAGES-SENT", buf);
41
42 reset = (char *)evhttp_find_header(&args, "reset");
b1d1329c »
2009-08-25 Trying to fix new stats command.
43
44 if (reset) {
1604d507 »
2009-08-25 Added stats.
45 totalConns = 0;
46 currentConns = 0;
47 msgRecv = 0;
48 msgSent = 0;
49 }
50
38c69fdd »
2009-08-26 Moved stats to HTTP response headers.
51 evhttp_send_reply(req, HTTP_OK, "OK", evb);
52 evhttp_clear_headers(&args);
1604d507 »
2009-08-25 Added stats.
53 }
eeebb66f »
2009-08-21 initial import of pubsub
54
55 void on_close(struct evhttp_connection *evcon, void *ctx)
56 {
57 struct cli *client = (struct cli *)ctx;
58
59 if (client) {
1604d507 »
2009-08-25 Added stats.
60 currentConns--;
eeebb66f »
2009-08-21 initial import of pubsub
61 TAILQ_REMOVE(&clients, client, entries);
62 evbuffer_free(client->buf);
63 free(client);
64 }
65 }
66
67 void pub_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
68 {
69 int i = 0;
70 struct cli *client;
1604d507 »
2009-08-25 Added stats.
71
72 msgRecv++;
5b0770fd »
2009-08-25 Fixed args.
73 totalConns++;
eeebb66f »
2009-08-21 initial import of pubsub
74
75 TAILQ_FOREACH(client, &clients, entries) {
1604d507 »
2009-08-25 Added stats.
76 msgSent++;
eeebb66f »
2009-08-21 initial import of pubsub
77 evbuffer_drain(client->buf, EVBUFFER_LENGTH(client->buf));
09802ddd »
2009-08-26 Adding support for multipart.
78
79 evbuffer_add_printf(client->buf, "Content-Type: %s\n\n", "text/html");
eeebb66f »
2009-08-21 initial import of pubsub
80 evbuffer_add(client->buf, req->input_buffer->buffer, EVBUFFER_LENGTH(req->input_buffer));
09802ddd »
2009-08-26 Adding support for multipart.
81 evbuffer_add_printf(client->buf, "--%s\n", BOUNDARY);
82
83 /* evbuffer_add(client->buf, "\r\n", 2); */
84
eeebb66f »
2009-08-21 initial import of pubsub
85 printf("sending to client\n");
86 evhttp_send_reply_chunk(client->req, client->buf);
87 i++;
88 }
5b0770fd »
2009-08-25 Fixed args.
89
eeebb66f »
2009-08-21 initial import of pubsub
90 evbuffer_add_printf(evb, "Published to %d clients.\n", i);
91 evhttp_send_reply(req, HTTP_OK, "OK", evb);
92 }
93
94 void sub_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
95 {
96 struct cli *client;
97
98 printf("sub_cb\n");
1604d507 »
2009-08-25 Added stats.
99 currentConns++;
100 totalConns++;
eeebb66f »
2009-08-21 initial import of pubsub
101 client = calloc(1, sizeof(*client));
102 client->req = req;
103 client->buf = evbuffer_new();
09802ddd »
2009-08-26 Adding support for multipart.
104 evhttp_add_header(client->req->output_headers, "Content-Type",
105 "multipart/x-mixed-replace; boundary=" BOUNDARY);
106 evbuffer_add_printf(client->buf, "--%s\n", BOUNDARY);
107 evhttp_send_reply_start(client->req, HTTP_OK, "OK");
108 evhttp_send_reply_chunk(client->req, client->buf);
eeebb66f »
2009-08-21 initial import of pubsub
109 TAILQ_INSERT_TAIL(&clients, client, entries);
110 evhttp_connection_set_closecb(req->evcon, on_close, (void *)client);
111 }
112
113 int
114 main(int argc, char **argv)
115 {
116 TAILQ_INIT(&clients);
117 simplehttp_init();
118 simplehttp_set_cb("/pub*", pub_cb, NULL);
119 simplehttp_set_cb("/sub*", sub_cb, NULL);
38c69fdd »
2009-08-26 Moved stats to HTTP response headers.
120 simplehttp_set_cb("/stats*", stats_cb, NULL);
eeebb66f »
2009-08-21 initial import of pubsub
121 simplehttp_main(argc, argv);
122
123 return 0;
124 }
Something went wrong with that request. Please try again.