Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[senf.git] / Examples / TCPClientServer / server.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Thorsten Horstmann <tho@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 // Definition of non-inline non-template functions
24
25 //#include "server.hh"
26 //#include "server.ih"
27
28 // Custom includes
29 #include <string>
30 #include <iostream>
31 #include <senf/Scheduler/Scheduler.hh>
32 #include <senf/Utils/membind.hh>
33 #include <senf/Socket/Protocols/INet.hh>
34
35 class Server
36 {
37     senf::TCPv4ServerSocketHandle serverSock;
38
39 public:
40     Server(senf::INet4Address const & host, unsigned int port)
41         : serverSock(senf::INet4SocketAddress(host, port)) {}
42
43     void run()
44     {
45         senf::Scheduler::instance().add(
46             serverSock,
47             senf::membind(&Server::accept, this),
48             senf::Scheduler::EV_READ);
49         senf::Scheduler::instance().process();
50     }
51
52 private:
53     void accept(int event)
54     {
55         senf::TCPv4ClientSocketHandle clientSock (serverSock.accept());
56         senf::Scheduler::instance().add(
57             clientSock,
58             boost::bind(&Server::readFromClient, this, clientSock, _1),
59             senf::Scheduler::EV_READ);
60     }
61
62     void readFromClient(senf::TCPv4ClientSocketHandle clientSock, int event)
63     {
64         if (!clientSock) {
65             senf::Scheduler::instance().remove(clientSock);
66             return;
67         }
68         std::string data (clientSock.read());
69         std::cout << "'" << data << "'" << std::endl;
70     }
71 };
72
73
74 int main(int argc, char const * argv[])
75 {
76     try {
77         Server myServer (senf::INet4Address::Loopback, 4243);
78         myServer.run();
79     }
80     catch (std::exception const & ex) {
81         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
82     }
83     return 0;
84 }
85
86 \f
87 // Local Variables:
88 // mode: c++
89 // fill-column: 100
90 // c-file-style: "senf"
91 // indent-tabs-mode: nil
92 // ispell-local-dictionary: "american"
93 // compile-command: "scons -u"
94 // comment-column: 40
95 // End: