~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/json_server/json_server.cc

  • Committer: Stewart Smith
  • Date: 2011-04-21 01:27:52 UTC
  • mto: (2295.1.1 drizzle-ga)
  • mto: This revision was merged to the branch mainline in revision 2310.
  • Revision ID: stewart@flamingspork.com-20110421012752-0xnzsx01gizd5dm6
very basic HTTP server

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2011 Stewart Smith
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
 
 
22
#include <config.h>
 
23
#include <drizzled/module/module.h>
 
24
#include <drizzled/module/context.h>
 
25
#include <drizzled/plugin/plugin.h>
 
26
#include <drizzled/plugin.h>
 
27
#include <drizzled/sys_var.h>
 
28
#include <drizzled/gettext.h>
 
29
#include <drizzled/error.h>
 
30
#include <drizzled/query_id.h>
 
31
#include <drizzled/session.h>
 
32
#include <drizzled/internal/my_sys.h>
 
33
#include <drizzled/internal/m_string.h>
 
34
#include <algorithm>
 
35
#include <iostream>
 
36
#include <boost/program_options.hpp>
 
37
#include <drizzled/module/option_map.h>
 
38
#include <drizzled/constrained_value.h>
 
39
#include <evhttp.h>
 
40
#include <pthread.h>
 
41
 
 
42
namespace po= boost::program_options;
 
43
using namespace drizzled;
 
44
using namespace std;
 
45
 
 
46
namespace drizzle_plugin
 
47
{
 
48
namespace json_server
 
49
{
 
50
 
 
51
static port_constraint port;
 
52
struct event_base *base= NULL;
 
53
struct evhttp *httpd;
 
54
 
 
55
static in_port_t getPort(void)
 
56
{
 
57
  return port.get();
 
58
}
 
59
 
 
60
extern "C" void process_request(struct evhttp_request *req, void* );
 
61
extern "C" void process_root_request(struct evhttp_request *req, void* );
 
62
 
 
63
extern "C" void process_request(struct evhttp_request *req, void* )
 
64
{
 
65
  struct evbuffer *buf = evbuffer_new();
 
66
  if (buf == NULL) return;
 
67
  evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));
 
68
  evhttp_send_reply(req, HTTP_OK, "OK", buf);
 
69
}
 
70
 
 
71
extern "C" void process_root_request(struct evhttp_request *req, void* )
 
72
{
 
73
  struct evbuffer *buf = evbuffer_new();
 
74
  if (buf == NULL) return;
 
75
  evbuffer_add_printf(buf, "Handling root request for URI %s\n", evhttp_request_uri(req));
 
76
  evhttp_send_reply(req, HTTP_OK, "OK", buf);
 
77
}
 
78
 
 
79
extern "C" void *libevent_thread(void*);
 
80
 
 
81
extern "C" void *libevent_thread(void*)
 
82
{
 
83
  event_base_dispatch(base);
 
84
  evhttp_free(httpd);
 
85
  return NULL;
 
86
}
 
87
 
 
88
static int json_server_init(drizzled::module::Context &context)
 
89
{
 
90
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
 
91
 
 
92
  base= event_init();
 
93
  httpd= evhttp_new(base);
 
94
  if (httpd == NULL)
 
95
    return -1;
 
96
 
 
97
  int r= evhttp_bind_socket(httpd, "0.0.0.0", getPort());
 
98
 
 
99
  if (r != 0)
 
100
    return -2;
 
101
 
 
102
  evhttp_set_cb(httpd, "/", process_root_request, NULL);
 
103
  evhttp_set_gencb(httpd, process_request, NULL);
 
104
 
 
105
  pthread_t libevent_loop_thread;
 
106
 
 
107
  pthread_create(&libevent_loop_thread, NULL, libevent_thread, NULL);
 
108
 
 
109
  return 0;
 
110
}
 
111
 
 
112
static void init_options(drizzled::module::option_context &context)
 
113
{
 
114
  context("port",
 
115
          po::value<port_constraint>(&port)->default_value(80),
 
116
          _("Port number to use for connection or 0 for default (port 80) "));
 
117
}
 
118
 
 
119
} /* namespace json_server */
 
120
} /* namespace drizzle_plugin */
 
121
 
 
122
DRIZZLE_DECLARE_PLUGIN
 
123
{
 
124
  DRIZZLE_VERSION_ID,
 
125
  "json-server",
 
126
  "0.1",
 
127
  "Stewart Smith",
 
128
  "JSON HTTP interface",
 
129
  PLUGIN_LICENSE_GPL,
 
130
  drizzle_plugin::json_server::json_server_init,             /* Plugin Init */
 
131
  NULL, /* depends */
 
132
  drizzle_plugin::json_server::init_options    /* config options */
 
133
}
 
134
DRIZZLE_DECLARE_PLUGIN_END;