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

Go to the SVN repository for this file.

1 /* $Id: lbsm_resolver.cpp 84219 2018-10-25 17:53:05Z saprykin $
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: Dmitri Dmitrienko
27  *
28  * File Description:
29  *
30  * Wrapper class around lbsm "C"-API
31  *
32  */
33 
34 #include <ncbi_pch.hpp>
35 
36 #include <memory>
37 #include <vector>
38 #include <sstream>
39 #include <algorithm>
40 
41 #include <connect/ncbi_connutil.h>
44 
46 
47 bool LbsmLookup::s_Resolve(const string & service, vector<pair<string, int>> & result, TSERV_Type serv_type)
48 {
49  unique_ptr<SConnNetInfo, function<void(SConnNetInfo*)> > net_info(
50  ConnNetInfo_Create(service.c_str()),
51  [](SConnNetInfo* net_info)
52  {
53  ConnNetInfo_Destroy(net_info);
54  }
55  );
56 
57  unique_ptr<SSERV_IterTag, function<void(SSERV_IterTag*)> > iter(
58  SERV_Open(service.c_str(), serv_type, 0, net_info.get()),
59  [](SSERV_IterTag* iter)
60  {
61  SERV_Close(iter);
62  }
63  );
64 
65  const SSERV_Info * info;
66  while (info = SERV_GetNextInfo(iter.get()), info != nullptr) {
67  char buff[128];
68  SOCK_HostPortToString(info->host, info->port, buff, sizeof(buff));
69  result.push_back(make_pair(buff, info->rate));
70  }
71 
72  sort(result.begin(), result.end(),
73  [](const pair<string, int> & a, const pair<string, int> & b) -> bool
74  {
75  return a.second > b.second;
76  }
77  );
78 
79  return true;
80 }
81 
82 
83 string LbsmLookup::s_Resolve(const string & service, char delimiter, TSERV_Type serv_type)
84 {
85  stringstream rv;
86  vector<pair<string,int>> result;
87  if (s_Resolve(service, result, serv_type)) {
88  bool is_first = true;
89  for (auto const & el : result) {
90  if (!is_first) {
91  rv << delimiter;
92  }
93  is_first = false;
94  rv << el.first;
95  }
96  }
97  return rv.str();
98 }
99 
static bool s_Resolve(const string &service, vector< pair< string, int >> &result, TSERV_Type serv_type=fSERV_Any)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
SSERV_InfoCPtr SERV_GetNextInfo(SERV_ITER iter)
Same as "SERV_GetNextInfoEx(., 0)" – i.e.
Definition: ncbi_service.c:936
SERV_ITER SERV_Open(const char *service, TSERV_Type types, unsigned int preferred_host, const SConnNetInfo *net_info)
Same as "SERV_OpenEx(., ., ., ., 0, 0)" – i.e.
Definition: ncbi_service.c:809
unsigned int TSERV_Type
Bitwise OR of ESERV_Type[Special].
Definition: ncbi_service.h:94
size_t SOCK_HostPortToString(unsigned int host, unsigned short port, char *buf, size_t bufsize)
Print numeric string "host:port" into a buffer provided, not to exceed 'bufsize' bytes (including the...
Definition: ncbi_socket.c:8945
SConnNetInfo * ConnNetInfo_Create(const char *service)
static MDB_envinfo info
Definition: mdb_load.c:37
constexpr auto sort(_Init &&init)
unsigned int a
Definition: ncbi_localip.c:102
static const char delimiter[]
else result
Definition: token2.c:20
Modified on Thu Jul 18 16:04:56 2024 by modify_doxy.py rev. 669887