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

Go to the SVN repository for this file.

1 #ifndef CONNECT_SERVICES___NETSERVICE_API__HPP
2 #define CONNECT_SERVICES___NETSERVICE_API__HPP
3 
4 /* $Id: netservice_api.hpp 90054 2020-05-06 17:39:37Z sadyrovr $
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  * Authors: Dmitry Kazimirov, Maxim Didenko
30  *
31  * File Description:
32  * Helper classes for client APIs of network services
33  * like NetCache and NetSchedule.
34  *
35  */
36 
37 #include "srv_connections.hpp"
38 #include "json_over_uttp.hpp"
39 
40 #include <functional>
41 #include <memory>
42 
44 
46 
48 {
49  NCBI_NET_COMPONENT(NetServiceIterator);
50 
51  CNetServer GetServer();
52  CNetServer operator *() {return GetServer();}
53 
54  bool Next();
55  CNetServiceIterator& operator ++() {Next(); return *this;}
56  bool Prev();
57  CNetServiceIterator& operator --() {Prev(); return *this;}
58 
59  double GetRate() const;
60 };
61 
63 {
64 public:
65  virtual bool Consider(CNetServer server) = 0;
66 
67  virtual ~INetServerFinder() {}
68 };
69 
70 struct SNetServerPoolImpl;
71 
73 {
74  NCBI_NET_COMPONENT(NetServerPool);
75 
76  void StickToServer(SSocketAddress address);
77  void StickToServer(const string& host, unsigned short port) { StickToServer(SSocketAddress(host, port)); }
78 
79  void SetCommunicationTimeout(const STimeout& to);
80  const STimeout& GetCommunicationTimeout() const;
81 };
82 
83 struct SNetServiceImpl;
84 
86 {
87  NCBI_NET_COMPONENT(NetService);
88 
89  const string& GetServiceName() const;
90 
91  CNetServerPool GetServerPool();
92 
97  eIncludePenalized
98  };
99 
100  CNetServer GetServer(unsigned host, unsigned short port);
101  CNetServer GetServer(const string& host, unsigned short port);
102 
103  CNetServiceIterator Iterate(EIterationMode mode = eSortByLoad);
104  CNetServiceIterator Iterate(CNetServer::TInstance priority_server);
105  CNetServiceIterator IterateByWeight(const string& key);
106 
107  /// Start iteration excluding 'server' (return the next server
108  /// after 'server' or NULL).
109  CNetServiceIterator ExcludeServer(CNetServer::TInstance server);
110 
111  CNetServiceIterator FindServer(INetServerFinder* finder,
112  EIterationMode mode = eSortByLoad);
113 
114  bool IsLoadBalanced() const;
115 
116  CNetServer::SExecResult FindServerAndExec(const string& cmd,
117  bool multiline_output = false);
118 
119  void ExecOnAllServers(const string& cmd);
120 
126  eMultilineOutput_NetCacheStyle
127  };
128 
129  void PrintCmdOutput(const string& cmd,
130  CNcbiOstream& output_stream,
131  ECmdOutputStyle output_style,
133 
134  CNetService Clone(const string& name);
135 
136  using TEventHandler = function<bool(const string&, CNetServer)>;
137 
138  void SetErrorHandler(TEventHandler error_handler);
139  void SetWarningHandler(TEventHandler warning_handler);
140 
141 #ifdef NCBI_GRID_XSITE_CONN_SUPPORT
142  static void AllowXSiteConnections();
143  static bool IsUsingXSiteProxy();
144 #endif
145 
146  static CNetService Create(const string& api_name, const string& service_name, const string& client_name);
147 };
148 
149 /// This class is for use by the grid_cli utility only.
150 /// @internal
152 {
153 public:
154  virtual CJsonNode ExecOn(CNetServer server) = 0;
155 
156  virtual ~IExecToJson() {}
157 };
158 
159 /// This function is for use by the grid_cli utility only.
160 /// @internal
162 CJsonNode g_ExecToJson(IExecToJson& exec_to_json,
163  CNetService service,
165 
166 /// @deprecated, use CNetService::Create()
167 NCBI_DEPRECATED inline
168 CNetService g_DiscoverService(const string& s, const string& c) { return CNetService::Create("discovery", s, c); }
169 
170 /// @internal
172 void g_AppendClientIPSessionIDHitID(string& cmd);
173 
175 
176 #endif /* CONNECT_SERVICES___NETSERVICE_API__HPP */
JSON node abstraction.
void StickToServer(const string &host, unsigned short port)
function< bool(const string &, CNetServer)> TEventHandler
static bool IsUsingXSiteProxy()
static CNetService Create(const string &api_name, const string &service_name, const string &client_name)
static void AllowXSiteConnections()
This class is for use by the grid_cli utility only.
virtual ~IExecToJson()
virtual CJsonNode ExecOn(CNetServer server)=0
virtual ~INetServerFinder()
virtual bool Consider(CNetServer server)=0
static CS_COMMAND * cmd
Definition: ct_dynamic.c:26
#define bool
Definition: bool.h:34
CVect2< NCBI_PROMOTE(int,U) > operator*(int v1, const CVect2< U > &v2)
Definition: globals.hpp:371
#define NCBI_DEPRECATED
#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
#define NCBI_XCONNECT_EXPORT
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::KEY key
#define NCBI_NET_COMPONENT(component)
S & operator--(CNetRef< S > &r, int)
CJsonNode g_ExecToJson(IExecToJson &exec_to_json, CNetService service, CNetService::EIterationMode iteration_mode=CNetService::eSortByLoad)
This function is for use by the grid_cli utility only.
void g_AppendClientIPSessionIDHitID(string &cmd)
CNetService g_DiscoverService(const string &s, const string &c)
Timeout structure.
Definition: ncbi_types.h:76
Modified on Sun Jun 23 05:18:21 2024 by modify_doxy.py rev. 669887