switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / Examples / TCPClientServer / server.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
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 //   Thorsten Horstmann <tho@berlios.de>
27
28
29 // Definition of non-inline non-template functions
30
31 //#include "server.hh"
32 //#include "server.ih"
33
34 // Custom includes
35 #include <string>
36 #include <iostream>
37 #include <senf/Scheduler/Scheduler.hh>
38 #include <senf/Utils/membind.hh>
39 #include <senf/Socket/Protocols/INet.hh>
40
41 class Server
42 {
43     senf::TCPv4ServerSocketHandle serverSock;
44     senf::scheduler::FdEvent acceptevent;
45     senf::scheduler::FdEvent readevent;
46
47 public:
48     Server(senf::INet4Address const & host, unsigned int port)
49         : serverSock(senf::INet4SocketAddress(host, port)),
50           acceptevent("Server accept", senf::membind(&Server::accept, this),
51                       serverSock, senf::scheduler::FdEvent::EV_READ),
52           readevent("Server read", 0)
53         {}
54
55     void run()
56     {
57         senf::scheduler::process();
58     }
59
60 private:
61     void accept(int event)
62     {
63         senf::TCPv4ClientSocketHandle clientSock (serverSock.accept());
64         readevent
65             .action(boost::bind(&Server::readFromClient, this, clientSock, _1))
66             .handle(clientSock)
67             .events(senf::scheduler::FdEvent::EV_READ)
68             .enable();
69     }
70
71     void readFromClient(senf::TCPv4ClientSocketHandle clientSock, int event)
72     {
73         if (!clientSock) {
74             readevent.disable();
75             return;
76         }
77         std::string data (clientSock.read());
78         std::cout << "'" << data << "'" << std::endl;
79     }
80 };
81
82
83 int main(int argc, char const * argv[])
84 {
85     try {
86         Server myServer (senf::INet4Address::Loopback, 4243);
87         myServer.run();
88     }
89     catch (std::exception const & ex) {
90         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
91     }
92     return 0;
93 }
94
95 \f
96 // Local Variables:
97 // mode: c++
98 // fill-column: 100
99 // c-file-style: "senf"
100 // indent-tabs-mode: nil
101 // ispell-local-dictionary: "american"
102 // compile-command: "scons -u"
103 // comment-column: 40
104 // End: