Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 217 lines (189 sloc) 5.643 kB
5b25a13 @nathanfolkman New.
nathanfolkman authored
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
c2cd618 @jayridge fixes for linux compile
jayridge authored
4 #include <signal.h>
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
5 // from ../server
6 #include "queue.h"
7 #include "simplehttp.h"
5b25a13 @nathanfolkman New.
nathanfolkman authored
8
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
9 struct queue_entry {
10 TAILQ_ENTRY(queue_entry) entries;
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
11 size_t bytes;
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
12 char data[1];
13 };
5b25a13 @nathanfolkman New.
nathanfolkman authored
14
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
15 TAILQ_HEAD(, queue_entry) queues;
5b25a13 @nathanfolkman New.
nathanfolkman authored
16
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
17 char *progname = "simplequeue";
18 char *version = "1.1";
19 char *overflow_log = NULL;
20 FILE *overflow_log_fp = NULL;
21 uint64_t max_depth = 0;
22 size_t max_bytes = 0;
23
24 uint64_t depth = 0;
25 uint64_t depth_high_water = 0;
26 uint64_t n_puts = 0;
27 uint64_t n_gets = 0;
28 uint64_t n_overflow = 0;
29 size_t n_bytes = 0;
30
31 void
32 hup_handler(int signum)
33 {
34 signal(SIGHUP, hup_handler);
35 if (overflow_log_fp) {
36 fclose(overflow_log_fp);
37 }
38 if (overflow_log) {
39 overflow_log_fp = fopen(overflow_log, "a");
40 if (!overflow_log_fp) {
41 perror("fopen failed: ");
42 exit(1);
43 }
44 fprintf(stdout, "opened overflow_log: %s\n", overflow_log);
45 }
46 }
47
48 void
49 overflow_one()
50 {
51 struct queue_entry *entry;
52
53 entry = TAILQ_FIRST(&queues);
54 if (entry != NULL) {
55 TAILQ_REMOVE(&queues, entry, entries);
56 fwrite(entry->data, entry->bytes, 1, overflow_log_fp);
57 fwrite("\n", 1, 1, overflow_log_fp);
58 n_bytes -= entry->bytes;
59 depth--;
60 n_overflow++;
61 free(entry);
62 }
63 }
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
64
65 void
66 stats(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
67 {
c56b689 @nathanfolkman Reverted.
nathanfolkman authored
68 struct evkeyvalq args;
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
69 const char *reset;
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
70
f7b1992 @jehiah update simplequeue to use proper installed version of simplehttp, and…
jehiah authored
71 evhttp_parse_query(req->uri, &args);
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
72 reset = evhttp_find_header(&args, "reset");
73 if (reset != NULL && strcmp(reset, "1") == 0) {
74 depth_high_water = 0;
75 n_puts = 0;
76 n_gets = 0;
77 } else {
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
78 evbuffer_add_printf(evb, "puts:%lld\n", n_puts);
79 evbuffer_add_printf(evb, "gets:%lld\n", n_gets);
80 evbuffer_add_printf(evb, "depth:%lld\n", depth);
81 evbuffer_add_printf(evb, "depth_high_water:%lld\n", depth_high_water);
82 evbuffer_add_printf(evb, "bytes:%ld\n", n_bytes);
83 evbuffer_add_printf(evb, "overflow:%lld\n", n_overflow);
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
84 }
85
86 evhttp_send_reply(req, HTTP_OK, "OK", evb);
87 evhttp_clear_headers(&args);
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
88 }
89
90 void
91 get(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
92 {
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
93 struct queue_entry *entry;
94
95 n_gets++;
96 entry = TAILQ_FIRST(&queues);
97 if (entry != NULL) {
98 evbuffer_add_printf(evb, "%s", entry->data);
99 TAILQ_REMOVE(&queues, entry, entries);
100 free(entry);
101 depth--;
342d871 @nathanfolkman Adding persistent, per-queue metrics.
nathanfolkman authored
102 }
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
103
104 evhttp_send_reply(req, HTTP_OK, "OK", evb);
342d871 @nathanfolkman Adding persistent, per-queue metrics.
nathanfolkman authored
105 }
106
5b25a13 @nathanfolkman New.
nathanfolkman authored
107 void
108 put(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
109 {
110 struct evkeyvalq args;
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
111 struct queue_entry *entry;
112 const char *data;
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
113 size_t size;
5b25a13 @nathanfolkman New.
nathanfolkman authored
114
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
115 n_puts++;
f7b1992 @jehiah update simplequeue to use proper installed version of simplehttp, and…
jehiah authored
116 evhttp_parse_query(req->uri, &args);
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
117 data = evhttp_find_header(&args, "data");
118 if (data == NULL) {
119 evbuffer_add_printf(evb, "%s\n", "missing data");
120 evhttp_send_reply(req, HTTP_BADREQUEST, "OK", evb);
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
121 evhttp_clear_headers(&args);
09f9757 @nathanfolkman Added check for NULL data in put.
nathanfolkman authored
122 return;
123 }
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
124
125 evhttp_send_reply(req, HTTP_OK, "OK", evb);
126
127 size = strlen(data);
128 entry = malloc(sizeof(*entry)+size);
129 entry->bytes = size;
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
130 strcpy(entry->data, data);
131 TAILQ_INSERT_TAIL(&queues, entry, entries);
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
132 n_bytes += size;
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
133 depth++;
648a7c6 @nathanfolkman Fixes to stats reset.
nathanfolkman authored
134 if (depth > depth_high_water) {
135 depth_high_water = depth;
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
136 }
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
137 while ((max_depth > 0 && depth > max_depth)
138 || (max_bytes > 0 && n_bytes > max_bytes)) {
139 overflow_one();
140 }
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
141 evhttp_clear_headers(&args);
142 }
342d871 @nathanfolkman Adding persistent, per-queue metrics.
nathanfolkman authored
143
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
144 void
145 dump(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
146 {
147 struct queue_entry *entry;
148
149 TAILQ_FOREACH(entry, &queues, entries) {
150 evbuffer_add_printf(evb, "%s\n", entry->data);
151 }
152
153 evhttp_send_reply(req, HTTP_OK, "OK", evb);
5b25a13 @nathanfolkman New.
nathanfolkman authored
154 }
155
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
156 void usage()
157 {
158 fprintf(stderr, "%s: A simple http buffer queue.\n", progname);
159 fprintf(stderr, "Version %s, http://code.google.com/p/simplehttp/\n", version);
160 fprintf(stderr, "\n");
161 fprintf(stderr, "usage:\n");
162 fprintf(stderr, " %s -- [--overflow_log] [--max_bytes] [--max_depth]\n", progname);
163 fprintf(stderr, "\n");
164 exit(1);
165 }
166
5b25a13 @nathanfolkman New.
nathanfolkman authored
167 int
168 main(int argc, char **argv)
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
169 {
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
170 int i;
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
171 TAILQ_INIT(&queues);
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
172
173 for (i=1; i < argc; i++) {
174 if(!strcmp(argv[i], "--overflow_log")) {
175 if(++i >= argc) usage();
176 overflow_log = argv[i];
177 } else if(!strcmp(argv[i], "--max_bytes")) {
178 if(++i >= argc) usage();
179 max_bytes = strtod(argv[i], (char **) NULL);
180 } else if(!strcmp(argv[i], "--max_depth")) {
181 if(++i >= argc) usage();
182 max_depth = strtod(argv[i], (char **) NULL);
183 fprintf(stdout, "max_depth set to %lld\n", max_depth);
184 } else if (!strcmp(argv[i], "--help")) {
185 usage();
186 }
187 }
5b25a13 @nathanfolkman New.
nathanfolkman authored
188
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
189 if (overflow_log) {
190 overflow_log_fp = fopen(overflow_log, "a");
191 if (!overflow_log_fp) {
192 perror("fopen failed: ");
193 exit(1);
194 }
195 fprintf(stdout, "opened overflow_log: %s\n", overflow_log);
196 }
197
198 fprintf(stderr, "Version %s, http://code.google.com/p/simplehttp/\n", version);
199 fprintf(stderr, "\"%s -- --help\" for options\n", progname);
5b25a13 @nathanfolkman New.
nathanfolkman authored
200 simplehttp_init();
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
201 signal(SIGHUP, hup_handler);
5b25a13 @nathanfolkman New.
nathanfolkman authored
202 simplehttp_set_cb("/put*", put, NULL);
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
203 simplehttp_set_cb("/get*", get, NULL);
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
204 simplehttp_set_cb("/dump*", dump, NULL);
5e30dc3 @nathanfolkman Added check for debug flag.
nathanfolkman authored
205 simplehttp_set_cb("/stats*", stats, NULL);
5b25a13 @nathanfolkman New.
nathanfolkman authored
206 simplehttp_main(argc, argv);
207
e45b1e9 @jayridge Added max_depth/max_bytes/overflow_log. Also removed unused json dep …
jayridge authored
208 if (overflow_log_fp) {
209 while (depth) {
210 overflow_one();
211 }
212 fclose(overflow_log_fp);
213 }
5b25a13 @nathanfolkman New.
nathanfolkman authored
214 return 0;
9ac1d0d @nathanfolkman Fixed args.
nathanfolkman authored
215 }
0fc7263 @jehiah revert to rev33 which was buildable; revs between then started to add…
jehiah authored
216
Something went wrong with that request. Please try again.