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

Revision 7, 6.3 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: Catalin Popescu
31 //
32
33 #ifndef __NET_RPC_LIB_SERVER_RPC_HTTP_SERVER_H__
34 #define __NET_RPC_LIB_SERVER_RPC_HTTP_SERVER_H__
35
36 #include <map>
37 #include <string>
38 #include <whisperlib/common/base/types.h>
39
40 #include WHISPER_HASH_SET_HEADER
41
42 #include <whisperlib/common/base/log.h>
43 #include <whisperlib/net/rpc/lib/rpc_constants.h>
44 #include <whisperlib/net/rpc/lib/codec/binary/rpc_binary_codec.h>
45 #include <whisperlib/net/rpc/lib/codec/json/rpc_json_codec.h>
46 #include <whisperlib/net/rpc/lib/server/rpc_core_types.h>
47 #include <whisperlib/net/rpc/lib/server/rpc_service_invoker.h>
48 #include <whisperlib/net/http/http_server_protocol.h>
49 #include <whisperlib/net/base/user_authenticator.h>
50 #include <whisperlib/net/util/ipclassifier.h>
51
52 namespace rpc {
53
54 class HttpServer {
55  public:
56   // Starts serving RPC requests under the provided path
57   // Makes the processing calls to be issued in the given select server
58   // IMPORTANT: this rpc::HttpServer *must* live longer then the
59   //            underlining http::Server
60   // We don't own any of the provided objects..
61   HttpServer(net::Selector* selector,
62              http::Server* server,
63              net::UserAuthenticator* authenticator,
64              const string& path,
65              bool auto_js_forms,
66              bool is_public,
67              int max_concurrent_requests,
68              const string& ip_class_restriction);
69   ~HttpServer();
70
71   // Registers a service with this server (will be provided
72   // under /..path../
73   bool RegisterService(rpc::ServiceInvoker* invoker);
74   bool UnregisterService(rpc::ServiceInvoker* invoker);
75   // Registers a service with this name (will be provided
76   // under /..path../..sub_path../
77   // NOTE: sub_path should contain a final slash (if you wish !)
78   bool RegisterService(const string& sub_path, rpc::ServiceInvoker* invoker);
79   bool UnregisterService(const string& sub_path, rpc::ServiceInvoker* invoker);
80
81   // A special mapping - all rpc callbacks on sub_path_src will be mirrored
82   // also on sub_path_dest.
83   void RegisterServiceMirror(const string& sub_path_src,
84                              const string& sub_path_dest);
85
86   const string& path() const {
87     return path_;
88   }
89  protected:
90   // Actual request processing.
91   void ProcessRequest(http::ServerRequest* req);
92
93   // The actual processing - if req == NULL - it is a mirrored request..
94   void ProcessRequestCore(http::ServerRequest* req,
95                           const string& sub_path,
96                           const rpc::Transport& transport,
97                           const rpc::Message& p,
98                           rpc::Codec* codec,
99                           int timeout);
100
101   // Request processing continuation (after authentication)
102   void ProcessAuthenticatedRequest(
103     http::ServerRequest* req,
104     string* service_name, string* method_name,
105     net::UserAuthenticator::Answer auth_answer);
106
107   // Callback upon query completion
108   // The query is about to be deleted after this call,
109   // so don't use it for a new scheduled callback.
110   void RpcCallback(http::ServerRequest* req,
111                    const rpc::Query& query);
112
113   // Callback completion for secondary requests
114   void RpcSecondaryCallback(io::MemoryStream* params,
115                             const rpc::Query& query);
116
117   // Callback to send http reply from selector context.
118   void RpcReply(http::ServerRequest* req, io::MemoryStream* reply);
119
120   // We run your processing callbacks in the select loop of this selector
121   net::Selector* const selector_;
122
123   // If this is set empty we require for our users to be authenticated
124   net::UserAuthenticator* const authenticator_;
125
126   // If not null accept clients only from guys identified under this class
127   net::IpClassifier* const accepted_clients_;
128
129   // On which path we serve ?
130   const string path_;
131
132   // We do not accept more then these concurrent requests
133   int max_concurrent_requests_;
134
135   // Current statistics
136   int current_requests_;
137
138   // Used for encoding / decoding of stuff
139   rpc::BinaryCodec binary_codec_;
140   rpc::JsonCodec json_codec_;
141
142   // What services we provide ..
143   typedef map<string, rpc::ServiceInvoker*> ServicesMap;
144   ServicesMap services_;
145
146   // Services Mirror - forward map -
147   // i.e. when we receive a request on given first path we invoke the
148   // same function on all seconds
149   typedef map<string, hash_set<string>*> MirrorMap;
150   MirrorMap mirror_map_;
151   // The revers for above
152   typedef map<string, string> ReverseMirrorMap;
153   ReverseMirrorMap reverse_mirror_map_;
154
155   // Used to keep some javascript that needs included in http forms
156   string js_prolog_;
157
158   // TODO(cpopescu): stats !
159   //
160   // - per each service / method -
161   //    - num received (min etc)
162   //    - processing time
163   //
164   // - stats per error case
165   // - recent request browsing
166   //
167  private:
168   DISALLOW_EVIL_CONSTRUCTORS(HttpServer);
169 };
170 }
171 #endif  // __NET_RPC_LIBS_SERVER_RPC_HTTP_SERVER_H__
Note: See TracBrowser for help on using the browser.