DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
Host.cpp
Go to the documentation of this file.
1// DUNE DAQ modification notice:
2// This file has been modified from the original ATLAS system source for the DUNE DAQ project.
3// Fork baseline commit: system-00-00-20 (2020-09-25).
4// Renamed since fork: yes (from src/Host.cxx to src/Host.cpp).
5
6/*
7 * Host.cxx
8 * OksSystem
9 *
10 * Created by Matthias Wiesmann on 03.02.05.
11 * Copyright 2005 CERN. All rights reserved.
12 *
13 */
14
15
16#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/utsname.h>
19#include <netdb.h>
20
21#include <unistd.h>
22
23#include <iostream>
24#include <sstream>
25#include <strings.h>
26
27#include "okssystem/Host.hpp"
28
29#define BUFFER_SIZE 256
30
36
37struct sockaddr_in OksSystem::Host::resolve(const std::string &name) throw() {
38 struct addrinfo *info_ptr;
39 const char *str = name.c_str();
40 const int status = getaddrinfo(str,0,0,&info_ptr);
41 struct sockaddr_in address;
42 bzero(&address,sizeof(address));
43 address.sin_family = AF_INET;
44 address.sin_addr.s_addr=0;
45 if (status==0) {
46 struct sockaddr_in *ptr = (sockaddr_in *) info_ptr->ai_addr;
47 address = *ptr;
48 freeaddrinfo(info_ptr);
49 } // if
50 return address;
51} // resolve
52
57
58std::string OksSystem::Host::resolve(struct sockaddr_in address) throw() {
59 char buffer[NI_MAXHOST];
60 const struct sockaddr *ptr = (const struct sockaddr *) &address;
61 const int status= getnameinfo(ptr,sizeof(address),buffer,sizeof(buffer),0,0,0);
62 if (status!=0) return to_string(address);
63 return std::string(buffer);
64} // resolve
65
73
74std::string OksSystem::Host::expand(const std::string &name) throw() {
75 const struct sockaddr_in address = resolve(name);
76 if (address.sin_addr.s_addr!=0) return resolve(address);
77 return std::string(name) ;
78} // expand
79
85
86std::string OksSystem::Host::to_string(struct sockaddr_in ip_addr) {
87 const char* s = inet_ntoa(ip_addr.sin_addr);
88 return std::string(s);
89} // to_string
90
91// Constructors, destructors
92// --------------------------
93
96
97OksSystem::Host::Host() throw() {} // Host
98
100 m_name = other.m_name;
101 if (! other.m_full_name.empty()) {
102 m_full_name = other.m_full_name;
103 } // if
104} // Host
105
106
110
111OksSystem::Host::Host(const std::string &s_name) {
112 m_name = s_name;
113} // Host
114
115
116OksSystem::Host::Host(struct sockaddr_in ip_addr) {
117 m_name = resolve(ip_addr);
119} // Host
120
121
123} // OksSystem
124
125// Operator
126// --------------------------
127
131
132OksSystem::Host::operator struct sockaddr_in() const throw() {
133 return ip();
134} // struct sockaddr_in
135
141
142bool OksSystem::Host::equals(const Host &other) const throw() {
143 return full_name()==other.full_name();
144} // equals
145
146
147// Methods
148// --------------------------
149
153
154const std::string & OksSystem::Host::name() const throw() { return m_name;}
155
159
160struct sockaddr_in OksSystem::Host::ip() const throw() { return resolve(m_name);}
161
165
166const std::string & OksSystem::Host::full_name() const throw() {
167 if (m_full_name.empty()) {
168 m_full_name = expand(m_name);
169 } // if
170 return m_full_name;
171} // full_name
172
176
177std::string OksSystem::Host::ip_string() const throw() {
178 const struct sockaddr_in address = ip();
179 return to_string(address);
180} // ip_string
181
182
183
184bool OksSystem::operator ==(const Host &a, const Host &b) throw() {
185 return a.equals(b);
186} // operator ==
187
188bool OksSystem::operator !=(const Host &a, const Host &b) throw() {
189 return ! a.equals(b);
190} // operator ≠
191
192// Local Host
193// --------------------------
194
198
200
206
207const std::string & OksSystem::LocalHost::local_name() throw() {
208 return instance()->name();
209} // localhostname
210
215
216const std::string & OksSystem::LocalHost::full_local_name() throw() {
217 return instance()->full_name();
218} // fulllocalhostname
219
225
227 if (0==s_instance) {
228 s_instance = new LocalHost();
229 } //
230 return s_instance;
231} // instance
232
237
239 struct utsname u_name_data;
240 const int status = ::uname(&u_name_data);
241 if (status==0) {
242 m_name = u_name_data.nodename;
243 m_os_name = u_name_data.sysname;
244 m_release = u_name_data.release;
245 m_version = u_name_data.version;
246 m_machine = u_name_data.machine;
247 } else {
248 char buffer[NI_MAXHOST];
249 const int host_status = gethostname(buffer,sizeof(buffer));
250 if (host_status==0) {
251 m_name = buffer;
252 } else { // both uname and gethostname screwed
253 m_name.clear();
254 } // check for host_name
255 } // uname failed.
256} // LocalHost
257
259 // delete s_instance;
260}
261
263
264const std::string & OksSystem::LocalHost::os_name() const throw() {
265 return m_os_name;
266} // os_name
267
269
270const std::string & OksSystem::LocalHost::os_release() const throw() {
271 return m_release;
272} // os_release
273
275
276const std::string & OksSystem::LocalHost::os_version() const throw() {
277 return m_version;
278} // os_version
279
281
282const std::string & OksSystem::LocalHost::machine() const throw() {
283 return m_machine;
284} // machine
285
290
291const std::string & OksSystem::LocalHost::description() const throw() {
292 if (m_description.empty()) {
293 std::ostringstream stream;
294 stream << m_os_name << " " << m_release << "/" << m_machine;
295 m_description = stream.str();
296 }
297 return m_description;
298} // description
299
306
307const char* OksSystem::getfullhost() throw() {
308 const std::string & str = OksSystem::LocalHost::full_local_name();
309 return str.c_str();
310} // full_host_name
311
317
318bool OksSystem::operator ==(const LocalHost &, const LocalHost & ) throw()
319{
320 return true;
321}
322
328
329bool OksSystem::operator !=(const LocalHost &, const LocalHost & ) throw()
330{
331 return false;
332}
333
334std::ostream& operator<<(std::ostream& stream, const OksSystem::Host& host) {
335 stream << host.full_name();
336 return stream;
337} // operator<<
338
339
340
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:102
Network host.
Definition Host.hpp:33
virtual ~Host()
destructor
Definition Host.cpp:122
std::string ip_string() const
get ip in string mode
Definition Host.cpp:177
static struct sockaddr_in resolve(const std::string &name)
name to ip conversion
Definition Host.cpp:37
const std::string & name() const
get name
Definition Host.cpp:154
static std::string expand(const std::string &name)
expands to full name
Definition Host.cpp:74
struct sockaddr_in ip() const
get ip address
Definition Host.cpp:160
static std::string to_string(struct sockaddr_in ip_addr)
ip to string conversion
Definition Host.cpp:86
bool equals(const Host &other) const
equality method
Definition Host.cpp:142
const std::string & full_name() const
get fully qualified name
Definition Host.cpp:166
std::string m_name
name of the host
Definition Host.hpp:35
Host()
constructor for current host
Definition Host.cpp:97
std::string m_full_name
cached fully qualified host name
Definition Host.hpp:36
Network host - localhost.
Definition Host.hpp:67
std::string m_os_name
name of the operating system
Definition Host.hpp:71
std::string m_version
version of the operating system
Definition Host.hpp:73
const std::string & description() const
machine type *‍/
Definition Host.cpp:291
static LocalHost * s_instance
singleton for local host
Definition Host.hpp:70
const std::string & os_release() const
release of the operating system
Definition Host.cpp:270
static const std::string & full_local_name()
fully qualified local host name
Definition Host.cpp:216
std::string m_machine
machine type
Definition Host.hpp:74
const std::string & os_name() const
name of the operating system
Definition Host.cpp:264
static const LocalHost * instance()
pointer to the singleton local host
Definition Host.cpp:226
const std::string & os_version() const
version of the operating system
Definition Host.cpp:276
static const std::string & local_name()
localhostname
Definition Host.cpp:207
std::string m_release
release of the operating system
Definition Host.hpp:72
const std::string & machine() const
version of the operating system
Definition Host.cpp:282
std::string m_description
machine description (cached)
Definition Host.hpp:75
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:188
const char * getfullhost()
get fully qualified host name
Definition Host.cpp:307
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:184