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

Go to the SVN repository for this file.

1 #ifndef GRPC_INTEGRATION_IMPL___GRPC_SUPPORT__HPP
2 #define GRPC_INTEGRATION_IMPL___GRPC_SUPPORT__HPP
3 
4 /* $Id: grpc_support.hpp 78881 2017-07-26 15:20:27Z ucko $
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: Aaron Ucko
30  *
31  */
32 
33 /// @file grpc_support.hpp
34 /// Classes and functions supporting grpc_integration.[ch]pp that
35 /// nothing else is expected to use directly.
36 
37 
38 #include <corelib/ncbimtx.hpp>
39 #include <corelib/request_ctx.hpp>
41 #include <google/protobuf/message.h>
42 #include <grpc++/server.h>
43 
44 
45 /** @addtogroup Miscellaneous
46  *
47  * @{
48  */
49 
50 
52 
53 grpc::Status g_AsGRPCStatus (CRequestStatus::ECode status_code);
54 grpc::StatusCode g_AsGRPCStatusCode(CRequestStatus::ECode status_code);
55 
56 
58 {
59 public:
60  typedef google::protobuf::Message TMessage;
61  CGRPCRequestLogger(grpc::ServerContext* sctx, CTempString method_name,
62  const TMessage& request, const TMessage& reply);
64 
65 private:
68  const TMessage& m_Reply;
70 };
71 
72 
73 class CGRPCServerCallbacks : public grpc::Server::GlobalCallbacks
74 {
75 public:
76  void PreSynchronousRequest (grpc::ServerContext* sctx) override
77  { BeginRequest(sctx); }
78  void PostSynchronousRequest(grpc::ServerContext* sctx) override
79  { EndRequest(sctx); }
80 
81  // Static methods for use by any asynchronous service implementations,
82  // which have no obvious provision for global hooks.
83  static void BeginRequest(grpc::ServerContext* sctx);
84  static void EndRequest (grpc::ServerContext* sctx);
85 };
86 
87 
88 //////////////////////////////////////////////////////////////////////
89 
90 
91 inline
92 grpc::Status g_AsGRPCStatus(CRequestStatus::ECode status_code)
93 {
94  if (status_code >= CRequestStatus::e100_Continue
95  && status_code < CRequestStatus::e400_BadRequest) {
96  return grpc::Status::OK;
97  } else {
98  return grpc::Status(g_AsGRPCStatusCode(status_code),
100  }
101 }
102 
103 
104 inline
105 CGRPCRequestLogger::CGRPCRequestLogger(grpc::ServerContext* sctx,
106  CTempString method_name,
107  const TMessage& request,
108  const TMessage& reply)
109  : m_DiagContext(GetDiagContext()),
110  m_RequestContext(m_DiagContext.GetRequestContext()),
111  m_Reply(reply), m_ManagingRequest(false)
112 {
114  m_ManagingRequest = true;
116  }
117  m_DiagContext.Extra().Print("method_name", method_name);
118  m_RequestContext.SetBytesRd(request.ByteSizeLong());
119 }
120 
121 inline
123 {
124  m_RequestContext.SetBytesWr(m_Reply.ByteSizeLong());
125  if (m_ManagingRequest) {
127  }
128 }
129 
131 
132 
133 /* @} */
134 
135 #endif /* GRPC_INTEGRATION_IMPL___GRPC_SUPPORT__HPP */
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
#define false
Definition: bool.h:36
void PrintRequestStop(void)
Print request stop message (for request-driven applications)
Definition: ncbidiag.cpp:2778
CDiagContext_Extra & Print(const string &name, const string &value)
The method does not print the argument, but adds it to the string.
Definition: ncbidiag.cpp:2622
void SetBytesWr(Int8 bytes)
CDiagContext & GetDiagContext(void)
Get diag context instance.
Definition: logging.cpp:818
CDiagContext_Extra Extra(void) const
Create a temporary CDiagContext_Extra object.
Definition: ncbidiag.hpp:2095
static string GetStdStatusMessage(ECode code)
void SetBytesRd(Int8 bytes)
EDiagAppState GetAppState(void) const
Return application state for the current thread if it's set.
Definition: ncbidiag.cpp:2802
@ eDiagAppState_RequestBegin
RB.
Definition: ncbidiag.hpp:794
void PostSynchronousRequest(grpc::ServerContext *sctx) override
google::protobuf::Message TMessage
const TMessage & m_Reply
grpc::StatusCode g_AsGRPCStatusCode(CRequestStatus::ECode status_code)
CDiagContext & m_DiagContext
static void EndRequest(grpc::ServerContext *sctx)
void PreSynchronousRequest(grpc::ServerContext *sctx) override
CRequestContext & m_RequestContext
grpc::Status g_AsGRPCStatus(CRequestStatus::ECode status_code)
static void BeginRequest(grpc::ServerContext *sctx)
CGRPCRequestLogger(grpc::ServerContext *sctx, CTempString method_name, const TMessage &request, const TMessage &reply)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
Multi-threading – mutexes; rw-locks; semaphore.
Defines CRequestContext class for NCBI C++ diagnostic API.
Defines CRequestStatus class for NCBI C++ diagnostic API.
Modified on Sun May 19 04:46:26 2024 by modify_doxy.py rev. 669887