Drizzled Public API Documentation

drizzle_protocol.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 #include <config.h>
00023 #include <drizzled/gettext.h>
00024 #include <drizzled/error.h>
00025 #include <drizzled/query_id.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 #include <drizzled/internal/m_string.h>
00029 #include <algorithm>
00030 #include <iostream>
00031 #include <boost/program_options.hpp>
00032 #include <drizzled/module/option_map.h>
00033 #include <drizzled/util/tokenize.h>
00034 #include "drizzle_protocol.h"
00035 
00036 namespace po= boost::program_options;
00037 using namespace drizzled;
00038 using namespace std;
00039 
00040 namespace drizzle_plugin
00041 {
00042 namespace drizzle_protocol
00043 {
00044 
00045 std::vector<std::string> ClientDrizzleProtocol::drizzle_admin_ip_addresses;
00046 static port_constraint port;
00047 static timeout_constraint connect_timeout;
00048 static timeout_constraint read_timeout;
00049 static timeout_constraint write_timeout;
00050 static retry_constraint retry_count;
00051 static buffer_constraint buffer_length;
00052 
00053 static const uint32_t DRIZZLE_TCP_PORT= 4427;
00054 
00055 ProtocolCounters *ListenDrizzleProtocol::drizzle_counters= new ProtocolCounters();
00056 
00057 ListenDrizzleProtocol::~ListenDrizzleProtocol()
00058 {
00059 }
00060 
00061 in_port_t ListenDrizzleProtocol::getPort(void) const
00062 {
00063   return port;
00064 }
00065 
00066 void ClientDrizzleProtocol::drizzle_compose_ip_addresses(vector<string> options)
00067 {
00068   for (vector<string>::iterator it= options.begin();
00069        it != options.end();
00070        ++it)
00071   {
00072     tokenize(*it, drizzle_admin_ip_addresses, ",", true);
00073   }
00074 }
00075 
00076 bool ClientDrizzleProtocol::isAdminAllowed(void)
00077 {
00078   if (std::find(drizzle_admin_ip_addresses.begin(), drizzle_admin_ip_addresses.end(), session->user()->address()) != drizzle_admin_ip_addresses.end())
00079     return true;
00080 
00081   return false;
00082 }
00083 
00084 plugin::Client *ListenDrizzleProtocol::getClient(int fd)
00085 {
00086   int new_fd;
00087   new_fd= acceptTcp(fd);
00088   if (new_fd == -1)
00089     return NULL;
00090 
00091   return new ClientDrizzleProtocol(new_fd, getCounters());
00092 }
00093 
00094 static int init(drizzled::module::Context &context)
00095 {  
00096   const module::option_map &vm= context.getOptions();
00097 
00098   ListenDrizzleProtocol *protocol=new ListenDrizzleProtocol("drizzle_protocol", vm["bind-address"].as<std::string>(), true);
00099   protocol->addCountersToTable();
00100   context.add(protocol);
00101   context.registerVariable(new sys_var_constrained_value_readonly<in_port_t>("port", port));
00102   context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("connect_timeout", connect_timeout));
00103   context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("read_timeout", read_timeout));
00104   context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("write_timeout", write_timeout));
00105   context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("retry_count", retry_count));
00106   context.registerVariable(new sys_var_constrained_value_readonly<uint32_t>("buffer_length", buffer_length));
00107   context.registerVariable(new sys_var_const_string_val("bind_address",
00108                                                         vm["bind-address"].as<std::string>()));
00109 
00110   context.registerVariable(new sys_var_uint32_t_ptr("max-connections", &ListenDrizzleProtocol::drizzle_counters->max_connections));
00111 
00112   return 0;
00113 }
00114 
00115 
00116 static void init_options(drizzled::module::option_context &context)
00117 {
00118   context("port",
00119           po::value<port_constraint>(&port)->default_value(DRIZZLE_TCP_PORT),
00120           N_("Port number to use for connection or 0 for default to with Drizzle/MySQL protocol."));
00121   context("connect-timeout",
00122           po::value<timeout_constraint>(&connect_timeout)->default_value(10),
00123           N_("Connect Timeout."));
00124   context("read-timeout",
00125           po::value<timeout_constraint>(&read_timeout)->default_value(30),
00126           N_("Read Timeout."));
00127   context("write-timeout",
00128           po::value<timeout_constraint>(&write_timeout)->default_value(60),
00129           N_("Write Timeout."));
00130   context("retry-count",
00131           po::value<retry_constraint>(&retry_count)->default_value(10),
00132           N_("Retry Count."));
00133   context("buffer-length",
00134           po::value<buffer_constraint>(&buffer_length)->default_value(16384),
00135           N_("Buffer length."));
00136   context("bind-address",
00137           po::value<std::string>()->default_value("localhost"),
00138           N_("Address to bind to."));
00139   context("max-connections",
00140           po::value<uint32_t>(&ListenDrizzleProtocol::drizzle_counters->max_connections)->default_value(1000),
00141           N_("Maximum simultaneous connections."));
00142   context("admin-ip-addresses",
00143           po::value<vector<string> >()->composing()->notifier(&ClientDrizzleProtocol::drizzle_compose_ip_addresses),
00144           N_("A restrictive IP address list for incoming admin connections."));
00145 }
00146 
00147 } /* namespace drizzle_protocol */
00148 } /* namespace drizzle_plugin */
00149 
00150 DRIZZLE_PLUGIN(drizzle_plugin::drizzle_protocol::init, NULL, drizzle_plugin::drizzle_protocol::init_options);