Scheduler: Fix ReadHelper to conform to new Scheduler API
[senf.git] / Console / Server.cc
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Server non-inline non-template implementation */
25
26 #include "Server.hh"
27 #include "Server.ih"
28
29 // Custom includes
30 #include <boost/algorithm/string/trim.hpp>
31 #include "../Utils/senfassert.hh"
32 #include "../Utils/membind.hh"
33 #include "../Utils/Logger/SenfLog.hh"
34
35 //#include "Server.mpp"
36 #define prefix_
37 ///////////////////////////////cc.p////////////////////////////////////////
38
39 prefix_ void senf::console::start(senf::INet4SocketAddress const & address)
40 {
41     senf::console::detail::Server::start(senf::TCPv4ServerSocketHandle(address));
42     SENF_LOG((detail::Server::SENFLogArea)(log::NOTICE)( 
43                  "Console server started at " << address ));
44 }
45
46 prefix_ void senf::console::start(senf::INet6SocketAddress const & address)
47 {
48     senf::console::detail::Server::start(senf::TCPv6ServerSocketHandle(address));
49     SENF_LOG((detail::Server::SENFLogArea)(log::NOTICE)( 
50                  "Console server started at " << address ));
51 }
52
53 ///////////////////////////////////////////////////////////////////////////
54 // senf::console::detail::Server
55
56 boost::scoped_ptr<senf::console::detail::Server> senf::console::detail::Server::instance_;
57
58 prefix_ void senf::console::detail::Server::start(ServerHandle handle)
59 {
60     SENF_ASSERT( ! instance_ );
61     instance_.reset(new Server(handle));
62 }
63
64 prefix_ senf::console::detail::Server::Server(ServerHandle handle)
65     : handle_ (handle)
66 {
67     Scheduler::instance().add( handle_, senf::membind(&Server::newClient, this) );
68 }
69
70 prefix_ senf::console::detail::Server::~Server()
71 {
72     Scheduler::instance().remove(handle_);
73 }
74
75 prefix_ void senf::console::detail::Server::newClient(Scheduler::EventId event)
76 {
77     ServerHandle::ClientSocketHandle client (handle_.accept());
78     boost::intrusive_ptr<Client> p (new Client(client));
79     clients_.insert( p );
80     SENF_LOG(( "Registered new client " << p.get() ));
81 }
82
83 prefix_ void senf::console::detail::Server::removeClient(Client & client)
84 {
85     SENF_LOG(( "Disposing client " << & client ));
86     // THIS DELETES THE CLIENT INSTANCE !!
87     clients_.erase(boost::intrusive_ptr<Client>(&client));
88 }
89
90 ///////////////////////////////////////////////////////////////////////////
91 // senf::console::detail::Client
92
93 prefix_ senf::console::detail::Client::Client(ClientHandle handle)
94     : handle_ (handle)
95 {
96     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
97                                         senf::membind(&Client::clientData, this) );
98 }
99
100 prefix_ senf::console::detail::Client::~Client()
101 {}
102
103 prefix_ void senf::console::detail::Client::stopClient()
104 {
105     // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
106     Server::instance_->removeClient(*this);
107 }
108
109 prefix_ void senf::console::detail::Client::clientData(ReadHelper<ClientHandle>::ptr helper)
110 {
111     if (helper->error() || handle_.eof()) {
112         // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER stopClient RETURNS
113         stopClient();
114         return;
115     }
116
117     std::string data (tail_ + helper->data());
118     tail_ = helper->tail();
119     boost::trim(data); // Gets rid of superfluous  \r or \n characters
120
121     SENF_LOG(( this << ": " << data ));
122     ReadHelper<ClientHandle>::dispatch( handle_, 16384u, ReadUntil("\n"),
123                                         senf::membind(&Client::clientData, this) );
124 }
125
126 ///////////////////////////////cc.e////////////////////////////////////////
127 #undef prefix_
128 //#include "Server.mpp"
129
130 \f
131 // Local Variables:
132 // mode: c++
133 // fill-column: 100
134 // comment-column: 40
135 // c-file-style: "senf"
136 // indent-tabs-mode: nil
137 // ispell-local-dictionary: "american"
138 // compile-command: "scons -u test"
139 // End: