~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/drizzle_protocol.cc

  • Committer: Andrew Hutchings
  • Date: 2010-11-01 22:04:06 UTC
  • mfrom: (1897 merge)
  • mto: This revision was merged to the branch mainline in revision 1907.
  • Revision ID: andrew@linuxjedi.co.uk-20101101220406-nj6i29uzja17u1hn
Merge trunk into branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
#include <iostream>
32
32
#include <boost/program_options.hpp>
33
33
#include <drizzled/module/option_map.h>
34
 
#include "drizzled/util/tokenize.h"
35
34
#include "drizzle_protocol.h"
36
35
#include "plugin/drizzle_protocol/status_table.h"
37
36
 
39
38
using namespace drizzled;
40
39
using namespace std;
41
40
 
42
 
namespace drizzle_plugin
43
 
{
44
41
namespace drizzle_protocol
45
42
{
46
43
 
47
 
std::vector<std::string> ClientDrizzleProtocol::drizzle_admin_ip_addresses;
48
 
static port_constraint port;
49
 
static timeout_constraint connect_timeout;
50
 
static timeout_constraint read_timeout;
51
 
static timeout_constraint write_timeout;
52
 
static retry_constraint retry_count;
53
 
static buffer_constraint buffer_length;
 
44
static uint32_t port;
 
45
static uint32_t connect_timeout;
 
46
static uint32_t read_timeout;
 
47
static uint32_t write_timeout;
 
48
static uint32_t retry_count;
 
49
static uint32_t buffer_length;
 
50
static char* bind_address;
54
51
 
55
52
static const uint32_t DRIZZLE_TCP_PORT= 4427;
56
53
 
57
 
ProtocolCounters *ListenDrizzleProtocol::drizzle_counters= new ProtocolCounters();
58
 
 
59
54
ListenDrizzleProtocol::~ListenDrizzleProtocol()
60
55
{
 
56
  /* This is strdup'd from the options */
 
57
  free(bind_address);
 
58
}
 
59
 
 
60
const char* ListenDrizzleProtocol::getHost(void) const
 
61
{
 
62
  return bind_address;
61
63
}
62
64
 
63
65
in_port_t ListenDrizzleProtocol::getPort(void) const
64
66
{
65
 
  return port;
66
 
}
67
 
 
68
 
void ClientDrizzleProtocol::drizzle_compose_ip_addresses(vector<string> options)
69
 
{
70
 
  for (vector<string>::iterator it= options.begin();
71
 
       it != options.end();
72
 
       ++it)
73
 
  {
74
 
    tokenize(*it, drizzle_admin_ip_addresses, ",", true);
75
 
  }
76
 
}
77
 
 
78
 
bool ClientDrizzleProtocol::isAdminAllowed(void)
79
 
{
80
 
  if (std::find(drizzle_admin_ip_addresses.begin(), drizzle_admin_ip_addresses.end(), session->user()->address()) != drizzle_admin_ip_addresses.end())
81
 
    return true;
82
 
 
83
 
  return false;
84
 
}
85
 
 
86
 
plugin::Client *ListenDrizzleProtocol::getClient(int fd)
87
 
{
88
 
  int new_fd;
89
 
  new_fd= acceptTcp(fd);
90
 
  if (new_fd == -1)
91
 
    return NULL;
92
 
 
93
 
  return new ClientDrizzleProtocol(new_fd, getCounters());
 
67
  return (in_port_t) port;
94
68
}
95
69
 
96
70
static int init(drizzled::module::Context &context)
97
71
{  
98
72
  const module::option_map &vm= context.getOptions();
 
73
  if (vm.count("port"))
 
74
  { 
 
75
    if (port > 65535)
 
76
    {
 
77
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
 
78
      exit(-1);
 
79
    }
 
80
  }
 
81
 
 
82
  if (vm.count("connect-timeout"))
 
83
  {
 
84
    if (connect_timeout < 1 || connect_timeout > 300)
 
85
    {
 
86
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
 
87
      exit(-1);
 
88
    }
 
89
  }
 
90
 
 
91
  if (vm.count("read-timeout"))
 
92
  {
 
93
    if (read_timeout < 1 || read_timeout > 300)
 
94
    {
 
95
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
 
96
      exit(-1);
 
97
    }
 
98
  }
 
99
 
 
100
  if (vm.count("write-timeout"))
 
101
  {
 
102
    if (write_timeout < 1 || write_timeout > 300)
 
103
    {
 
104
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
 
105
      exit(-1);
 
106
    }
 
107
  }
 
108
 
 
109
  if (vm.count("retry-count"))
 
110
  {
 
111
    if (retry_count < 1 || retry_count > 100)
 
112
    {
 
113
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
 
114
      exit(-1);
 
115
    }
 
116
  }
 
117
 
 
118
  if (vm.count("buffer-length"))
 
119
  {
 
120
    if (buffer_length < 1024 || buffer_length > 1024*1024)
 
121
    {
 
122
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
 
123
      exit(-1);
 
124
    }
 
125
  }
 
126
 
 
127
  if (vm.count("bind-address"))
 
128
  {
 
129
    bind_address= strdup(vm["bind-address"].as<string>().c_str());
 
130
  }
 
131
 
 
132
  else
 
133
  {
 
134
    bind_address= NULL;
 
135
  }
99
136
 
100
137
  context.add(new StatusTable);
101
 
  context.add(new ListenDrizzleProtocol("drizzle_protocol", vm["bind-address"].as<std::string>(), true));
102
 
  context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
103
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("connect_timeout", connect_timeout));
104
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("read_timeout", read_timeout));
105
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("write_timeout", write_timeout));
106
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("retry_count", retry_count));
107
 
  context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("buffer_length", buffer_length));
108
 
  context.registerVariable(new sys_var_const_string_val("bind_address",
109
 
                                                        vm["bind-address"].as<std::string>()));
110
 
 
111
 
  context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenDrizzleProtocol::drizzle_counters->max_connections));
 
138
  context.add(new ListenDrizzleProtocol("drizzle_protocol", true));
112
139
 
113
140
  return 0;
114
141
}
115
142
 
 
143
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
 
144
                           N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."),
 
145
                           NULL, NULL, DRIZZLE_TCP_PORT, 0, 65535, 0);
 
146
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
 
147
                           PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
 
148
                           NULL, NULL, 10, 1, 300, 0);
 
149
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
 
150
                           N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
 
151
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
 
152
                           N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
 
153
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
 
154
                           N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
 
155
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
 
156
                           N_("Buffer length."), NULL, NULL, 16384, 1024,
 
157
                           1024*1024, 0);
 
158
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
 
159
                          N_("Address to bind to."), NULL, NULL, NULL);
116
160
 
117
161
static void init_options(drizzled::module::option_context &context)
118
162
{
119
163
  context("port",
120
 
          po::value<port_constraint>(&port)->default_value(DRIZZLE_TCP_PORT),
 
164
          po::value<uint32_t>(&port)->default_value(DRIZZLE_TCP_PORT),
121
165
          N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."));
122
166
  context("connect-timeout",
123
 
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
 
167
          po::value<uint32_t>(&connect_timeout)->default_value(10),
124
168
          N_("Connect Timeout."));
125
169
  context("read-timeout",
126
 
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
 
170
          po::value<uint32_t>(&read_timeout)->default_value(30),
127
171
          N_("Read Timeout."));
128
172
  context("write-timeout",
129
 
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
 
173
          po::value<uint32_t>(&write_timeout)->default_value(60),
130
174
          N_("Write Timeout."));
131
175
  context("retry-count",
132
 
          po::value<retry_constraint>(&retry_count)->default_value(10),
 
176
          po::value<uint32_t>(&retry_count)->default_value(10),
133
177
          N_("Retry Count."));
134
178
  context("buffer-length",
135
 
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
 
179
          po::value<uint32_t>(&buffer_length)->default_value(16384),
136
180
          N_("Buffer length."));
137
181
  context("bind-address",
138
 
          po::value<std::string>()->default_value(""),
 
182
          po::value<string>(),
139
183
          N_("Address to bind to."));
140
 
  context("max-connections",
141
 
          po::value<uint32_t>(&ListenDrizzleProtocol::drizzle_counters->max_connections)->default_value(1000),
142
 
          N_("Maximum simultaneous connections."));
143
 
  context("admin-ip-addresses",
144
 
          po::value<vector<string> >()->composing()->notifier(&ClientDrizzleProtocol::drizzle_compose_ip_addresses),
145
 
          N_("A restrictive IP address list for incoming admin connections."));
146
184
}
147
185
 
 
186
static drizzle_sys_var* sys_variables[]= {
 
187
  DRIZZLE_SYSVAR(port),
 
188
  DRIZZLE_SYSVAR(connect_timeout),
 
189
  DRIZZLE_SYSVAR(read_timeout),
 
190
  DRIZZLE_SYSVAR(write_timeout),
 
191
  DRIZZLE_SYSVAR(retry_count),
 
192
  DRIZZLE_SYSVAR(buffer_length),
 
193
  DRIZZLE_SYSVAR(bind_address),
 
194
  NULL
 
195
};
 
196
 
148
197
} /* namespace drizzle_protocol */
149
 
} /* namespace drizzle_plugin */
150
198
 
151
 
DRIZZLE_PLUGIN(drizzle_plugin::drizzle_protocol::init, NULL, drizzle_plugin::drizzle_protocol::init_options);
 
199
DRIZZLE_PLUGIN(drizzle_protocol::init, drizzle_protocol::sys_variables, drizzle_protocol::init_options);