Utils/Termlib: Implement terminfo format string interpretation
[senf.git] / Utils / Termlib / telnetServer.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 telnetServer non-inline non-template implementation */
25
26 //#include "telnetServer.hh"
27 //#include "telnetServer.ih"
28
29 // Custom includes
30 #include <boost/bind.hpp>
31 #include "../../Scheduler/Scheduler.hh"
32 #include "../Logger.hh"
33 #include "../../Socket/Protocols/INet.hh"
34 #include "TelnetTerminal.hh"
35 #include "Editor.hh"
36
37 //#include "telnetServer.mpp"
38 #define prefix_
39 ///////////////////////////////cc.p////////////////////////////////////////
40
41 namespace {
42
43     class MyEditor
44         : public senf::term::BaseEditor
45     {
46     public:
47         MyEditor(senf::term::AbstractTerminal & terminal) 
48             : BaseEditor(terminal) {}
49
50     private:
51         virtual void v_keyReceived(keycode_t key)
52             {
53                 SENF_LOG(("Key " << senf::term::KeyParser::describe(key)));
54                 if (key >= ' ' && key < 256)
55                     insertChar(key);
56                 else
57                     switch (key) {
58                     case '\r':
59                         newline();
60                         break;
61                     case senf::term::KeyParser::Left: 
62                     {
63                         unsigned c (currentColumn());
64                         if (c > 0)
65                             toColumn(c-1);
66                         break;
67                     }
68                     case senf::term::KeyParser::Backspace: 
69                     {
70                         unsigned c (currentColumn());
71                         if (c > 0) {
72                             toColumn(c-1);
73                             deleteChar();
74                         }
75                         break;
76                     }
77                     case senf::term::KeyParser::Delete:
78                         deleteChar();
79                         break;
80                     }
81             }
82
83     };
84
85     class MyTelnet 
86         : public senf::term::TelnetTerminal
87     {
88     public:
89         explicit MyTelnet(Handle handle) 
90             : senf::term::BaseTelnetProtocol(handle),
91               editor_(*this) {}
92         
93         virtual void v_eof()
94             {
95                 SENF_LOG(("EOF"));
96                 delete this;
97             }
98
99     private:
100         MyEditor editor_;
101     };
102
103     typedef senf::TCPv4ServerSocketHandle ServerHandle;
104     typedef ServerHandle::ClientHandle ClientHandle;
105
106     void connect(ServerHandle handle, int events)
107     {
108         if (events != senf::scheduler::FdEvent::EV_READ) {
109             senf::scheduler::terminate();
110             return;
111         }
112         ClientHandle client (handle.accept());
113         SENF_LOG(("new client ..."));
114         new MyTelnet (client);
115     }
116
117 }
118
119 int main(int argc, char const ** argv)
120 {
121     senf::log::ConsoleTarget::instance().timeFormat("");
122
123     ServerHandle server (ServerHandle::Address("127.0.0.1:22344"));
124     senf::scheduler::FdEvent serverEvent ("telnetServer", boost::bind(&connect, server, _1),
125                                           server, senf::scheduler::FdEvent::EV_READ);
126     SENF_LOG(("Server started at " << server.local()));
127
128     senf::scheduler::process();
129
130     return 0;
131 }
132
133 ///////////////////////////////cc.e////////////////////////////////////////
134 #undef prefix_
135 //#include "telnetServer.mpp"
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // comment-column: 40
142 // c-file-style: "senf"
143 // indent-tabs-mode: nil
144 // ispell-local-dictionary: "american"
145 // compile-command: "scons -u telnetServer"
146 // End: