NCBI C++ ToolKit
netcache_params.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: netcache_params.cpp 69195 2015-10-08 15:54:01Z sadyrovr $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Authors: Dmitry Kazimirov
27  *
28  * File Description:
29  * NetCache API parameters (implementation).
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 
35 #include "netcache_params.hpp"
36 
37 #define MAX_NETCACHE_PASSWORD_LENGTH 64
38 
40 
42  const CNetCacheAPIParameters* defaults) :
43  m_DefinedParameters(0),
44  m_Defaults(defaults)
45 {
46 }
47 
49  const CNamedParameterList* optional)
50 {
51  for (; optional; optional = optional->m_MoreParams)
52  if (optional->Is(CNetCacheAPI::eNPT_BlobTTL))
53  SetTTL(Get<unsigned>(optional));
54  else if (optional->Is(CNetCacheAPI::eNPT_Password))
55  SetPassword(Get<std::string>(optional));
56  else if (optional->Is(CNetCacheAPI::eNPT_CachingMode))
57  SetCachingMode(Get<CNetCacheAPI::ECachingMode>(optional));
58  else if (optional->Is(CNetCacheAPI::eNPT_MirroringMode))
59  SetMirroringMode(Get<CNetCacheAPI::EMirroringMode>(optional));
60  else if (optional->Is(CNetCacheAPI::eNPT_ServerCheck))
61  SetServerCheck(Get<ESwitch>(optional));
62  else if (optional->Is(CNetCacheAPI::eNPT_ServerCheckHint))
63  SetServerCheckHint(Get<bool>(optional));
64  else if (optional->Is(CNetCacheAPI::eNPT_ServerToUse))
65  SetServerToUse(Get<CNetServer::TInstance>(optional));
66  else if (optional->Is(CNetCacheAPI::eNPT_ServerLastUsedPtr))
67  SetServerLastUsedPtr(Get<CNetServer*>(optional));
68  else if (optional->Is(CNetCacheAPI::eNPT_MaxBlobAge))
69  SetMaxBlobAge(Get<unsigned>(optional));
70  else if (optional->Is(CNetCacheAPI::eNPT_ActualBlobAgePtr))
71  SetActualBlobAgePtr(Get<unsigned*>(optional));
72  else if (optional->Is(CNetCacheAPI::eNPT_UseCompoundID))
73  SetUseCompoundID(Get<bool>(optional));
74  else if (optional->Is(CNetCacheAPI::eNPT_TryAllServers))
75  SetTryAllServers(Get<bool>(optional));
76  else if (optional->Is(CNetCacheAPI::eNPT_CacheName))
77  SetCacheName(Get<string>(optional));
78 }
79 
80 void CNetCacheAPIParameters::SetTTL(unsigned blob_ttl)
81 {
82  if ((m_TTL = blob_ttl) == 0)
84  else
86 }
87 
88 void CNetCacheAPIParameters::SetPassword(const string& password)
89 {
90  if (password.empty()) {
93  } else {
95 
96  string encoded_password(NStr::PrintableString(password));
97 
98  if (encoded_password.length() > MAX_NETCACHE_PASSWORD_LENGTH) {
100  eAuthenticationError, "Password is too long");
101  }
102 
103  m_Password.assign(" pass=\"");
104  m_Password.append(encoded_password);
105  m_Password.append("\"");
106  }
107 }
108 
109 void CNetCacheAPIParameters::SetMirroringMode(const string& mirroring_mode)
110 {
111  if (!mirroring_mode.empty()) {
112  string enable_mirroring(NStr::Replace(mirroring_mode, "_", kEmptyStr));
113 
115  NStr::CompareNocase(enable_mirroring, "ifkeymirrored") == 0 ||
116  NStr::CompareNocase(enable_mirroring, "onread") == 0 ?
118  StringToBool(enable_mirroring) ?
121  }
122 }
123 
124 void CNetCacheAPIParameters::SetServerCheck(const string& server_check)
125 {
126  if (!server_check.empty())
127  SetServerCheck(NStr::CompareNocase(server_check, "auto") == 0 ?
128  eDefault : StringToBool(server_check) ? eOn : eOff);
129 }
130 
131 void CNetCacheAPIParameters::SetServerCheckHint(const string& server_check_hint)
132 {
133  if (!server_check_hint.empty())
134  SetServerCheckHint(StringToBool(server_check_hint));
135 }
136 
137 #define NETCACHE_API_GET_PARAM_IMPL(param_name) \
138  return m_Defaults == NULL || (m_DefinedParameters & eDP_##param_name) ? \
139  m_##param_name : m_Defaults->Get##param_name()
140 
142 {
144 }
145 
147 {
148  NETCACHE_API_GET_PARAM_IMPL(CachingMode);
149 }
150 
152 {
153  NETCACHE_API_GET_PARAM_IMPL(MirroringMode);
154 }
155 
156 #define NETCACHE_API_GET_PARAM_REVERSE_IMPL(param_name, target_var) \
157  if (m_Defaults != NULL && m_Defaults->Get##param_name(target_var)) \
158  return true; \
159  if ((m_DefinedParameters & eDP_##param_name) == 0) \
160  return false; \
161  *target_var = m_##param_name; \
162  return true
163 
165 {
166  NETCACHE_API_GET_PARAM_REVERSE_IMPL(ServerCheck, server_check);
167 }
168 
169 bool CNetCacheAPIParameters::GetServerCheckHint(bool* server_check_hint) const
170 {
171  NETCACHE_API_GET_PARAM_REVERSE_IMPL(ServerCheckHint, server_check_hint);
172 }
173 
175 {
176  NETCACHE_API_GET_PARAM_IMPL(Password);
177 }
178 
180 {
181  NETCACHE_API_GET_PARAM_IMPL(ServerToUse);
182 }
183 
185 {
186  NETCACHE_API_GET_PARAM_IMPL(ServerLastUsedPtr);
187 }
188 
190 {
191  NETCACHE_API_GET_PARAM_IMPL(MaxBlobAge);
192 }
193 
195 {
196  NETCACHE_API_GET_PARAM_IMPL(ActualBlobAgePtr);
197 }
198 
200 {
201  NETCACHE_API_GET_PARAM_IMPL(UseCompoundID);
202 }
203 
205 {
206  NETCACHE_API_GET_PARAM_IMPL(TryAllServers);
207 }
208 
210 {
211  NETCACHE_API_GET_PARAM_IMPL(CacheName);
212 }
213 
214 bool CNetCacheAPIParameters::StringToBool(const string& bool_str,
215  bool default_value)
216 {
217  try {
218  return NStr::StringToBool(bool_str);
219  }
220  catch (CStringException&) {
221  return default_value;
222  }
223 }
224 
const CNamedParameterList * m_MoreParams
bool Is(int tag) const
NetCache internal exception.
CStringException –.
Definition: ncbistr.hpp:4500
@ eOn
Definition: ncbi_types.h:111
string
Definition: cgiapp.hpp:690
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
void SetUseCompoundID(bool use_compound_id)
ECachingMode
Allows to define caching behavior on a per-call basis.
unsigned * GetActualBlobAgePtr() const
CNetCacheAPI::EMirroringMode GetMirroringMode() const
void SetCacheName(const string &cache_name)
void SetActualBlobAgePtr(unsigned *actual_age_ptr)
void SetServerCheck(ESwitch server_check)
std::string GetPassword() const
EMirroringMode
Mirroring modes.
void SetMirroringMode(CNetCacheAPI::EMirroringMode mirroring_mode)
void SetServerLastUsedPtr(CNetServer *server_last_used_ptr)
TDefinedParameters m_DefinedParameters
CNetCacheAPI::ECachingMode GetCachingMode() const
bool GetServerCheck(ESwitch *server_check) const
static bool StringToBool(const string &bool_str, bool default_value=false)
CNetServer * GetServerLastUsedPtr() const
void SetMaxBlobAge(unsigned max_age)
void SetTTL(unsigned blob_ttl)
void LoadNamedParameters(const CNamedParameterList *optional)
void SetPassword(const string &password)
void SetServerToUse(CNetServer::TInstance server_to_use)
CNetServer GetServerToUse() const
unsigned GetMaxBlobAge() const
void SetTryAllServers(bool try_all_servers)
void SetServerCheckHint(bool server_check_hint)
bool GetServerCheckHint(bool *server_check_hint) const
void SetCachingMode(CNetCacheAPI::ECachingMode caching_mode)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
static bool StringToBool(const CTempString str)
Convert string to bool.
Definition: ncbistr.cpp:2812
static string PrintableString(const CTempString str, TPrintableMode mode=fNewLine_Quote|fNonAscii_Passthru)
Get a printable version of the specified string.
Definition: ncbistr.cpp:3944
#define kEmptyStr
Definition: ncbistr.hpp:123
static int CompareNocase(const CTempString s1, SIZE_TYPE pos, SIZE_TYPE n, const char *s2)
Case-insensitive compare of a substring with another string.
Definition: ncbistr.cpp:219
static string & Replace(const string &src, const string &search, const string &replace, string &dst, SIZE_TYPE start_pos=0, SIZE_TYPE max_replace=0, SIZE_TYPE *num_replace=0)
Replace occurrences of a substring within a string.
Definition: ncbistr.cpp:3305
enum ENcbiSwitch ESwitch
Aux.
#define NETCACHE_API_GET_PARAM_IMPL(param_name)
#define MAX_NETCACHE_PASSWORD_LENGTH
#define NETCACHE_API_GET_PARAM_REVERSE_IMPL(param_name, target_var)
NetCache client parameters.
Modified on Fri Sep 20 14:57:22 2024 by modify_doxy.py rev. 669887