Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Server.hh

Go to the documentation of this file.
00001 // $Id: Server.hh 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 HH_SENF_Scheduler_Console_Server_
00027 #define HH_SENF_Scheduler_Console_Server_ 1
00028 
00029 // Custom includes
00030 #include <set>
00031 #include <boost/utility.hpp>
00032 #include <boost/scoped_ptr.hpp>
00033 #include <senf/Scheduler/FdEvent.hh>
00034 #include <senf/Scheduler/TimerEvent.hh>
00035 #include <senf/Socket/Protocols/INet/INetAddressing.hh>
00036 #include <senf/Utils/Logger.hh>
00037 #include <senf/Utils/intrusive_refcount.hh>
00038 #include "Executor.hh"
00039 
00040 //#include "Server.mpp"
00041 #include "Server.ih"
00042 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00043 
00044 namespace senf {
00045 namespace console {
00046 
00047     class Client;
00048 
00063     class Server
00064         : public senf::intrusive_refcount
00065     {
00066         SENF_LOG_CLASS_AREA();
00067         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
00068     public:
00069         //-////////////////////////////////////////////////////////////////////////
00070         // Types
00071 
00072         typedef detail::ServerHandle ServerHandle;
00073 
00074         enum Mode { Automatic, Interactive, Noninteractive };
00075 
00076         //-////////////////////////////////////////////////////////////////////////
00077 
00078         static Server & start(senf::INet4SocketAddress const & address);
00080         static Server & start(senf::INet6SocketAddress const & address);
00082 
00083         std::string const & name() const; 
00084 
00086         Server & name(std::string const & name); 
00087 
00089         DirectoryNode & root() const;   
00090 
00091         Server & root(DirectoryNode & root); 
00092 
00095         Mode mode() const;              
00096 
00098         Server & mode(Mode mode);       
00099 
00113         void stop();                    
00114 
00117     protected:
00118 
00119     private:
00120         Server(ServerHandle handle);
00121 
00122         static Server & start(ServerHandle handle);
00123 
00124         void newClient(int event);
00125         void removeClient(Client & client);
00126 
00127         ServerHandle handle_;
00128         scheduler::FdEvent event_;
00129         DirectoryNode::ptr root_;
00130         Mode mode_;
00131 
00132         typedef std::set< boost::intrusive_ptr<Client> > Clients;
00133         Clients clients_;
00134         std::string name_;
00135 
00136         friend class Client;
00137     };
00138 
00147     class Client
00148         : public senf::intrusive_refcount,
00149           private boost::base_from_member< detail::NonblockingSocketOStream >,
00150           public senf::log::IOStreamTarget
00151     {
00152         typedef boost::base_from_member< detail::NonblockingSocketOStream > out_t;
00153 
00154         SENF_LOG_CLASS_AREA();
00155         SENF_LOG_DEFAULT_LEVEL( senf::log::NOTICE );
00156 
00157         static const unsigned INTERACTIVE_TIMEOUT = 500; // milliseconds;
00158 
00159     public:
00160         typedef Server::ServerHandle::ClientHandle ClientHandle;
00161 
00162         ~Client();
00163 
00164         void stop();                    
00165 
00167         std::string const & name() const; 
00168 
00170         ClientHandle handle() const;    
00171         std::ostream & stream();        
00172 
00176         std::string promptString() const; 
00177         DirectoryNode & root() const;   
00178         DirectoryNode & cwd() const;    
00179 
00181         Server::Mode mode() const;      
00182 
00183         void write(std::string const & data) const;
00185 
00188         std::string const & backtrace() const; 
00189         unsigned width() const;         
00190 
00194         static Client & get(std::ostream & os);
00196 
00203         static unsigned getWidth(std::ostream & os, unsigned defaultWidth = 0,
00204                                  unsigned minWidth = 0);
00206 
00213     protected:
00214 
00215     private:
00216         Client(Server & server, ClientHandle handle);
00217 
00218         void setInteractive();
00219         void setNoninteractive();
00220 
00221         size_t handleInput(std::string input, bool incremental = false);
00222         virtual void v_write(senf::log::time_type timestamp, std::string const & stream,
00223                              std::string const & area, unsigned level,
00224                              std::string const & message);
00225 
00226         Server & server_;
00227         ClientHandle handle_;
00228         scheduler::FdEvent readevent_;
00229         scheduler::TimerEvent timer_;
00230         CommandParser parser_;
00231         Executor executor_;
00232         std::string name_;
00233         boost::scoped_ptr<detail::ClientReader> reader_;
00234         Server::Mode mode_;
00235         std::string backtrace_;
00236 
00237         friend class Server;
00238         friend class detail::ClientReader;
00239         friend class detail::NonblockingSocketSink;
00240 
00241         class SysBacktrace
00242         {
00243             SysBacktrace();
00244             static void  backtrace(std::ostream & os);
00245             static SysBacktrace instance_;
00246         };
00247 
00248     };
00249 
00253     std::ostream & operator<<(std::ostream & os, Client const & client);
00254 
00258     std::ostream & operator<<(std::ostream & os, Client * client);
00259 
00260 }}
00261 
00262 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00263 #include "Server.cci"
00264 //#include "Server.ct"
00265 //#include "Server.cti"
00266 #endif
00267 
00268 
00269 // Local Variables:
00270 // mode: c++
00271 // fill-column: 100
00272 // comment-column: 40
00273 // c-file-style: "senf"
00274 // indent-tabs-mode: nil
00275 // ispell-local-dictionary: "american"
00276 // compile-command: "scons -u test"
00277 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research