root/trunk/whisperlib/net/rpc/lib/server/rpc_server.h

Revision 7, 4.5 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
1 // Copyright (c) 2009, Whispersoft s.r.l.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Whispersoft s.r.l. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Cosmin Tudorache
31
32 #ifndef __NET_RPC_LIB_SERVER_RPC_SERVER_H__
33 #define __NET_RPC_LIB_SERVER_RPC_SERVER_H__
34
35 #include <whisperlib/common/sync/event.h>
36 #include <whisperlib/net/base/connection.h>
37 #include <whisperlib/net/base/address.h>
38 #include <whisperlib/net/rpc/lib/server/execution/rpc_execution_pool.h>
39
40 // This is the TCP server, listening and creating rpc::ServerConnections for
41 // every accepted client.
42
43 namespace rpc {
44
45 class Server {
46  public:
47   //enum { DEFAULT_PORT = 5678 };
48   typedef Callback1<bool> OpenCompletedCallback;
49
50  public:
51   Server(net::Selector& selector,
52          net::NetFactory& net_factory,
53          net::PROTOCOL net_protocol,
54          IAsyncQueryExecutor& executor);
55   virtual ~Server();
56
57
58   //////////////////////////////////////////////////////////////////////
59   //
60   //        Methods available to any external thread.
61   //
62
63   //  Opens service on the given local address & port.
64   //  To close the server use Shutdown().
65   // open_completed_callback: if NULL: This function is synchronous
66   //                                   and blocks untill Open completes.
67   //                          non-NULL: This function is asynchronous,
68   //                                    returns immediately and
69   //                                    open_completed_callback will be called
70   //                                    when Open completes.
71   //
72   //  IMPORTANT: do NOT delete the rpc::Server from within
73   //             open_completed_callback !
74   bool Open(const net::HostPort& addr,
75             OpenCompletedCallback* open_completed_callback = NULL);
76
77   // Test if the server is running.
78   bool IsOpen() const {
79     return net_acceptor_->state() == net::NetAcceptor::LISTENING;
80   }
81
82   // Close the server.
83   // Does nothing if the server is already closed.
84   void Shutdown();
85
86  private:
87   //////////////////////////////////////////////////////////////////////
88   //
89   //     Methods available only from the selector thread.
90   //
91   //
92
93   // Opens the server in Listen mode on the given local addr & port.
94   void OpenInSelectThread(net::HostPort addr);
95
96   //////////////////////////////////////////////////////////////////////
97   //
98   //                net::NetAcceptor handlers
99   //
100   bool AcceptorFilterHandler(const net::HostPort& peer_address);
101   void AcceptorAcceptHandler(net::NetConnection* peer_connection);
102
103  private:
104   net::Selector& selector_;
105
106   net::NetFactory& net_factory_;      // the factory used to create
107                                       // the TCP or SSL acceptor
108
109   net::NetAcceptor* net_acceptor_;    // the acceptor (TCP or SSL)
110
111   IAsyncQueryExecutor& executor_;     // the queries executor
112   synch::Event open_completed_;       // signaled when the Open procedure
113                                       // is completed
114   synch::Event shutdown_completed_;   // ......... Shutdown .............
115
116   // external function, called when Open procedure completed
117   OpenCompletedCallback* open_completed_callback_;
118
119  private:
120   DISALLOW_EVIL_CONSTRUCTORS(Server);
121 };
122 }
123 #endif   // __NET_RPC_LIB_SERVER_RPC_SERVER_H__
Note: See TracBrowser for help on using the browser.