00001 // $Id: Server.ih 1781 2011-04-11 12:10:19Z tho $ 00002 // 00003 // Copyright (C) 2008 00004 // Fraunhofer (FOKUS) 00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY 00006 // Stefan Bund <g0dil@berlios.de> 00007 // 00008 // This program 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 2 of the License, or 00011 // (at your option) any later version. 00012 // 00013 // This program 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, write to the 00020 // Free Software Foundation, Inc., 00021 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00022 00026 #ifndef IH_SENF_Scheduler_Console_Server_ 00027 #define IH_SENF_Scheduler_Console_Server_ 1 00028 00029 // Custom includes 00030 #include <boost/iostreams/concepts.hpp> 00031 #include <boost/iostreams/stream.hpp> 00032 #include <senf/Scheduler/ReadHelper.hh> 00033 #include <senf/Socket/Protocols/INet/TCPSocketHandle.hh> 00034 #include <senf/Utils/Logger/SenfLog.hh> 00035 00036 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00037 00038 namespace senf { 00039 namespace console { 00040 00041 class Server; 00042 class Client; 00043 00044 namespace detail { 00045 00046 class ServerManager 00047 { 00048 public: 00049 typedef boost::intrusive_ptr<Server> ptr; 00050 00051 protected: 00052 00053 private: 00054 static void add(ptr server); 00055 static void remove(ptr server); 00056 00057 static ServerManager & instance(); 00058 00059 typedef std::set<ptr> Servers; 00060 Servers servers_; 00061 00062 friend class senf::console::Server; 00063 }; 00064 00071 class NonblockingSocketSink 00072 : public boost::iostreams::sink 00073 { 00074 public: 00075 NonblockingSocketSink(Client & client); 00076 std::streamsize write(const char * s, std::streamsize n); 00077 00078 Client & client() const; 00079 00080 private: 00081 Client & client_; 00082 }; 00083 00084 typedef boost::iostreams::stream<NonblockingSocketSink> NonblockingSocketOStream; 00085 00086 typedef senf::ServerSocketHandle< 00087 senf::MakeSocketPolicy< senf::TCPv4SocketProtocol::Policy, 00088 senf::BSDAddressingPolicy>::policy > ServerHandle; 00089 00095 class ClientReader 00096 { 00097 public: 00098 SENF_LOG_CLASS_AREA(); 00099 00100 typedef ServerHandle::ClientHandle ClientHandle; 00101 00102 virtual ~ClientReader() = 0; 00103 00104 // Called by subclasses to get information from the Client 00105 00106 Client & client() const; 00107 std::string promptString() const; 00108 ClientHandle handle() const; 00109 std::ostream & stream() const; 00110 00111 // Called by subclasses to perform actions in the Client 00112 00113 void stopClient(); 00114 std::string::size_type handleInput(std::string const & input, bool incremental=false) const; 00115 00116 // Called by the Client 00117 00118 void disablePrompt(); 00119 void enablePrompt(); 00120 void write(std::string const & data); 00121 unsigned width() const; 00122 00123 protected: 00124 ClientReader(Client & client); 00125 00126 private: 00127 virtual void v_disablePrompt() = 0; 00128 virtual void v_enablePrompt() = 0; 00129 virtual void v_write(std::string const & data) = 0; 00130 virtual unsigned v_width() const = 0; 00131 00132 Client & client_; 00133 }; 00134 00140 class DumbClientReader 00141 : public ClientReader 00142 { 00143 public: 00144 DumbClientReader(Client & client); 00145 00146 private: 00147 virtual void v_disablePrompt(); 00148 virtual void v_enablePrompt(); 00149 virtual void v_write(std::string const & data); 00150 virtual unsigned v_width() const; 00151 00152 void clientData(senf::ReadHelper<ClientHandle>::ptr helper); 00153 void showPrompt(); 00154 00155 std::string tail_; 00156 unsigned promptLen_; 00157 bool promptActive_; 00158 }; 00159 00165 class NoninteractiveClientReader 00166 : public ClientReader 00167 { 00168 public: 00169 NoninteractiveClientReader(Client & client); 00170 00171 private: 00172 virtual void v_disablePrompt(); 00173 virtual void v_enablePrompt(); 00174 virtual void v_write(std::string const & data); 00175 virtual unsigned v_width() const; 00176 00177 void newData(int event); 00178 00179 scheduler::FdEvent readevent_; 00180 std::string buffer_; 00181 }; 00182 00183 }}} 00184 00185 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00186 #endif 00187 00188 00189 // Local Variables: 00190 // mode: c++ 00191 // fill-column: 100 00192 // comment-column: 40 00193 // c-file-style: "senf" 00194 // indent-tabs-mode: nil 00195 // ispell-local-dictionary: "american" 00196 // compile-command: "scons -u test" 00197 // End: