NCBI C++ ToolKit
rpcbase_impl.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef SERIAL___RPCBASE_IMPL__HPP
2 #define SERIAL___RPCBASE_IMPL__HPP
3 
4 /* $Id: rpcbase_impl.hpp 98548 2022-12-02 15:55:12Z ucko $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Aaron Ucko, NCBI
30  *
31  * File Description:
32  * Generic template class for ASN.1/XML RPC clients
33  *
34  */
35 
36 #include <corelib/ncbimtx.hpp>
37 #include <corelib/ncbi_system.hpp>
38 #include <serial/objistr.hpp>
39 #include <serial/objostr.hpp>
40 #include <serial/serial.hpp>
41 #include <util/retry_ctx.hpp>
42 
43 
44 /** @addtogroup GenClassSupport
45  *
46  * @{
47  */
48 
49 
51 
52 /// Base class for CRPCClient template - defines methods
53 /// independent of request and response types.
55 {
56 public:
57  CRPCClient_Base(const string& service,
59  CRPCClient_Base(const string& service,
61  unsigned int try_limit);
62  virtual ~CRPCClient_Base(void);
63 
64  void Connect(void);
65  void Disconnect(void);
66  void Reset(void);
67 
68  /// Set additional connection arguments.
69  void SetArgs(const string& args) { m_Args = args; }
70  void SetArgs(const CUrlArgs& args) { m_Args = args.GetQueryString(CUrlArgs::eAmp_Char); }
71  /// Get additional connection arguments.
72  const string& GetArgs(void) const { return m_Args; }
73 
74  const string& GetService(void) const { return m_Service; }
75  void SetService(const string& service) { m_Service = service; }
76 
77  ESerialDataFormat GetFormat(void) const { return m_Format; }
78  void SetFormat(ESerialDataFormat fmt) { m_Format = fmt; }
79 
80  /// Get number of request attempts. If not set explicitly through SetTryLimit
81  /// or constructor argument, the following values are used:
82  /// - <upcase_service_name>__RPC_CLIENT__MAX_TRY environment varialbe
83  /// - [service_name.rpc_client] section, max_try value in the INI file
84  /// - 3 (global default)
85  unsigned int GetTryLimit(void) const { return m_TryLimit; }
86  void SetTryLimit(unsigned int n) { m_TryLimit = n > 0 ? n : 3; }
87  /// @deprecated Use GetTryLimit()
89  unsigned int GetRetryLimit(void) const { return GetTryLimit(); }
90  /// @deprecated Use SetTryLimit()
92  void SetRetryLimit(unsigned int n) { SetTryLimit(n); }
93 
94  /// Get retry delay. If not set explicitly through SetRetryDelay, the following values are used:
95  /// - <upcase_service_name>__RPC_CLIENT__RETRY_DELAY environment varialbe
96  /// - [service_name.rpc_client] section, retry_delay value in the INI file
97  /// - 0 (global default)
98  const CTimeSpan GetRetryDelay(void) const { return m_RetryDelay; }
99  void SetRetryDelay(const CTimeSpan& ts) { m_RetryDelay = ts; }
100 
101  /// Set request canceler. The canceler is passed to CConn_IOStream and
102  /// must be be derived from CObject as its first superclass.
103  /// @sa CConn_IOStream::SetCanceledCallback
104  void SetCanceledCallback(const ICanceled* canceled) { m_Canceler = canceled; }
105  bool IsCanceled(void) const { return m_Canceler.NotNull() && m_Canceler->IsCanceled(); }
106 
107 protected:
108  void SetAffinity(const string& affinity);
109 
110  /// These run with m_Mutex already acquired.
111  virtual void x_Connect(void) = 0;
112 
113  /// Disconnect as cleanly as possible.
114  ///
115  /// @note Derived classes that override this function should copy
116  /// the destructor logic that calls it (via Disconnect, which
117  /// avoids duplicate calls).
118  virtual void x_Disconnect(void);
119  void x_SetStream(CNcbiIostream* stream);
120 
121  void x_Ask(const CSerialObject& request, CSerialObject& reply);
122  // Casting stubs.
123  virtual void x_WriteRequest(CObjectOStream& out, const CSerialObject& request) = 0;
124  virtual void x_ReadReply(CObjectIStream& in, CSerialObject& reply) = 0;
125  virtual string x_GetAffinity(const CSerialObject& request) const = 0;
126 
127  static const char* GetContentTypeHeader(ESerialDataFormat format);
128 
129 private:
130  /// Prohibit default copy constructor and assignment operator.
132  bool operator= (const CRPCClient_Base&);
133 
135  CMutex m_Mutex; ///< To allow sharing across threads.
137  unsigned int m_TryCount;
139 
140 protected:
141  string m_Service; ///< Used by default Connect().
142  string m_Args;
143  unique_ptr<CNcbiIostream> m_Stream; // This must be destroyed after m_In/m_Out.
144  unique_ptr<CObjectIStream> m_In;
145  unique_ptr<CObjectOStream> m_Out;
146  string m_Affinity;
147  unsigned int m_TryLimit;
150 
151  // Retry policy; by default, just _TRACEs the event and returns
152  // true. May reset the connection (or do anything else, really),
153  // but note that Ask() will always automatically reconnect if the
154  // stream is explicitly bad. (Ask() also takes care of enforcing
155  // m_TryLimit.)
156  virtual bool x_ShouldRetry(unsigned int tries);
157 
158  // Calculate effective retry delay. Returns value from CRetryContext
159  // if any, or the value set by SetRetryDelay. The returned value never
160  // exceeds max_delay.
161  CTimeSpan x_GetRetryDelay(double max_delay) const;
162 };
163 
164 
166 {
167 public:
168  enum EErrCode {
169  eRetry, ///< Request failed, should be retried if possible.
170  eFailed, ///< Request (or retry) failed.
171  eArgs, ///< Failed to send request arguments.
172  eOther
173  };
174 
175  virtual const char* GetErrCodeString(void) const override;
176 
177  bool IsSetRetryContext(void) const { return m_RetryCtx; }
178  /// Read retry related data.
179  CRetryContext& GetRetryContext(void) { return *m_RetryCtx; }
180  /// Set new retry context.
181  void SetRetryContext(CRetryContext& ctx) { m_RetryCtx.Reset(&ctx); }
182 
184 
185 protected:
186  virtual void x_Assign(const CException& src) override
187  {
189  m_RetryCtx = dynamic_cast<const CRPCClientException&>(src).m_RetryCtx;
190  }
191 
192 private:
194 };
195 
196 
198 
199 
200 /* @} */
201 
202 #endif /* SERIAL___RPCBASE_IMPL__HPP */
HTTP-specific retry context implementation.
Definition: retry_ctx.hpp:191
CMutex –.
Definition: ncbimtx.hpp:749
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStream –.
Definition: objostr.hpp:83
Base class for CRPCClient template - defines methods independent of request and response types.
CRetryContext – Retry context.
Definition: retry_ctx.hpp:58
Base class for all serializable objects.
Definition: serialbase.hpp:150
CTimeSpan.
Definition: ncbitime.hpp:1313
CUrlArgs::
Definition: ncbi_url.hpp:240
Interface for testing cancellation request in a long lasting operation.
Definition: icanceled.hpp:51
std::ofstream out("events_result.xml")
main entry point for tests
CS_CONTEXT * ctx
Definition: t0006.c:12
virtual void x_Assign(const CException &src)
Helper method for copying exception data.
Definition: ncbiexpt.cpp:536
EErrCode
Error types that an application can generate.
Definition: ncbiexpt.hpp:884
const string & GetService(void) const
void SetTryLimit(unsigned int n)
bool IsCanceled(void) const
CRef< CRetryContext > m_RetryCtx
unsigned int GetTryLimit(void) const
Get number of request attempts.
unsigned int GetRetryLimit(void) const
CTimeSpan m_RetryDelay
CConstIRef< ICanceled > m_Canceler
bool IsSetRetryContext(void) const
virtual void x_ReadReply(CObjectIStream &in, CSerialObject &reply)=0
unsigned int m_TryLimit
void SetArgs(const CUrlArgs &args)
const string & GetArgs(void) const
Get additional connection arguments.
unique_ptr< CObjectIStream > m_In
void SetCanceledCallback(const ICanceled *canceled)
Set request canceler.
string m_Service
Used by default Connect().
void SetService(const string &service)
ESerialDataFormat m_Format
void SetRetryDelay(const CTimeSpan &ts)
void SetRetryLimit(unsigned int n)
CMutex m_Mutex
To allow sharing across threads.
void SetFormat(ESerialDataFormat fmt)
virtual void x_WriteRequest(CObjectOStream &out, const CSerialObject &request)=0
CRetryContext & GetRetryContext(void)
Read retry related data.
CRPCClient_Base(const CRPCClient_Base &)
Prohibit default copy constructor and assignment operator.
void SetArgs(const string &args)
Set additional connection arguments.
virtual void x_Assign(const CException &src) override
Helper method for copying exception data.
unique_ptr< CObjectOStream > m_Out
virtual string x_GetAffinity(const CSerialObject &request) const =0
NCBI_EXCEPTION_DEFAULT(CRPCClientException, CException)
virtual void x_Connect(void)=0
These run with m_Mutex already acquired.
unique_ptr< CNcbiIostream > m_Stream
CHttpRetryContext m_RetryCtx
ESerialDataFormat
Data file format.
Definition: serialdef.hpp:71
void SetRetryContext(CRetryContext &ctx)
Set new retry context.
ESerialDataFormat GetFormat(void) const
const CTimeSpan GetRetryDelay(void) const
Get retry delay.
unsigned int m_TryCount
@ eArgs
Failed to send request arguments.
@ eRetry
Request failed, should be retried if possible.
@ eFailed
Request (or retry) failed.
#define NCBI_DEPRECATED
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::iostream CNcbiIostream
Portable alias for iostream.
Definition: ncbistre.hpp:152
string GetQueryString(EAmpEncoding amp_enc, NStr::EUrlEncode encode) const
Construct and return complete query string.
Definition: ncbi_url.cpp:225
@ eAmp_Char
Use & to separate arguments.
Definition: ncbi_url.hpp:254
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
yy_size_t n
static PyObject * Connect(PyObject *self, PyObject *args)
Multi-threading – mutexes; rw-locks; semaphore.
static Format format
Definition: njn_ioutil.cpp:53
std::istream & in(std::istream &in_, double &x_)
Retry context class.
Modified on Sun Jul 14 05:00:21 2024 by modify_doxy.py rev. 669887