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

Go to the SVN repository for this file.

1 /* $Id: id1_fetch_simple.cpp 92123 2020-12-22 16:35:10Z grichenk $
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: Denis Vakatov, Aleksey Grichenko
27  *
28  * File Description:
29  * New IDFETCH network client (get Seq-Entry by GI)
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbiapp.hpp>
34 #include <corelib/ncbienv.hpp>
35 #include <corelib/ncbiargs.hpp>
36 #include <corelib/ncbireg.hpp>
37 
38 #include <connect/ncbi_util.h>
39 #include <connect/ncbi_socket.h>
42 
43 #include <serial/serial.hpp>
44 #include <serial/objistrasnb.hpp>
45 #include <serial/objostrasnb.hpp>
46 
52 
53 #include <memory>
54 
55 #include<common/test_assert.h> /* This header must go last */
56 
57 
60 
61 
62 
63 /////////////////////////////////
64 // CId1FetchApp::
65 //
66 
67 class CId1FetchApp : public CNcbiApplication
68 {
69  virtual void Init(void);
70  virtual int Run(void);
71  virtual void Exit(void);
72 private:
73  bool m_ResetDiagStream = false;
74 };
75 
76 
77 void CId1FetchApp::Init(void)
78 {
79  // Prepare command line descriptions
80  //
81 
82  // Create
83  unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
84 
85  // GI
86  arg_desc->AddOptionalKey
87  ("gi", "SeqEntryID",
88  "GI id of the Seq-Entry to fetch",
90  // Request
91  arg_desc->AddOptionalKey
92  ("req", "Request",
93  "ID1 request in ASN.1 text format",
95  arg_desc->AddOptionalKey
96  ("in", "RequestFile",
97  "File to read request(s) from",
99 
100  // Output format
101  arg_desc->AddDefaultKey
102  ("fmt", "OutputFormat",
103  "Format to dump the resulting data in",
105  arg_desc->SetConstraint("fmt", &(*new CArgAllow_Strings,
106  "asn", "asnb", "xml", "raw", "none"));
107 
108  // Output datafile
109  arg_desc->AddDefaultKey
110  ("out", "ResultFile",
111  "File to dump the resulting data to",
113 
114  // Log file
115  arg_desc->AddOptionalKey
116  ("log", "LogFile",
117  "File to post errors and messages to",
119  0);
120 
121  // Server to connect
122  arg_desc->AddDefaultKey
123  ("server", "Server",
124  "ID1 server name",
126 
127  // Program description
128  string prog_description =
129  "Fetch SeqEntry from ID server by its GI id";
130  arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
131  prog_description, false);
132 
133  // Pass argument descriptions to the application
134  //
135  SetupArgDescriptions(arg_desc.release());
136 }
137 
138 
139 int CId1FetchApp::Run(void)
140 {
141  // Process command line args
142  const CArgs& args = GetArgs();
143 
144  // Setup and tune logging facilities
145  if ( args["log"] ) {
146  m_ResetDiagStream = true;
147  SetDiagStream( &args["log"].AsOutputFile() );
148  }
149 #ifdef _DEBUG
150  // SetDiagTrace(eDT_Enable);
153 #endif
154 
155  // Setup application registry, error log, and MT-lock for CONNECT library
157 
158  // Compose request to ID1 server
159  typedef vector<CRef<CID1server_request> > TReqs;
160  TReqs reqs;
161  if ( args["gi"] ) {
162  TGi gi = GI_FROM(TIntId, args["gi"].AsIntId());
163  // id1_request.SetGetsefromgi().SetGi() = gi;
165  req->SetGetseqidsfromgi(gi);
166  reqs.push_back(req);
167  }
168  else if ( args["req"] ) {
169  string text = args["req"].AsString();
170  if ( text.find("::=") == NPOS ) {
171  text = "ID1server-request ::= " + text;
172  }
175  in >> MSerial_AsnText >> *req;
176  reqs.push_back(req);
177  }
178  else if ( args["in"] ) {
179  unique_ptr<CObjectIStream> req_input
180  (CObjectIStream::Open(eSerial_AsnText, args["in"].AsInputFile()));
181  while ( !req_input->EndOfData() ) {
183  *req_input >> *req;
184  reqs.push_back(req);
185  }
186  }
187 
188  // Open connection to ID1 server
189  string server_name = args["server"].AsString();
190  STimeout tmout; tmout.sec = 9; tmout.usec = 0;
191  CConn_ServiceStream id1_server(server_name, fSERV_Any, 0, 0, &tmout);
192  CObjectOStreamAsnBinary id1_server_output(id1_server);
193  CObjectIStreamAsnBinary id1_server_input(id1_server);
194 
195  CNcbiOstream* datafile =
196  args["fmt"].AsString() == "none"? 0: &args["out"].AsOutputFile();
198  unique_ptr<CObjectOStream> id1_client_output;
199  if ( datafile ) {
200  string fmt = args["fmt"].AsString();
201  if (fmt == "asn") {
203  } else if (fmt == "asnb") {
205  } else if (fmt == "xml") {
207  }
208  if ( format != eSerial_None ) {
209  id1_client_output.reset(CObjectOStream::Open(format, *datafile));
210  }
211  }
212 
213  ITERATE ( TReqs, it, reqs ) {
214  // Send request to the server
215  id1_server_output << **it;
216  id1_server_output.Flush();
217 
218  // Get response (Seq-Entry) from the server, dump it to the
219  // output data file in the requested format
220  // Dump the raw data coming from server "as is", if so specified
221  if ( !id1_client_output.get() && datafile ) {
222  *datafile << id1_server.rdbuf();
223  return 0; // Done
224  }
225  CID1server_back id1_response;
226  // Read server response in ASN.1 binary format
227  id1_server_input.UseMemoryPool();
228  id1_server_input >> id1_response;
229  if ( id1_client_output.get() ) {
230  // Dump server response in the specified format
231  *id1_client_output << id1_response;
232  if ( format != eSerial_AsnBinary ) {
233  id1_client_output->FlushBuffer();
234  *datafile << '\n';
235  }
236  }
237  }
238 
239  return 0; // Done
240 }
241 
242 
243 
244 // Cleanup
245 void CId1FetchApp::Exit(void)
246 {
248  if ( m_ResetDiagStream ) {
249  SetDiagStream(0);
250  }
251 }
252 
253 
254 
255 /////////////////////////////////////////////////////////////////////////////
256 // MAIN
257 //
258 
259 int main(int argc, const char* argv[])
260 {
261  return CId1FetchApp().AppMain(argc, argv);
262 }
User-defined methods of the data storage class.
User-defined methods of the data storage class.
CArgAllow_Strings –.
Definition: ncbiargs.hpp:1641
CArgDescriptions –.
Definition: ncbiargs.hpp:541
CArgs –.
Definition: ncbiargs.hpp:379
This stream exchanges data with a named service, in a constraint that the service is implemented as o...
@ID1server_back.hpp User-defined methods of the data storage class.
CID1server_request –.
virtual int Run(void)
Run the application.
virtual int Run(void)
Run the application.
Definition: id1_fetch.cpp:267
virtual void Exit(void)
Cleanup on application exit.
virtual void Init(void)
Initialize the application.
Definition: id1_fetch.cpp:120
virtual void Init(void)
Initialize the application.
bool m_ResetDiagStream
Definition: id1_fetch.cpp:116
virtual void Exit(void)
Cleanup on application exit.
Definition: id1_fetch.cpp:666
CObjectIStreamAsnBinary –.
Definition: objistrasnb.hpp:59
CObjectOStreamAsnBinary –.
Definition: objostrasnb.hpp:58
CRef –.
Definition: ncbiobj.hpp:618
#define GI_FROM(T, value)
Definition: ncbimisc.hpp:1086
const CNcbiRegistry & GetConfig(void) const
Get the application's cached configuration parameters (read-only).
virtual const CArgs & GetArgs(void) const
Get parsed command line arguments.
Definition: ncbiapp.cpp:305
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:819
virtual void SetupArgDescriptions(CArgDescriptions *arg_desc)
Setup the command line argument descriptions.
Definition: ncbiapp.cpp:1195
#define ITERATE(Type, Var, Cont)
ITERATE macro to sequence through container elements.
Definition: ncbimisc.hpp:815
Int8 TIntId
Definition: ncbimisc.hpp:999
const CNcbiArguments & GetArguments(void) const
Get the application's cached unprocessed command-line arguments.
@ fBinary
Open as binary file; for eInputFile, eOutputFile, eIOFile.
Definition: ncbiargs.hpp:620
@ eInputFile
Name of file (must exist and be readable)
Definition: ncbiargs.hpp:595
@ eIntId
Convertible to TIntId (int or Int8 depending on NCBI_INT8_GI)
Definition: ncbiargs.hpp:593
@ eString
An arbitrary string.
Definition: ncbiargs.hpp:589
@ eOutputFile
Name of file (must be writable)
Definition: ncbiargs.hpp:596
void SetDiagPostFlag(EDiagPostFlag flag)
Set the specified flag (globally).
Definition: ncbidiag.cpp:6070
EDiagSev SetDiagPostLevel(EDiagSev post_sev=eDiag_Error)
Set the threshold severity for posting the messages.
Definition: ncbidiag.cpp:6129
void SetDiagStream(CNcbiOstream *os, bool quick_flush=true, FDiagCleanup cleanup=0, void *cleanup_data=0, const string &stream_name="")
Set diagnostic stream.
Definition: ncbidiag.cpp:8083
@ eDPF_All
All flags (except for the "unusual" ones!)
Definition: ncbidiag.hpp:718
@ eDiag_Info
Informational message.
Definition: ncbidiag.hpp:651
#define MSerial_AsnText
I/O stream manipulators –.
Definition: serialbase.hpp:696
ESerialDataFormat
Data file format.
Definition: serialdef.hpp:71
@ eSerial_AsnText
ASN.1 text.
Definition: serialdef.hpp:73
@ eSerial_Xml
XML.
Definition: serialdef.hpp:75
@ eSerial_None
Definition: serialdef.hpp:72
@ eSerial_AsnBinary
ASN.1 binary.
Definition: serialdef.hpp:74
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
@ fSERV_Any
Definition: ncbi_service.h:79
EIO_Status SOCK_ShutdownAPI(void)
Cleanup; destroy all internal/system data & resources used by the SOCK API.
Definition: ncbi_socket.c:1019
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NPOS
Definition: ncbistr.hpp:133
void CONNECT_Init(const IRWRegistry *reg=0, CRWLock *lock=0, TConnectInitFlags flag=eConnectInit_OwnNothing, FSSLSetup ssl=0)
Init [X]CONNECT library with the specified "reg" and "lock" (ownership for either or both can be deta...
unsigned int usec
microseconds (modulo 1,000,000)
Definition: ncbi_types.h:78
unsigned int sec
seconds
Definition: ncbi_types.h:77
USING_SCOPE(objects)
int main(int argc, const char *argv[])
USING_NCBI_SCOPE
static void text(MDB_val *v)
Definition: mdb_dump.c:62
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines command line argument related classes.
Defines unified interface to application:
Process information in the NCBI Registry, including working with configuration files.
static Format format
Definition: njn_ioutil.cpp:53
std::istream & in(std::istream &in_, double &x_)
Timeout structure.
Definition: ncbi_types.h:76
Modified on Mon May 20 05:00:10 2024 by modify_doxy.py rev. 669887