Skip to content
This repository
Newer
Older
100644 146 lines (120 sloc) 3.821 kb
eeebb66f » jayridge
2009-08-21 initial import of pubsub
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "simplehttp.h"
5 #include "queue.h"
1604d507 » nathanfolkman
2009-08-25 Added stats.
6 #include "json.h"
eeebb66f » jayridge
2009-08-21 initial import of pubsub
7
9eca1531 » nathanfolkman
2009-08-25 Added BUFSIZE.
8 #define BUFSZ 1024
9
eeebb66f » jayridge
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 » nathanfolkman
2009-08-25 Added stats.
17 void finalize_json(struct evhttp_request *req, struct evbuffer *evb,
18 struct evkeyvalq *args, struct json_object *jsobj);
19 void argtoi(struct evkeyvalq *args, char *key, int *val, int def);
20
21 uint32_t totalConns = 0;
22 uint32_t currentConns = 0;
23 uint32_t msgRecv = 0;
24 uint32_t msgSent = 0;
25
26 void finalize_json(struct evhttp_request *req, struct evbuffer *evb,
27 struct evkeyvalq *args, struct json_object *jsobj)
28 {
29 char *json, *jsonp;
30
31 jsonp = (char *)evhttp_find_header(args, "jsonp");
32 json = json_object_to_json_string(jsobj);
33 if (jsonp) {
34 evbuffer_add_printf(evb, "%s(%s)\n", jsonp, json);
35 } else {
36 evbuffer_add_printf(evb, "%s\n", json);
37 }
38 json_object_put(jsobj); // Odd free function
39
40 evhttp_send_reply(req, HTTP_OK, "OK", evb);
41 evhttp_clear_headers(args);
42 }
43
44 void argtoi(struct evkeyvalq *args, char *key, int *val, int def)
45 {
46 char *tmp;
47
48 *val = def;
49 tmp = (char *)evhttp_find_header(args, (const char *)key);
50 if (tmp) {
51 *val = atoi(tmp);
52 }
53 }
54
55 void
56 stats(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
57 {
58 struct evkeyvalq args;
59 struct json_object *jsobj;
60 int reset;
61 char *uri, *queue, *total_gets, *total_puts, *total;
62 char kbuf[BUFSZ];
63
64 uri = evhttp_decode_uri(req->uri);
65 evhttp_parse_query(uri, &args);
66 free(uri);
67
68 argtoi(&args, "reset", &reset, 0);
69 jsobj = json_object_new_object();
70 json_object_object_add(jsobj, "totalConnections", json_object_new_int(totalConns));
71 json_object_object_add(jsobj, "currentConnections", json_object_new_int(currentConns));
72 json_object_object_add(jsobj, "messagesReceived", json_object_new_int(msgRecv));
73 json_object_object_add(jsobj, "messagesSent", json_object_new_int(msgSent));
74
75 if (reset) {
76 totalConns = 0;
77 currentConns = 0;
78 msgRecv = 0;
79 msgSent = 0;
80 }
81
82 finalize_json(req, evb, &args, jsobj);
83 }
eeebb66f » jayridge
2009-08-21 initial import of pubsub
84
85 void on_close(struct evhttp_connection *evcon, void *ctx)
86 {
87 struct cli *client = (struct cli *)ctx;
88
89 if (client) {
1604d507 » nathanfolkman
2009-08-25 Added stats.
90 currentConns--;
eeebb66f » jayridge
2009-08-21 initial import of pubsub
91 TAILQ_REMOVE(&clients, client, entries);
92 evbuffer_free(client->buf);
93 free(client);
94 }
95 }
96
97 void pub_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
98 {
99 int i = 0;
100 struct cli *client;
1604d507 » nathanfolkman
2009-08-25 Added stats.
101
102 msgRecv++;
eeebb66f » jayridge
2009-08-21 initial import of pubsub
103
104 TAILQ_FOREACH(client, &clients, entries) {
1604d507 » nathanfolkman
2009-08-25 Added stats.
105 msgSent++;
eeebb66f » jayridge
2009-08-21 initial import of pubsub
106 evbuffer_drain(client->buf, EVBUFFER_LENGTH(client->buf));
107 evbuffer_add(client->buf, req->input_buffer->buffer, EVBUFFER_LENGTH(req->input_buffer));
108 evbuffer_add(client->buf, "\r\n", 2);
109 printf("sending to client\n");
110 evhttp_send_reply_chunk(client->req, client->buf);
111 i++;
112 }
113
114 evbuffer_add_printf(evb, "Published to %d clients.\n", i);
115 evhttp_send_reply(req, HTTP_OK, "OK", evb);
116 }
117
118 void sub_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
119 {
120 struct cli *client;
121
122 printf("sub_cb\n");
1604d507 » nathanfolkman
2009-08-25 Added stats.
123 currentConns++;
124 totalConns++;
eeebb66f » jayridge
2009-08-21 initial import of pubsub
125 evhttp_send_reply_start(req, HTTP_OK, "OK");
126
127 client = calloc(1, sizeof(*client));
128 client->req = req;
129 client->buf = evbuffer_new();
130 TAILQ_INSERT_TAIL(&clients, client, entries);
131
132 evhttp_connection_set_closecb(req->evcon, on_close, (void *)client);
133 }
134
135 int
136 main(int argc, char **argv)
137 {
138 TAILQ_INIT(&clients);
139 simplehttp_init();
140 simplehttp_set_cb("/pub*", pub_cb, NULL);
141 simplehttp_set_cb("/sub*", sub_cb, NULL);
1604d507 » nathanfolkman
2009-08-25 Added stats.
142 simplehttp_set_cb("/stats*", stats, NULL);
eeebb66f » jayridge
2009-08-21 initial import of pubsub
143 simplehttp_main(argc, argv);
144
145 return 0;
146 }
Something went wrong with that request. Please try again.