SocketManager.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef ADCHPP_SOCKETMANAGER_H
00020 #define ADCHPP_SOCKETMANAGER_H
00021 
00022 #include "common.h"
00023 
00024 #include "forward.h"
00025 #include "ServerInfo.h"
00026 #include "Singleton.h"
00027 #include "Thread.h"
00028 
00029 #include <boost/asio.hpp>
00030 
00031 namespace adchpp {
00032 
00033 class SocketManager : public Singleton<SocketManager>, public Thread {
00034 public:
00035     typedef std::function<void()> Callback;
00037     ADCHPP_DLL void addJob(const Callback& callback) throw();
00041     ADCHPP_DLL void addJob(const long msec, const Callback& callback);
00045     ADCHPP_DLL void addJob(const std::string& time, const Callback& callback);
00050     ADCHPP_DLL Callback addTimedJob(const long msec, const Callback& callback);
00055     ADCHPP_DLL Callback addTimedJob(const std::string& time, const Callback& callback);
00056 
00057     void startup() throw(ThreadException);
00058     void shutdown();
00059 
00060     void setServers(const ServerInfoList& servers_) { servers = servers_; }
00061 
00062     typedef std::function<void (const ManagedSocketPtr&)> IncomingHandler;
00063     void setIncomingHandler(const IncomingHandler& handler) { incomingHandler = handler; }
00064 
00065     std::map<std::string, int> errors;
00066 
00067 private:
00068     friend class ManagedSocket;
00069     friend class SocketFactory;
00070 
00071     virtual int run();
00072 
00073     void closeFactories();
00074 
00075     boost::asio::io_service io;
00076     std::unique_ptr<boost::asio::io_service::work> work;
00077 
00078     ServerInfoList servers;
00079     std::vector<SocketFactoryPtr> factories;
00080 
00081     IncomingHandler incomingHandler;
00082 
00083     static const std::string className;
00084 
00085     friend class Singleton<SocketManager>;
00086     ADCHPP_DLL static SocketManager* instance;
00087 
00088     typedef shared_ptr<boost::asio::deadline_timer> timer_ptr;
00089     void addJob(const boost::asio::deadline_timer::duration_type& duration, const Callback& callback);
00090     Callback addTimedJob(const boost::asio::deadline_timer::duration_type& duration, const Callback& callback);
00091     void setTimer(timer_ptr timer, const boost::asio::deadline_timer::duration_type& duration, Callback* callback);
00092     void handleWait(timer_ptr timer, const boost::asio::deadline_timer::duration_type& duration, const boost::system::error_code& error,
00093         Callback* callback);
00094     void cancelTimer(timer_ptr timer, Callback* callback);
00095 
00096     void onLoad(const SimpleXML& xml) throw();
00097 
00098     SocketManager();
00099     virtual ~SocketManager();
00100 };
00101 
00102 }
00103 
00104 #endif // SOCKETMANAGER_H
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3