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

Go to the SVN repository for this file.

1 /* $Id: taxon3.cpp 94765 2021-09-09 15:09:43Z stakhovv $
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  * Author: Colleen Bollin, based on work by Vladimir Soussov, Michael Domrachev
27  *
28  * File Description:
29  * NCBI Taxonomy 3 service library implementation
30  *
31  */
32 
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbistr.hpp>
35 #include <corelib/ncbienv.hpp>
40 #include <serial/serial.hpp>
41 #include <serial/enumvalues.hpp>
42 #include <serial/objistr.hpp>
43 #include <serial/objostr.hpp>
44 
45 #include <algorithm>
46 
47 
48 #define NCBI_USE_ERRCODE_X Objects_Taxonomy
49 
50 
52 BEGIN_objects_SCOPE // namespace ncbi::objects::
53 
54 
56 {
57  if (init == initialize::yes) {
58  CTaxon3::Init();
59  }
60 }
61 
62 CTaxon3::CTaxon3(const STimeout& timeout, unsigned reconnect_attempts, bool exponential)
63  : m_exponential(exponential)
64 {
65  CTaxon3::Init(&timeout, reconnect_attempts);
66 }
67 
69 {
70 }
71 
72 
73 void
75 {
76  static const STimeout def_timeout = { 20, 0 };
78 }
79 
80 
81 void
82 CTaxon3::Init(const STimeout* timeout, unsigned reconnect_attempts)
83 {
84  SetLastError(nullptr);
85 
86  if ( timeout ) {
87  m_timeout_value = *timeout;
89  } else {
91  }
92 
93  m_nReconnectAttempts = reconnect_attempts;
94 
96  bool bFound = false;
97 
98  m_sService = env.Get("NI_SERVICE_NAME_TAXON3", &bFound);
99  if( !bFound ) {
100  m_sService = env.Get("NI_TAXON3_SERVICE_NAME", &bFound);
101  if( !bFound ) {
102  m_sService = "TaxService3";
103  }
104  }
105 
106 #ifdef USE_TEXT_ASN
108 #else
110 #endif
111 }
112 
113 
116 {
117  SetLastError(nullptr);
118 
119  unsigned reconnect_attempts = 0;
120  const STimeout* pTimeout = m_timeout;
121  STimeout to;
122  if (m_exponential) {
123  to = m_timeout_value;
124  pTimeout = &to;
125  }
126 
127  while (reconnect_attempts < m_nReconnectAttempts) {
128  try {
129  unique_ptr<CObjectOStream> pOut;
130  unique_ptr<CObjectIStream> pIn;
131  unique_ptr<CConn_ServiceStream> pServer(
132  new CConn_ServiceStream( m_sService, fSERV_Any, nullptr, nullptr, pTimeout) );
133 
134  pOut.reset( CObjectOStream::Open(m_eDataFormat, *pServer) );
135  pIn.reset( CObjectIStream::Open(m_eDataFormat, *pServer) );
136 
137  CObjectIStream* ppIn = pIn.release();
138  CObjectOStream* ppOut = pOut.release();
139 
140  try {
141  *ppOut << request;
142  ppOut->Flush();
143  ppOut->Close();
144 
145  try {
146  CRef< CTaxon3_reply > response(new CTaxon3_reply);
147  *ppIn >> *response;
148 
149  delete ppIn;
150  delete ppOut;
151 
152  return response;
153  } catch (exception& e) {
154  SetLastError( e.what() );
155  }
156  } catch (exception& e) {
157  SetLastError( e.what() );
158  }
159 
160  } catch( exception& e ) {
161  SetLastError( e.what() );
162  }
163  reconnect_attempts++;
164  if (m_exponential) {
165  // double the value
166  to.sec <<= 1;
167  to.usec <<= 1;
168  if (to.usec >= 1'000'000) {
169  to.sec++;
170  to.usec -= 1'000'000;
171  }
172  }
173  }
174 
175  // return NULL
176  CRef<CTaxon3_reply> reply;
177  return reply;
178 }
179 
180 void
181 CTaxon3::SetLastError( const char* pchErr )
182 {
183  if( pchErr )
184  m_sLastError.assign( pchErr );
185  else
186  m_sLastError.erase();
187 }
188 
189 
191  COrg_ref::fOrgref_parts result_parts,
192  fT3reply_parts t3reply_parts)
193 {
194  CTaxon3_request request;
195  if( result_parts != COrg_ref::eOrgref_default ||
196  t3reply_parts != eT3reply_default ) {
198  rq->SetJoin().Set().push_back( -int(result_parts) );
199  rq->SetJoin().Set().push_back( -int(t3reply_parts) ); // set return parts
200  request.SetRequest().push_back(rq);
201  }
202  for (CRef<COrg_ref> it : list) {
204  CRef<COrg_ref> org(new COrg_ref);
205  org->Assign(*it);
206  rq->SetOrg(*org);
207  request.SetRequest().push_back(rq);
208  }
209  return SendRequest(request);
210 }
211 
213 CTaxon3::SendNameList(const vector<std::string>& list,
214  COrg_ref::fOrgref_parts result_parts,
215  fT3reply_parts t3reply_parts)
216 {
217  CTaxon3_request request;
218  if( result_parts != COrg_ref::eOrgref_default ||
219  t3reply_parts != eT3reply_default ) {
221  rq->SetJoin().Set().push_back( -int(result_parts) );
222  rq->SetJoin().Set().push_back( -int(t3reply_parts) ); // set return parts
223  request.SetRequest().push_back(rq);
224  }
225  for (const std::string& it : list) {
227  rq->SetName(it);
228  request.SetRequest().push_back(rq);
229  }
230  return SendRequest(request);
231 }
232 
234 CTaxon3::SendTaxidList(const vector<TTaxId>& list,
235  COrg_ref::fOrgref_parts result_parts,
236  fT3reply_parts t3reply_parts)
237 {
238  CTaxon3_request request;
239  if( result_parts != COrg_ref::eOrgref_default ||
240  t3reply_parts != eT3reply_default ) {
242  rq->SetJoin().Set().push_back( -int(result_parts) );
243  rq->SetJoin().Set().push_back( -int(t3reply_parts) ); // set return parts
244  request.SetRequest().push_back(rq);
245  }
246  for (TTaxId it : list) {
248  rq->SetTaxid(TAX_ID_TO(int, it));
249  request.SetRequest().push_back(rq);
250  }
251  return SendRequest(request);
252 }
253 
254 
255 END_objects_SCOPE
This stream exchanges data with a named service, in a constraint that the service is implemented as o...
CNcbiEnvironment –.
Definition: ncbienv.hpp:110
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStream –.
Definition: objostr.hpp:83
unsigned int fOrgref_parts
Definition: Org_ref.hpp:94
@ eOrgref_default
Definition: Org_ref.hpp:92
CRef –.
Definition: ncbiobj.hpp:618
CT3Request –.
Definition: T3Request.hpp:66
CTaxon3_reply –.
CTaxon3_request –.
void SetLastError(const char *err_msg)
Definition: taxon3.cpp:181
virtual CRef< CTaxon3_reply > SendRequest(const CTaxon3_request &request)
Definition: taxon3.cpp:115
unsigned m_nReconnectAttempts
Definition: taxon3.hpp:105
virtual CRef< CTaxon3_reply > SendTaxidList(const vector< TTaxId > &list, COrg_ref::fOrgref_parts parts=(COrg_ref::eOrgref_taxname|COrg_ref::eOrgref_db_taxid), fT3reply_parts t3parts=eT3reply_org)
Definition: taxon3.cpp:234
STimeout m_timeout_value
Definition: taxon3.hpp:103
const STimeout * m_timeout
Definition: taxon3.hpp:102
virtual CRef< CTaxon3_reply > SendOrgRefList(const vector< CRef< COrg_ref > > &list, COrg_ref::fOrgref_parts result_parts=COrg_ref::eOrgref_default, fT3reply_parts t3result_parts=eT3reply_default)
Definition: taxon3.cpp:190
virtual ~CTaxon3()
Definition: taxon3.cpp:68
string m_sLastError
Definition: taxon3.hpp:108
initialize
Definition: taxon3.hpp:60
virtual void Init()
Definition: taxon3.cpp:74
bool m_exponential
Definition: taxon3.hpp:106
CTaxon3(initialize=initialize::no)
Definition: taxon3.cpp:55
string m_sService
Definition: taxon3.hpp:101
ESerialDataFormat m_eDataFormat
Definition: taxon3.hpp:100
virtual CRef< CTaxon3_reply > SendNameList(const vector< std::string > &list, COrg_ref::fOrgref_parts parts=(COrg_ref::eOrgref_taxname|COrg_ref::eOrgref_db_taxid), fT3reply_parts t3parts=(eT3reply_org|eT3reply_status))
Definition: taxon3.cpp:213
unsigned int fT3reply_parts
Definition: itaxon3.hpp:85
@ eT3reply_default
Definition: itaxon3.hpp:83
The NCBI C++ standard methods for dealing with std::string.
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
static HENV env
Definition: transaction2.c:38
#define TAX_ID_TO(T, tax_id)
Definition: ncbimisc.hpp:1110
SStrictId_Tax::TId TTaxId
Taxon id type.
Definition: ncbimisc.hpp:1048
string
Definition: cgiapp.hpp:687
virtual void Assign(const CSerialObject &source, ESerialRecursionMode how=eRecursive)
Set object to copy of another one.
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
void Close(void)
Definition: objostr.cpp:338
static CObjectOStream * Open(ESerialDataFormat format, CNcbiOstream &outStream, bool deleteOutStream)
Create serial object writer and attach it to an output stream.
Definition: objostr.cpp:126
static CObjectIStream * Open(ESerialDataFormat format, CNcbiIstream &inStream, bool deleteInStream)
Create serial object reader and attach it to an input stream.
Definition: objistr.cpp:195
void Flush(void)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
@ fSERV_Any
Definition: ncbi_service.h:79
#define kInfiniteTimeout
Definition: ncbi_types.h:82
unsigned int usec
microseconds (modulo 1,000,000)
Definition: ncbi_types.h:78
unsigned int sec
seconds
Definition: ncbi_types.h:77
TRequest & SetRequest(void)
Assign a value to Request data member.
Definition of all error codes used in objects libraries.
Defines unified interface to application:
Timeout structure.
Definition: ncbi_types.h:76
static const STimeout def_timeout
Definition: taxon1.cpp:98
Modified on Wed Sep 04 15:05:58 2024 by modify_doxy.py rev. 669887