root/trunk/whisperlib/net/rpc/lib/client/rpc_failsafe_client_connection_http.h

Revision 7, 4.7 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 #ifndef __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__
33 #define __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__
34
35 #include <map>
36 #include <string>
37 #include <whisperlib/common/base/types.h>
38 #include <whisperlib/common/base/log.h>
39 #include <whisperlib/common/base/errno.h>
40 #include <whisperlib/common/base/system.h>
41 #include <whisperlib/common/base/gflags.h>
42 #include <whisperlib/common/io/buffer/io_memory_stream.h>
43 #include <whisperlib/common/sync/event.h>
44 #include <whisperlib/common/sync/mutex.h>
45
46 #include <whisperlib/net/http/http_server_protocol.h>
47 #include <whisperlib/net/http/http_client_protocol.h>
48 #include <whisperlib/net/base/selector.h>
49 #include <whisperlib/net/http/failsafe_http_client.h>
50
51 #include <whisperlib/net/rpc/lib/client/irpc_client_connection.h>
52
53 namespace rpc {
54
55 // This is a failsafe transport over HTTP connection from client side.
56 // The big difference between this and the other HTTP rpc transport
57 // is that you can use a set of rpc servers to call (i.e. uses the
58 // first that is available, and does load balancing) and it manages
59 // differently the reconnection and retries (is not on every send, but
60 // employs a timeout etc.)
61 // And you can cancel the lookup (your callback will never be called again
62 // on completion)
63 //
64 class FailsafeClientConnectionHTTP : public rpc::IClientConnection {
65  public:
66   //////////////////////////////////////////////////////////////
67   //
68   //  Methods available to any external thread (application).
69   //
70   FailsafeClientConnectionHTTP(net::Selector* selector,
71                                rpc::CODEC_ID codec_id,
72                                http::FailSafeClient* failsafe_client,
73                                const string& httpRequestPath,
74                                const string& auth_user,
75                                const string& auth_pass);
76   virtual ~FailsafeClientConnectionHTTP();
77
78   // Returns a description of the last error on the http connection.
79   const char* Error() const;
80
81   // Close HTTP connection, if any.
82   void Close();
83
84  protected:
85   //////////////////////////////////////////////////////////////////////
86   //
87   //         IRPCClientConnection interface methods
88   //
89   virtual void Send(const rpc::Message* p);
90   virtual void Cancel(uint32 xid);
91
92  protected:
93   struct QueryStruct {
94     http::ClientRequest* const req_;
95     const rpc::Message* const msg_;
96     bool cancelled_;
97     const uint32 xid_;
98     QueryStruct(http::ClientRequest* req, const rpc::Message* msg)
99         : req_(req),
100           msg_(msg),
101           cancelled_(false),
102           xid_(msg->header_.xid_.Get()) {
103     }
104     ~QueryStruct() {
105       delete req_;
106     }
107   };
108  protected:
109   // Called by the selector after a http request receives full response
110   // from the server. The request object is the same one you used
111   // when launching the query (you probably want to delete it).
112   void CallbackRequestDone(QueryStruct* qs);
113
114   http::FailSafeClient* failsafe_client_;
115   const string http_request_path_;
116   const string auth_user_;
117   const string auth_pass_;
118
119   typedef map<uint32, QueryStruct*> QueryMap;
120   QueryMap queries_;
121
122  private:
123   DISALLOW_EVIL_CONSTRUCTORS(FailsafeClientConnectionHTTP);
124 };
125 }
126 #endif  // __NET_RPC_LIB_CLIENT_RPC_CLIENT_CONNECTION_HTTP_H__
Note: See TracBrowser for help on using the browser.