222
228
evhttp_send_reply(req, HTTP_OK, "OK", buf);
225
extern "C" void *libevent_thread(void*);
227
extern "C" void *libevent_thread(void*)
231
static void shutdown_event(int fd, short, void *arg)
233
struct event_base *base= (struct event_base *)arg;
234
event_base_loopbreak(base);
239
static void run(struct event_base *base)
229
241
internal::my_thread_init();
231
243
event_base_dispatch(base);
247
class JsonServer : public drizzled::plugin::Daemon
250
JsonServer(const JsonServer &);
251
JsonServer& operator=(const JsonServer &);
253
drizzled::thread_ptr json_thread;
255
struct evhttp *httpd;
256
struct event_base *base;
258
struct event wakeup_event;
261
JsonServer(in_port_t port_arg) :
262
drizzled::plugin::Daemon("JSON Server"),
270
if (pipe(wakeup_fd) < 0)
277
if ((returned_flags= fcntl(wakeup_fd[0], F_GETFL, 0)) < 0)
279
sql_perror("fcntl:F_GETFL");
283
if (fcntl(wakeup_fd[0], F_SETFL, returned_flags | O_NONBLOCK) < 0)
286
sql_perror("F_SETFL");
290
if ((base= event_init()) == NULL)
292
sql_perror("event_init()");
296
if ((httpd= evhttp_new(base)) == NULL)
298
sql_perror("evhttp_new()");
303
if ((evhttp_bind_socket(httpd, "0.0.0.0", getPort())) == -1)
305
sql_perror("evhttp_bind_socket()");
309
evhttp_set_cb(httpd, "/", process_root_request, NULL);
310
evhttp_set_cb(httpd, "/0.1/version", process_api01_version_req, NULL);
311
evhttp_set_cb(httpd, "/0.1/sql", process_api01_sql_req, NULL);
312
evhttp_set_gencb(httpd, process_request, NULL);
314
event_set(&wakeup_event, wakeup_fd[0], EV_READ | EV_PERSIST, shutdown_event, base);
315
event_base_set(base, &wakeup_event);
316
if (event_add(&wakeup_event, NULL) < 0)
318
sql_perror("event_add");
322
json_thread.reset(new boost::thread((boost::bind(&run, base))));
332
// If we can't write(), we will just muddle our way through the shutdown
335
if ((write(wakeup_fd[1], &buffer, 1)) == 1)
339
event_base_free(base);
236
344
static int json_server_init(drizzled::module::Context &context)
238
346
context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
241
httpd= evhttp_new(base);
245
int r= evhttp_bind_socket(httpd, "0.0.0.0", getPort());
349
context.add(server= new JsonServer(port));
351
if (server and not server->init())
250
evhttp_set_cb(httpd, "/", process_root_request, NULL);
251
evhttp_set_cb(httpd, "/0.1/version", process_api01_version_req, NULL);
252
evhttp_set_cb(httpd, "/0.1/sql", process_api01_sql_req, NULL);
253
evhttp_set_gencb(httpd, process_request, NULL);
255
pthread_t libevent_loop_thread;
257
pthread_create(&libevent_loop_thread, NULL, libevent_thread, NULL);
356
return bool(server) ? 0 : 1;
262
359
static void init_options(drizzled::module::option_context &context)
265
po::value<port_constraint>(&port)->default_value(80),
266
_("Port number to use for connection or 0 for default (port 80) "));
362
po::value<port_constraint>(&port)->default_value(8086),
363
_("Port number to use for connection or 0 for default (port 8086) "));
269
366
} /* namespace json_server */