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

Go to the SVN repository for this file.

1 /* $Id: displaytrack_client.cpp 73341 2016-06-30 17:03:39Z meric $
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: Peter Meric
27  *
28  * File Description: NetSchedule grid client for TrackManager display-track request/reply
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
35 #include <serial/rpcbase.hpp>
36 
37 
39 
40 
42  const string& NS_queue,
43  const string& client_name,
44  const string& NC_registry_section
45  )
46  : TBaseClient(NS_service, NS_queue, client_name, NC_registry_section)
47 {
48 }
49 
50 CTMS_DisplayTrack_Client::CTMS_DisplayTrack_Client(const string& NS_registry_section,
51  const string& NC_registry_section
52  )
53  : TBaseClient(NS_registry_section, NC_registry_section)
54 {
55 }
56 
58  : m_Http_svc(http_svc.service),
59  m_Http_session(new CHttpSession())
60 {
61 }
62 
65 {
66  return CTMS_DisplayTrack_Client{HttpService{http_svc}};
67 }
68 
69 bool
71 {
72  if (m_Http_session.NotNull()) {
73  NCBI_THROW(CException, eUnknown, "FetchRawStream() does not support HTTP-based client");
74  }
75  try {
76  const auto retval = TBaseClient::AskStream(requeststr, replystr);
77  return retval.second; // timed_out
78  }
79  catch (const CException& e) {
80  NCBI_REPORT_EXCEPTION("Exception communicating with TMS-DisplayTrack service ", e);
81  return false;
82  }
83  NCBI_THROW(CException, eUnknown, "FetchRawStream(): unexpected code path");
84 }
85 
86 string
88 {
89  CConn_MemoryStream mem_str;
90  // need the asnbincompressed stream to be destroyed in order for stream to be flushed
91  *CAsnBinCompressed::GetOStream(mem_str) << request;
92  string data;
93  mem_str.ToString(&data);
94  ERR_POST(Trace << "encoded request " << mem_str.tellp() << " bytes");
95  return data;
96 }
97 
98 auto
100 -> TReplyRef
101 {
102  CPerfLogGuard pl("Fetch via HTTP");
103  const CTimeout timeout{ 20, 400000 }; // 20400ms
104  const auto& content_type = kEmptyStr;
105  // read data from stream
106  const auto reqdata = RequestToString(request);
107  ERR_POST(Trace << "encoded request " << reqdata.size() << " bytes");
108  // connect to http service
109  const auto response = m_Http_session->Post(m_Http_svc, reqdata, content_type, timeout);
110  if (!response.CanGetContentStream()) {
111  pl.Post(CRequestStatus::e200_Ok, "No response");
112  CConn_MemoryStream mem_str;
113  NcbiStreamCopy(mem_str, response.ErrorStream());
114  string err;
115  mem_str.ToString(&err);
116  ERR_POST(response.GetStatusText() << " -- " << err);
117  return TReplyRef{};
118  }
119  ERR_POST(Trace << "status_code: " << response.GetStatusCode());
120  auto objistr = CAsnBinCompressed::GetIStream(response.ContentStream());
121  auto reply = Ref(new TReply());
122  *objistr >> *reply;
123  pl.Post(CRequestStatus::e200_Ok, "Fetch");
124  return reply;
125 }
126 
127 auto
129 -> TReplyRef
130 {
131  TReplyRef reply;
132  try {
133  if (m_Http_session.NotNull()) {
134  return x_HttpFetch(request);
135  }
136  reply.Reset(new TReply());
137  TBaseClient::Ask(request, reply.GetObject());
138  }
139  catch (const CException& e) {
140  NCBI_REPORT_EXCEPTION("Exception communicating with TMS-DisplayTracks service ", e);
141  reply.Reset();
142  }
143  return reply;
144 }
145 
146 
static unique_ptr< CObjectOStream > GetOStream(CNcbiOstream &ostr, SStreamProp stream_prop=SStreamProp(CCompressStream::eZip))
Return an object output stream (CObjectOStream)
static unique_ptr< CObjectIStream > GetIStream(CNcbiIstream &istr)
Return an object input stream (CObjectIStream)
In-memory stream (a la strstream or stringstream)
CGridRPCBaseClient<typename TConnectTraits>
pair< CNetScheduleJob, bool > AskStream(CNcbiIstream &request, CNcbiOstream &reply) const
Sends an ASN.1 request, waiting specified time.
CPerfLogGuard –.
Definition: perf_log.hpp:252
CRef –.
Definition: ncbiobj.hpp:618
TReplyRef Fetch(const TRequest &request) const
CTMS_DisplayTrack_Client(const string &NS_service, const string &NS_queue, const string &client_name, const string &NC_registry_section)
TReplyRef x_HttpFetch(const TRequest &request) const
objects::CTMgr_DisplayTrackReply TReply
static CTMS_DisplayTrack_Client CreateServiceClient(const string &http_svc="TMS_DisplayTracks")
objects::CTMgr_DisplayTrackRequest TRequest
CRef< CHttpSession > m_Http_session
bool FetchRawStream(CNcbiIstream &requeststr, CNcbiOstream &replystr) const
CTimeout – Timeout interval.
Definition: ncbitime.hpp:1693
string RequestToString(const CTMS_DisplayTrack_Client::TRequest &request)
char data[12]
Definition: iconv.c:80
void ToString(string *)
The CConnMemoryStream::To* methods allow to obtain unread portion of the stream into a single contain...
void Post(CRequestStatus::ECode status, CTempString status_msg=CTempString())
Write the collected resource info and timing to the log.
Definition: perf_log.hpp:578
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#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 Trace(CExceptionArgs_Base &args)
Definition: ncbiexpt.hpp:1179
#define NCBI_REPORT_EXCEPTION(title, ex)
Generate a report on the exception.
Definition: ncbiexpt.hpp:755
bool NotNull(void) const THROWS_NONE
Check if pointer is not null – same effect as NotEmpty().
Definition: ncbiobj.hpp:744
CRef< C > Ref(C *object)
Helper functions to get CRef<> and CConstRef<> objects.
Definition: ncbiobj.hpp:2015
void Reset(void)
Reset reference object.
Definition: ncbiobj.hpp:773
TObjectType & GetObject(void)
Get object.
Definition: ncbiobj.hpp:1011
#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::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
bool NcbiStreamCopy(CNcbiOstream &os, CNcbiIstream &is)
Copy the entire contents of stream "is" to stream "os".
Definition: ncbistre.cpp:211
#define kEmptyStr
Definition: ncbistr.hpp:123
Modified on Wed Jun 19 17:04:53 2024 by modify_doxy.py rev. 669887