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

Go to the SVN repository for this file.

1 #ifndef RETRY_CTX__HPP
2 #define RETRY_CTX__HPP
3 
4 /* $Id: retry_ctx.hpp 70186 2015-12-10 12:44:00Z ivanov $
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: Aleksey Grichenko
30  *
31  */
32 
33 /// @file retry_ctx.hpp
34 /// Retry context class.
35 
36 #include <map>
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbitime.hpp>
39 #include <corelib/ncbi_url.hpp>
40 
41 
42 /** @addtogroup Retry Context
43  *
44  * @{
45  */
46 
47 
49 
50 
51 /////////////////////////////////////////////////////////////////////////////
52 ///
53 /// CRetryContext -- Retry context.
54 ///
55 /// The class used to store, send and receive retry related data.
56 ///
58 {
59 public:
60  CRetryContext(void);
61  virtual ~CRetryContext(void) {}
62 
63  /// Content override flags
65  eNot_set, ///< No content provided, send the original request data
66  eNoContent, ///< Do not send any content on retry
67  eFromResponse, ///< On retry send content from the response body
68  eData ///< On retry send content from the response header
69  };
70 
71  /// Check if another retry attempt has been requested (any new headers
72  /// except STOP received in the last reply).
73  bool GetNeedRetry(void) const;
74  void SetNeedRetry(void);
75  void ResetNeedRetry(void);
76 
77  /// Clear all options.
78  void Reset(void);
79 
80  /// Check if STOP flag is set.
81  bool IsSetStop(void) const;
82  /// Get STOP reason (or empty string).
83  const string& GetStopReason(void) const;
84  /// Set STOP flag, clear all other options.
85  void SetStop(const string& reason);
86  /// Reset STOP flag and reason.
87  void ResetStop(void);
88 
89  /// Check if retry delay is set.
90  bool IsSetDelay(void) const;
91  /// Get retry delay.
92  const CTimeSpan& GetDelay(void) const;
93  /// Set retry delay.
94  void SetDelay(CTimeSpan delay);
95  /// Set retry delay in seconds.
96  void SetDelay(double sec);
97  /// Reset delay.
98  void ResetDelay(void);
99 
100  /// Check if retry args are set.
101  bool IsSetArgs(void) const;
102  /// Get retry args, URL-decoded.
103  const string& GetArgs(void) const;
104  /// Set retry args. The arguments must be pre-URL-encoded as required.
105  /// The arguments set here will override any values set in the URL.
106  /// @sa SetUrl()
107  void SetArgs(const string& args);
108  /// Set retry args.
109  /// The arguments set here will override any values set in the URL.
110  /// @sa SetUrl()
111  void SetArgs(const CUrlArgs& args);
112  /// Reset args.
113  void ResetArgs(void);
114 
115  /// Check if retry URL is set.
116  bool IsSetUrl(void) const;
117  /// Get retry URL.
118  const string& GetUrl(void) const;
119  /// Set retry URL. If the URL contains any arguments, they may be
120  /// overwritten by the values set through SetArgs().
121  void SetUrl(const string& url);
122  /// Set retry URL. If the URL contains any arguments, they may be
123  /// overwritten by the values set through SetArgs().
124  void SetUrl(const CUrl& url);
125  /// Reset URL.
126  void ResetUrl(void);
127 
128  /// Check if content source is set.
129  bool IsSetContentOverride(void) const;
130  /// Get content source.
131  EContentOverride GetContentOverride(void) const;
132  /// Set content source.
133  void SetContentOverride(EContentOverride content);
134  /// Reset content source.
135  void ResetContentOverride(void);
136 
137  /// Check if retry content is set and content-override flag assumes any
138  /// content (eFromResponse or eData).
139  bool IsSetContent(void) const;
140  /// Get retry content.
141  const string& GetContent(void) const;
142  /// Set retry content. If the content is non-empty and content-override
143  /// is eFromResponse or eData, the value should be sent by the server
144  /// automatically.
145  void SetContent(const string& content);
146  /// Reset content.
147  void ResetContent(void);
148 
149  /// Check if the client should reconnect (e.g. if URL or args have changed).
150  bool NeedReconnect(void) const { return m_NeedReconnect; }
151  /// Set need-reconnect flag.
152  void SetNeedReconnect(void) { m_NeedReconnect = true; }
153  /// Reset need-reconnect flag (e.g. after reconnecting to the new
154  /// URL, but before reading new context).
155  void ResetNeedReconnect(void) { m_NeedReconnect = false; }
156 
157  /// Map of name-value pairs.
159  /// Fill name-value pairs (e.g. to be used by a server application
160  /// to serve retry information). The default implementaion does nothing.
161  virtual void GetValues(TValues& /*values*/) const {}
162 
163 private:
164  enum FFlags {
165  fStop = 1 << 0,
166  fDelay = 1 << 1,
167  fArgs = 1 << 2,
168  fUrl = 1 << 3,
169  fContentOverride = 1 << 4,
170  fContent = 1 << 5
171  };
172  typedef int TFlags;
173 
174  bool x_IsSetFlag(FFlags f) const;
175  void x_SetFlag(FFlags f, bool value = true);
176 
178  string m_StopReason;
180  string m_Args;
181  string m_Url;
183  string m_Content;
186 };
187 
188 
189 /// HTTP-specific retry context implementation.
191 {
192 public:
194  virtual ~CHttpRetryContext(void) {}
195 
196  // Copy functions from CRetryContext - required by CRPCClient_Base
199 
200  void ParseHeader(const char* http_header);
201 
202  /// Fills HTTP header names and values.
203  virtual void GetValues(TValues& values) const;
204 
205  static const char* kHeader_Stop; // X-NCBI-Retry-Stop
206  static const char* kHeader_Delay; // X-NCBI-Retry-Delay
207  static const char* kHeader_Args; // X-NCBI-Retry-Args
208  static const char* kHeader_Url; // X-NCBI-Retry-URL
209  static const char* kHeader_Content; // X-NCBI-Retry-Content
210  static const char* kContent_None; // no_content
211  static const char* kContent_FromResponse; // from_response
212  static const char* kContent_Value; // content:<url-encoded content>
213 };
214 
215 
216 /* @} */
217 
218 
219 //////////////////////////////////////////////////////////////////////////////
220 //
221 // Inline
222 //
223 
224 
225 inline
227  : m_Flags(0),
228  m_ContentOverride(eNot_set),
229  m_NeedRetry(false),
230  m_NeedReconnect(false)
231 {
232 }
233 
234 
235 inline
237 {
238  return m_NeedRetry;
239 }
240 
241 
242 inline
244 {
245  m_NeedRetry = true;
246 }
247 
248 
249 inline
251 {
252  m_NeedRetry = false;
253 }
254 
255 
256 inline
258 {
259  ResetStop();
260  ResetDelay();
261  ResetArgs();
262  ResetUrl();
264  ResetContent();
265  ResetNeedRetry();
267 }
268 
269 
270 inline
271 bool CRetryContext::IsSetStop(void) const
272 {
273  return x_IsSetFlag(fStop);
274 }
275 
276 
277 inline
278 const string& CRetryContext::GetStopReason(void) const
279 {
280  return m_StopReason;
281 }
282 
283 
284 inline
285 void CRetryContext::SetStop(const string& reason)
286 {
287  x_SetFlag(fStop);
288  m_StopReason = reason;
289 }
290 
291 
292 inline
294 {
295  x_SetFlag(fStop, false);
296  m_StopReason.clear();
297 }
298 
299 
300 inline
302 {
303  return x_IsSetFlag(fDelay);
304 }
305 
306 
307 inline
309 {
310  return m_Delay;
311 }
312 
313 
314 inline
316 {
317  x_SetFlag(fDelay);
318  m_Delay = delay;
319 }
320 
321 
322 inline
323 void CRetryContext::SetDelay(double sec)
324 {
325  x_SetFlag(fDelay);
326  m_Delay.Set(sec);
327 }
328 
329 
330 inline
332 {
333  x_SetFlag(fDelay, false);
334  m_Delay.Clear();
335 }
336 
337 
338 inline
339 bool CRetryContext::IsSetArgs(void) const
340 {
341  return x_IsSetFlag(fArgs);
342 }
343 
344 
345 inline
346 const string& CRetryContext::GetArgs(void) const
347 {
348  return m_Args;
349 }
350 
351 
352 inline
353 void CRetryContext::SetArgs(const string& args)
354 {
355  x_SetFlag(fArgs);
356  m_Args = args;
357 }
358 
359 
360 inline
362 {
363  x_SetFlag(fArgs);
365 }
366 
367 
368 inline
370 {
371  x_SetFlag(fArgs, false);
372  m_Args.clear();
373 }
374 
375 
376 inline
377 bool CRetryContext::IsSetUrl(void) const
378 {
379  return x_IsSetFlag(fUrl);
380 }
381 
382 
383 inline
384 const string& CRetryContext::GetUrl(void) const
385 {
386  return m_Url;
387 }
388 
389 
390 inline
391 void CRetryContext::SetUrl(const string& url)
392 {
393  x_SetFlag(fUrl);
394  m_Url = url;
395 }
396 
397 
398 inline
399 void CRetryContext::SetUrl(const CUrl& url)
400 {
401  x_SetFlag(fUrl);
403 }
404 
405 
406 inline
408 {
409  x_SetFlag(fUrl, false);
410  m_Url.clear();
411 }
412 
413 
414 inline
416 {
417  return x_IsSetFlag(fContentOverride) &&
419 }
420 
421 
422 inline
425 {
426  return m_ContentOverride;
427 }
428 
429 
430 inline
432 {
434  m_ContentOverride = content;
435 }
436 
437 
438 inline
440 {
441  x_SetFlag(fContentOverride, false);
443 }
444 
445 
446 inline
448 {
449  return x_IsSetFlag(fContent) &&
451  && !m_Content.empty();
452 }
453 
454 
455 inline
456 const string& CRetryContext::GetContent(void) const
457 {
458  return m_Content;
459 }
460 
461 
462 inline
463 void CRetryContext::SetContent(const string& content)
464 {
466  m_Content = content;
467 }
468 
469 
470 inline
472 {
473  x_SetFlag(fContent, false);
474  m_Content.clear();
475 }
476 
477 
478 inline
480 {
481  return (m_Flags & f) == f;
482 }
483 
484 
485 inline
487 {
488  if ( value ) {
489  m_Flags |= f;
490  }
491  else {
492  m_Flags &= ~f;
493  }
494 }
495 
496 
498 
499 #endif /* RETRY_CTX__HPP */
#define false
Definition: bool.h:36
HTTP-specific retry context implementation.
Definition: retry_ctx.hpp:191
CObject –.
Definition: ncbiobj.hpp:180
CRetryContext – Retry context.
Definition: retry_ctx.hpp:58
CTimeSpan.
Definition: ncbitime.hpp:1313
CUrlArgs::
Definition: ncbi_url.hpp:240
CUrl –.
Definition: ncbi_url.hpp:353
char value[7]
Definition: config.c:431
URL parsing classes.
Include a standard set of the NCBI C++ Toolkit most basic headers.
CS_CONTEXT * ctx
Definition: t0006.c:12
CObject & operator=(const CObject &src) THROWS_NONE
Assignment operator.
Definition: ncbiobj.hpp:482
void x_SetFlag(FFlags f, bool value=true)
Definition: retry_ctx.hpp:486
virtual void GetValues(TValues &) const
Fill name-value pairs (e.g.
Definition: retry_ctx.hpp:161
static const char * kContent_Value
Definition: retry_ctx.hpp:212
map< string, string > TValues
Map of name-value pairs.
Definition: retry_ctx.hpp:158
static const char * kHeader_Stop
Definition: retry_ctx.hpp:205
bool IsSetStop(void) const
Check if STOP flag is set.
Definition: retry_ctx.hpp:271
bool IsSetUrl(void) const
Check if retry URL is set.
Definition: retry_ctx.hpp:377
CTimeSpan m_Delay
Definition: retry_ctx.hpp:179
const CTimeSpan & GetDelay(void) const
Get retry delay.
Definition: retry_ctx.hpp:308
void SetNeedReconnect(void)
Set need-reconnect flag.
Definition: retry_ctx.hpp:152
bool NeedReconnect(void) const
Check if the client should reconnect (e.g. if URL or args have changed).
Definition: retry_ctx.hpp:150
void ResetNeedReconnect(void)
Reset need-reconnect flag (e.g.
Definition: retry_ctx.hpp:155
void SetNeedRetry(void)
Definition: retry_ctx.hpp:243
string m_StopReason
Definition: retry_ctx.hpp:178
static const char * kHeader_Args
Definition: retry_ctx.hpp:207
void ResetUrl(void)
Reset URL.
Definition: retry_ctx.hpp:407
EContentOverride
Content override flags.
Definition: retry_ctx.hpp:64
void SetStop(const string &reason)
Set STOP flag, clear all other options.
Definition: retry_ctx.hpp:285
void ResetContent(void)
Reset content.
Definition: retry_ctx.hpp:471
virtual ~CRetryContext(void)
Definition: retry_ctx.hpp:61
bool IsSetContentOverride(void) const
Check if content source is set.
Definition: retry_ctx.hpp:415
static const char * kHeader_Content
Definition: retry_ctx.hpp:209
virtual ~CHttpRetryContext(void)
Definition: retry_ctx.hpp:194
bool m_NeedReconnect
Definition: retry_ctx.hpp:185
const string & GetUrl(void) const
Get retry URL.
Definition: retry_ctx.hpp:384
const string & GetContent(void) const
Get retry content.
Definition: retry_ctx.hpp:456
bool IsSetDelay(void) const
Check if retry delay is set.
Definition: retry_ctx.hpp:301
bool x_IsSetFlag(FFlags f) const
Definition: retry_ctx.hpp:479
void SetUrl(const string &url)
Set retry URL.
Definition: retry_ctx.hpp:391
static const char * kContent_FromResponse
Definition: retry_ctx.hpp:211
void SetDelay(CTimeSpan delay)
Set retry delay.
Definition: retry_ctx.hpp:315
const string & GetArgs(void) const
Get retry args, URL-decoded.
Definition: retry_ctx.hpp:346
void ResetArgs(void)
Reset args.
Definition: retry_ctx.hpp:369
const string & GetStopReason(void) const
Get STOP reason (or empty string).
Definition: retry_ctx.hpp:278
bool IsSetContent(void) const
Check if retry content is set and content-override flag assumes any content (eFromResponse or eData).
Definition: retry_ctx.hpp:447
bool IsSetArgs(void) const
Check if retry args are set.
Definition: retry_ctx.hpp:339
EContentOverride m_ContentOverride
Definition: retry_ctx.hpp:182
CRetryContext(void)
Definition: retry_ctx.hpp:226
EContentOverride GetContentOverride(void) const
Get content source.
Definition: retry_ctx.hpp:424
void ResetNeedRetry(void)
Definition: retry_ctx.hpp:250
static const char * kHeader_Url
Definition: retry_ctx.hpp:208
void SetContent(const string &content)
Set retry content.
Definition: retry_ctx.hpp:463
bool GetNeedRetry(void) const
Check if another retry attempt has been requested (any new headers except STOP received in the last r...
Definition: retry_ctx.hpp:236
void SetContentOverride(EContentOverride content)
Set content source.
Definition: retry_ctx.hpp:431
string m_Content
Definition: retry_ctx.hpp:183
void ResetContentOverride(void)
Reset content source.
Definition: retry_ctx.hpp:439
TFlags m_Flags
Definition: retry_ctx.hpp:177
void ResetDelay(void)
Reset delay.
Definition: retry_ctx.hpp:331
void Reset(void)
Clear all options.
Definition: retry_ctx.hpp:257
void ResetStop(void)
Reset STOP flag and reason.
Definition: retry_ctx.hpp:293
static const char * kHeader_Delay
Definition: retry_ctx.hpp:206
void SetArgs(const string &args)
Set retry args.
Definition: retry_ctx.hpp:353
static const char * kContent_None
Definition: retry_ctx.hpp:210
@ eFromResponse
On retry send content from the response body.
Definition: retry_ctx.hpp:67
@ eData
On retry send content from the response header.
Definition: retry_ctx.hpp:68
@ eNoContent
Do not send any content on retry.
Definition: retry_ctx.hpp:66
@ eNot_set
No content provided, send the original request data.
Definition: retry_ctx.hpp:65
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
void Set(long seconds, long nanoseconds=0)
Set time span in seconds and nanoseconds.
Definition: ncbitime.hpp:2572
CTimeSpan & Clear(void)
Make the time span "empty",.
Definition: ncbitime.hpp:2523
string GetQueryString(EAmpEncoding amp_enc, NStr::EUrlEncode encode) const
Construct and return complete query string.
Definition: ncbi_url.cpp:225
string ComposeUrl(CUrlArgs::EAmpEncoding amp_enc, const IUrlEncoder *encoder=0) const
Compose the URL.
Definition: ncbi_url.cpp:568
@ eAmp_Char
Use & to separate arguments.
Definition: ncbi_url.hpp:254
#define GetArgs
Avoid preprocessor name clash with the NCBI C Toolkit.
Definition: ncbiapp_api.hpp:53
Defines: CTimeFormat - storage class for time format.
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
NCBI_XUTIL_EXPORT
Parameter to control printing diagnostic message about conversion of static array data from a differe...
Definition: static_set.hpp:72
Modified on Wed Nov 29 02:20:08 2023 by modify_doxy.py rev. 669887