00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- 00002 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: 00003 * 00004 * Copyright (C) 2011 David Shrewsbury 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 #pragma once 00022 00023 #include <plugin/slave/queue_consumer.h> 00024 #include <plugin/slave/queue_producer.h> 00025 #include <plugin/slave/replication_schema.h> 00026 #include <drizzled/plugin/daemon.h> 00027 #include <boost/thread.hpp> 00028 #include <boost/unordered_map.hpp> 00029 00030 namespace drizzled 00031 { 00032 class Session; 00033 } 00034 00035 namespace slave 00036 { 00037 00038 class ReplicationSlave : public drizzled::plugin::Daemon 00039 { 00040 public: 00041 00042 ReplicationSlave(const std::string &config) 00043 : drizzled::plugin::Daemon("Replication Slave"), 00044 _config_file(config) 00045 {} 00046 00047 ~ReplicationSlave() 00048 { 00049 _consumer_thread.interrupt(); 00050 00051 boost::unordered_map<uint32_t, Master *>::const_iterator it; 00052 00053 for (it= _masters.begin(); it != _masters.end(); ++it) 00054 { 00055 it->second->thread().interrupt(); 00056 } 00057 } 00058 00060 void startup(drizzled::Session &session); 00061 00062 private: 00063 00067 class Master 00068 { 00069 public: 00070 Master(uint32_t id) 00071 { 00072 _producer.setMasterId(id); 00073 } 00074 00075 QueueProducer &producer() 00076 { 00077 return _producer; 00078 } 00079 00080 boost::thread &thread() 00081 { 00082 return _producer_thread; 00083 } 00084 00085 void start() 00086 { 00087 _producer_thread= boost::thread(&QueueProducer::run, &_producer); 00088 } 00089 00090 private: 00092 QueueProducer _producer; 00093 00095 boost::thread _producer_thread; 00096 }; 00097 00099 std::string _config_file; 00100 00101 std::string _error; 00102 00104 QueueConsumer _consumer; 00105 00110 boost::thread _consumer_thread; 00111 00113 boost::unordered_map<uint32_t, Master *> _masters; 00114 00116 Master &master(size_t index) 00117 { 00118 return *(_masters[index]); 00119 } 00120 00130 bool initWithConfig(); 00131 }; 00132 00133 } /* namespace slave */ 00134