~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/drizzle_protocol/drizzle_protocol.cc

  • Committer: Brian Aker
  • Date: 2010-10-22 17:44:34 UTC
  • mto: This revision was merged to the branch mainline in revision 1873.
  • Revision ID: brian@tangent.org-20101022174434-q8fjovcpclzqer7n
TableShare is no longer in the house (i.e. we no longer directly have a copy
of it in cursor).

One more bit of the knot now gone.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
 
4
<<<<<<< TREE
4
5
 *  Copyright (C) 2010 Brian Aker
5
6
 *
6
7
 *  This program is free software; you can redistribute it and/or modify
16
17
 *  You should have received a copy of the GNU General Public License
17
18
 *  along with this program; if not, write to the Free Software
18
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
=======
 
21
 *  Copyright (C) 2008 Sun Microsystems
 
22
 *
 
23
 * This program is free software; you can redistribute it and/or modify
 
24
 * it under the terms of the GNU General Public License as published by
 
25
 * the Free Software Foundation; version 2 of the License.
 
26
 *
 
27
 * This program is distributed in the hope that it will be useful,
 
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
30
 * GNU General Public License for more details.
 
31
 *
 
32
 * You should have received a copy of the GNU General Public License
 
33
 * along with this program; if not, write to the Free Software
 
34
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
35
>>>>>>> MERGE-SOURCE
19
36
 */
20
37
 
21
38
 
31
48
#include <iostream>
32
49
#include <boost/program_options.hpp>
33
50
#include <drizzled/module/option_map.h>
34
 
#include "drizzled/util/tokenize.h"
35
51
#include "drizzle_protocol.h"
36
52
#include "plugin/drizzle_protocol/status_table.h"
37
53
 
39
55
using namespace drizzled;
40
56
using namespace std;
41
57
 
42
 
namespace drizzle_plugin
43
 
{
44
58
namespace drizzle_protocol
45
59
{
46
60
 
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;
 
61
static uint32_t port;
 
62
static uint32_t connect_timeout;
 
63
static uint32_t read_timeout;
 
64
static uint32_t write_timeout;
 
65
static uint32_t retry_count;
 
66
static uint32_t buffer_length;
 
67
static char* bind_address;
54
68
 
55
69
static const uint32_t DRIZZLE_TCP_PORT= 4427;
56
70
 
57
 
ProtocolCounters *ListenDrizzleProtocol::drizzle_counters= new ProtocolCounters();
58
 
 
59
71
ListenDrizzleProtocol::~ListenDrizzleProtocol()
60
72
{
 
73
  /* This is strdup'd from the options */
 
74
  free(bind_address);
 
75
}
 
76
 
 
77
const char* ListenDrizzleProtocol::getHost(void) const
 
78
{
 
79
  return bind_address;
61
80
}
62
81
 
63
82
in_port_t ListenDrizzleProtocol::getPort(void) const
64
83
{
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->getSecurityContext().getIp()) != drizzle_admin_ip_addresses.end())
81
 
    return true;
82
 
  else
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());
 
84
  return (in_port_t) port;
94
85
}
95
86
 
96
87
static int init(drizzled::module::Context &context)
97
88
{  
98
89
  const module::option_map &vm= context.getOptions();
 
90
  if (vm.count("port"))
 
91
  { 
 
92
    if (port > 65535)
 
93
    {
 
94
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value of port\n"));
 
95
      exit(-1);
 
96
    }
 
97
  }
 
98
 
 
99
  if (vm.count("connect-timeout"))
 
100
  {
 
101
    if (connect_timeout < 1 || connect_timeout > 300)
 
102
    {
 
103
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for connect_timeout\n"));
 
104
      exit(-1);
 
105
    }
 
106
  }
 
107
 
 
108
  if (vm.count("read-timeout"))
 
109
  {
 
110
    if (read_timeout < 1 || read_timeout > 300)
 
111
    {
 
112
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for read_timeout\n"));
 
113
      exit(-1);
 
114
    }
 
115
  }
 
116
 
 
117
  if (vm.count("write-timeout"))
 
118
  {
 
119
    if (write_timeout < 1 || write_timeout > 300)
 
120
    {
 
121
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for write_timeout\n"));
 
122
      exit(-1);
 
123
    }
 
124
  }
 
125
 
 
126
  if (vm.count("retry-count"))
 
127
  {
 
128
    if (retry_count < 1 || retry_count > 100)
 
129
    {
 
130
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for retry_count"));
 
131
      exit(-1);
 
132
    }
 
133
  }
 
134
 
 
135
  if (vm.count("buffer-length"))
 
136
  {
 
137
    if (buffer_length < 1024 || buffer_length > 1024*1024)
 
138
    {
 
139
      errmsg_printf(ERRMSG_LVL_ERROR, _("Invalid value for buffer_length\n"));
 
140
      exit(-1);
 
141
    }
 
142
  }
 
143
 
 
144
  if (vm.count("bind-address"))
 
145
  {
 
146
    bind_address= strdup(vm["bind-address"].as<string>().c_str());
 
147
  }
 
148
 
 
149
  else
 
150
  {
 
151
    bind_address= NULL;
 
152
  }
99
153
 
100
154
  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));
 
155
  context.add(new ListenDrizzleProtocol("drizzle_protocol", true));
112
156
 
113
157
  return 0;
114
158
}
115
159
 
 
160
static DRIZZLE_SYSVAR_UINT(port, port, PLUGIN_VAR_RQCMDARG,
 
161
                           N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."),
 
162
                           NULL, NULL, DRIZZLE_TCP_PORT, 0, 65535, 0);
 
163
static DRIZZLE_SYSVAR_UINT(connect_timeout, connect_timeout,
 
164
                           PLUGIN_VAR_RQCMDARG, N_("Connect Timeout."),
 
165
                           NULL, NULL, 10, 1, 300, 0);
 
166
static DRIZZLE_SYSVAR_UINT(read_timeout, read_timeout, PLUGIN_VAR_RQCMDARG,
 
167
                           N_("Read Timeout."), NULL, NULL, 30, 1, 300, 0);
 
168
static DRIZZLE_SYSVAR_UINT(write_timeout, write_timeout, PLUGIN_VAR_RQCMDARG,
 
169
                           N_("Write Timeout."), NULL, NULL, 60, 1, 300, 0);
 
170
static DRIZZLE_SYSVAR_UINT(retry_count, retry_count, PLUGIN_VAR_RQCMDARG,
 
171
                           N_("Retry Count."), NULL, NULL, 10, 1, 100, 0);
 
172
static DRIZZLE_SYSVAR_UINT(buffer_length, buffer_length, PLUGIN_VAR_RQCMDARG,
 
173
                           N_("Buffer length."), NULL, NULL, 16384, 1024,
 
174
                           1024*1024, 0);
 
175
static DRIZZLE_SYSVAR_STR(bind_address, bind_address, PLUGIN_VAR_READONLY,
 
176
                          N_("Address to bind to."), NULL, NULL, NULL);
116
177
 
117
178
static void init_options(drizzled::module::option_context &context)
118
179
{
119
180
  context("port",
120
 
          po::value<port_constraint>(&port)->default_value(DRIZZLE_TCP_PORT),
 
181
          po::value<uint32_t>(&port)->default_value(DRIZZLE_TCP_PORT),
121
182
          N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."));
122
183
  context("connect-timeout",
123
 
          po::value<timeout_constraint>(&connect_timeout)->default_value(10),
 
184
          po::value<uint32_t>(&connect_timeout)->default_value(10),
124
185
          N_("Connect Timeout."));
125
186
  context("read-timeout",
126
 
          po::value<timeout_constraint>(&read_timeout)->default_value(30),
 
187
          po::value<uint32_t>(&read_timeout)->default_value(30),
127
188
          N_("Read Timeout."));
128
189
  context("write-timeout",
129
 
          po::value<timeout_constraint>(&write_timeout)->default_value(60),
 
190
          po::value<uint32_t>(&write_timeout)->default_value(60),
130
191
          N_("Write Timeout."));
131
192
  context("retry-count",
132
 
          po::value<retry_constraint>(&retry_count)->default_value(10),
 
193
          po::value<uint32_t>(&retry_count)->default_value(10),
133
194
          N_("Retry Count."));
134
195
  context("buffer-length",
135
 
          po::value<buffer_constraint>(&buffer_length)->default_value(16384),
 
196
          po::value<uint32_t>(&buffer_length)->default_value(16384),
136
197
          N_("Buffer length."));
137
198
  context("bind-address",
138
 
          po::value<std::string>()->default_value(""),
 
199
          po::value<string>(),
139
200
          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
201
}
147
202
 
 
203
static drizzle_sys_var* sys_variables[]= {
 
204
  DRIZZLE_SYSVAR(port),
 
205
  DRIZZLE_SYSVAR(connect_timeout),
 
206
  DRIZZLE_SYSVAR(read_timeout),
 
207
  DRIZZLE_SYSVAR(write_timeout),
 
208
  DRIZZLE_SYSVAR(retry_count),
 
209
  DRIZZLE_SYSVAR(buffer_length),
 
210
  DRIZZLE_SYSVAR(bind_address),
 
211
  NULL
 
212
};
 
213
 
148
214
} /* namespace drizzle_protocol */
149
 
} /* namespace drizzle_plugin */
150
215
 
151
 
DRIZZLE_PLUGIN(drizzle_plugin::drizzle_protocol::init, NULL, drizzle_plugin::drizzle_protocol::init_options);
 
216
DRIZZLE_PLUGIN(drizzle_protocol::init, drizzle_protocol::sys_variables, drizzle_protocol::init_options);