• Main Page
  • Related Pages
  • Classes
  • Files
  • File List

src/Server.hh

00001 /*
00002  * Copyright Staffan Gimåker 2010.
00003  *
00004  * ---
00005  *
00006  * This file is part of peekabot.
00007  *
00008  * peekabot is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * peekabot is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 #ifndef PEEKABOT_SERVER_HH_INCLUDED
00023 #define PEEKABOT_SERVER_HH_INCLUDED
00024 
00025 
00026 #include <queue>
00027 #include <string>
00028 #include <boost/function.hpp>
00029 #include <boost/utility.hpp>
00030 #include <boost/asio.hpp>
00031 #include <boost/thread/mutex.hpp>
00032 #include <boost/thread/condition.hpp>
00033 #include <boost/thread/thread.hpp>
00034 
00035 
00036 namespace peekabot
00037 {
00038     class Config;
00039     class ClientConnection;
00040     class Action;
00041     class SceneTree;
00042 
00043     struct ServerData
00044     {
00045         SceneTree *m_scene;
00046     };
00047 
00054     class Server : public boost::noncopyable
00055     {
00056     public:
00057         Server(const Config &config);
00058 
00059         ~Server();
00060 
00061         const Config &get_config() const;
00062 
00063         void start();
00064 
00065         void stop();
00066 
00067         bool is_stopped() const;
00068 
00069         void post(boost::function<void (ServerData &)> handler);
00070 
00071         // For recording actions
00072         //void announce_action(boost::shared_ptr<Action>);
00073 
00074         void listen_ipv4(unsigned int port);
00075 
00076         //boost::range<...> get_clients();
00077 
00078     private:
00079         void start_accept(
00080             boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor);
00081 
00082         void handle_accept(
00083             const boost::system::error_code &e,
00084             boost::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor,
00085             boost::shared_ptr<ClientConnection> c);
00086 
00087         void server_thread();
00088 
00089     private:
00090         const Config &m_config;
00091 
00092         ServerData m_data;
00093 
00094         typedef std::queue<
00095             boost::function<void (ServerData &)> > RequestQueue;
00096         RequestQueue m_requests;
00097         boost::mutex m_requests_mutex;
00098         boost::condition m_requests_cond;
00099 
00100         boost::asio::io_service m_io_service;
00101         boost::asio::io_service::work *m_work;
00102 
00103         boost::thread *m_asio_thread;
00104         boost::thread *m_server_thread;
00105         volatile bool m_stop_signal;
00106     };
00107 }
00108 
00109 
00110 #endif // PEEKABOT_SERVER_HH_INCLUDED

Generated on Sun Jan 30 2011 for peekabot by  doxygen 1.7.1