e235c62f6ebf0c9a6637ec397ba1cba8fb436d72
[senf.git] / senf / Utils / Console / Server.cci
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Server inline non-template implementation */
30
31 //#include "Server.ih"
32
33 // Custom includes
34
35 #define prefix_ inline
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::console::detail::ServerManager
40
41 prefix_ void senf::console::detail::ServerManager::add(ptr server)
42 {
43     instance().servers_.insert(server);
44 }
45
46 prefix_ void senf::console::detail::ServerManager::remove(ptr server)
47 {
48     instance().servers_.erase(instance().servers_.find(server));
49 }
50
51 prefix_ senf::console::detail::ServerManager & senf::console::detail::ServerManager::instance()
52 {
53     static ServerManager manager;
54     return manager;
55 }
56
57 //-/////////////////////////////////////////////////////////////////////////////////////////////////
58 // senf::console::detail::NonblockingSocketSink
59
60 prefix_ senf::console::detail::NonblockingSocketSink::NonblockingSocketSink(Client & client)
61     : client_ (client)
62 {}
63
64 prefix_ senf::console::Client & senf::console::detail::NonblockingSocketSink::client()
65     const
66 {
67     return client_;
68 }
69
70 //-/////////////////////////////////////////////////////////////////////////////////////////////////
71 // senf::console::Server
72
73 prefix_ senf::console::Server & senf::console::Server::name(std::string const & name)
74 {
75     name_ = name;
76     return *this;
77 }
78
79 prefix_ std::string const & senf::console::Server::name()
80     const
81 {
82     return name_;
83 }
84
85 prefix_ senf::console::DirectoryNode & senf::console::Server::root()
86     const
87 {
88     return *root_;
89 }
90
91 prefix_ senf::console::Server & senf::console::Server::root(DirectoryNode & root)
92 {
93     root_ = root.thisptr();
94     return *this;
95 }
96
97 prefix_ senf::console::Server & senf::console::Server::mode(Mode m)
98 {
99     mode_ = m;
100     return *this;
101 }
102
103 prefix_ senf::console::Server::Mode senf::console::Server::mode()
104     const
105 {
106     return mode_;
107 }
108
109 prefix_ void senf::console::Server::stop()
110 {
111     // commit suicide
112     detail::ServerManager::remove(boost::intrusive_ptr<Server>(this));
113 }
114
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 // senf::console::Client
117
118 prefix_ senf::console::Client::~Client()
119 {
120     stream() << std::flush;
121 }
122
123 prefix_ void senf::console::Client::stop()
124 {
125     // THIS COMMITS SUICIDE. THE INSTANCE IS GONE AFTER removeClient RETURNS
126     server_.removeClient(*this);
127 }
128
129 prefix_ std::string const & senf::console::Client::name()
130     const
131 {
132     return name_;
133 }
134
135 prefix_ std::string senf::console::Client::promptString()
136     const
137 {
138     return name_ + ":" + executor_.cwdPath() + "$";
139 }
140
141 prefix_ senf::console::DirectoryNode & senf::console::Client::root()
142     const
143 {
144     return server_.root();
145 }
146
147 prefix_ senf::console::DirectoryNode & senf::console::Client::cwd()
148     const
149 {
150     return executor_.cwd();
151 }
152
153 prefix_ senf::console::Server::Mode senf::console::Client::mode()
154     const
155 {
156     return mode_;
157 }
158
159 prefix_ void senf::console::Client::write(std::string const & data)
160     const
161 {
162     reader_->write(data);
163 }
164
165 prefix_ std::string const & senf::console::Client::backtrace()
166     const
167 {
168     return backtrace_;
169 }
170
171 prefix_ unsigned senf::console::Client::width()
172     const
173 {
174     return reader_->width();
175 }
176
177 prefix_ senf::console::Client & senf::console::Client::get(std::ostream & os)
178 {
179     return dynamic_cast<detail::NonblockingSocketOStream&>(os)->client();
180 }
181
182 prefix_ senf::console::Client::ClientHandle senf::console::Client::handle()
183     const
184 {
185     return handle_;
186 }
187
188 prefix_ std::ostream & senf::console::Client::stream()
189 {
190     return out_t::member;
191 }
192
193 //-/////////////////////////////////////////////////////////////////////////////////////////////////
194 // senf::console::detail::ClientReader
195
196 prefix_ senf::console::detail::ClientReader::~ClientReader()
197 {}
198
199 prefix_ senf::console::Client & senf::console::detail::ClientReader::client()
200     const
201 {
202     return client_;
203 }
204
205 prefix_ std::string senf::console::detail::ClientReader::promptString()
206     const
207 {
208     return client().promptString();
209 }
210
211 prefix_ senf::console::detail::ClientReader::ClientHandle senf::console::detail::ClientReader::handle()
212     const
213 {
214     return client().handle();
215 }
216
217 prefix_ std::ostream & senf::console::detail::ClientReader::stream()
218     const
219 {
220     return client().stream();
221 }
222
223 prefix_ void senf::console::detail::ClientReader::stopClient()
224 {
225     client().stop();
226 }
227
228 prefix_ std::string::size_type
229 senf::console::detail::ClientReader::handleInput(std::string const & input, bool incremental)
230     const
231 {
232     return client().handleInput(input, incremental);
233 }
234
235 prefix_ void senf::console::detail::ClientReader::disablePrompt()
236 {
237     v_disablePrompt();
238 }
239
240 prefix_ void senf::console::detail::ClientReader::enablePrompt()
241 {
242     v_enablePrompt();
243 }
244
245 prefix_ void senf::console::detail::ClientReader::write(std::string const & data)
246 {
247     v_write(data);
248 }
249
250 prefix_ unsigned senf::console::detail::ClientReader::width()
251     const
252 {
253     return v_width();
254 }
255
256 prefix_ senf::console::detail::ClientReader::ClientReader(Client & client)
257     : client_ (client)
258 {}
259
260 //-/////////////////////////////////////////////////////////////////////////////////////////////////
261 #undef prefix_
262
263 \f
264 // Local Variables:
265 // mode: c++
266 // fill-column: 100
267 // comment-column: 40
268 // c-file-style: "senf"
269 // indent-tabs-mode: nil
270 // ispell-local-dictionary: "american"
271 // compile-command: "scons -u test"
272 // End: