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#include <netdb.h>
16#include <sys/socket.h>
17#include <sys/types.h>
18#include <sys/utsname.h>
19
20#include <unistd.h>
21
22#include <iostream>
23#include <sstream>
24#include <strings.h>
25
26#include "okssystem/Host.hpp"
27
28#define BUFFER_SIZE 256
29
35
36struct sockaddr_in
37OksSystem::Host::resolve(const std::string& name) throw()
38{
39 struct addrinfo* info_ptr;
40 const char* str = name.c_str();
41 const int status = getaddrinfo(str, 0, 0, &info_ptr);
42 struct sockaddr_in address;
43 bzero(&address, sizeof(address));
44 address.sin_family = AF_INET;
45 address.sin_addr.s_addr = 0;
46 if (status == 0) {
47 struct sockaddr_in* ptr = (sockaddr_in*)info_ptr->ai_addr;
48 address = *ptr;
49 freeaddrinfo(info_ptr);
50 } // if
51 return address;
52} // resolve
53
58
59std::string
60OksSystem::Host::resolve(struct sockaddr_in address) throw()
61{
62 char buffer[NI_MAXHOST];
63 const struct sockaddr* ptr = (const struct sockaddr*)&address;
64 const int status = getnameinfo(ptr, sizeof(address), buffer, sizeof(buffer), 0, 0, 0);
65 if (status != 0)
66 return to_string(address);
67 return std::string(buffer);
68} // resolve
69
77
78std::string
79OksSystem::Host::expand(const std::string& name) throw()
80{
81 const struct sockaddr_in address = resolve(name);
82 if (address.sin_addr.s_addr != 0)
83 return resolve(address);
84 return std::string(name);
85} // expand
86
92
93std::string
94OksSystem::Host::to_string(struct sockaddr_in ip_addr)
95{
96 const char* s = inet_ntoa(ip_addr.sin_addr);
97 return std::string(s);
98} // to_string
99
100// Constructors, destructors
101// --------------------------
102
105
106OksSystem::Host::Host() throw() {} // Host
107
109{
110 m_name = other.m_name;
111 if (!other.m_full_name.empty()) {
112 m_full_name = other.m_full_name;
113 } // if
114} // Host
115
119
120OksSystem::Host::Host(const std::string& s_name)
121{
122 m_name = s_name;
123} // Host
124
125OksSystem::Host::Host(struct sockaddr_in ip_addr)
126{
127 m_name = resolve(ip_addr);
129} // Host
130
131OksSystem::Host::~Host() throw() {} // OksSystem
132
133// Operator
134// --------------------------
135
139
140OksSystem::Host::operator struct sockaddr_in() const throw()
141{
142 return ip();
143} // struct sockaddr_in
144
150
151bool
152OksSystem::Host::equals(const Host& other) const throw()
153{
154 return full_name() == other.full_name();
155} // equals
156
157// Methods
158// --------------------------
159
163
164const std::string&
166{
167 return m_name;
168}
169
173
174struct sockaddr_in
175OksSystem::Host::ip() const throw()
176{
177 return resolve(m_name);
178}
179
183
184const std::string&
186{
187 if (m_full_name.empty()) {
188 m_full_name = expand(m_name);
189 } // if
190 return m_full_name;
191} // full_name
192
196
197std::string
199{
200 const struct sockaddr_in address = ip();
201 return to_string(address);
202} // ip_string
203
204bool
205OksSystem::operator==(const Host& a, const Host& b) throw()
206{
207 return a.equals(b);
208} // operator ==
209
210bool
211OksSystem::operator!=(const Host& a, const Host& b) throw()
212{
213 return !a.equals(b);
214} // operator ≠
215
216// Local Host
217// --------------------------
218
222
224
230
231const std::string&
233{
234 return instance()->name();
235} // localhostname
236
241
242const std::string&
244{
245 return instance()->full_name();
246} // fulllocalhostname
247
253
256{
257 if (0 == s_instance) {
258 s_instance = new LocalHost();
259 } //
260 return s_instance;
261} // instance
262
267
269 : Host()
270{
271 struct utsname u_name_data;
272 const int status = ::uname(&u_name_data);
273 if (status == 0) {
274 m_name = u_name_data.nodename;
275 m_os_name = u_name_data.sysname;
276 m_release = u_name_data.release;
277 m_version = u_name_data.version;
278 m_machine = u_name_data.machine;
279 } else {
280 char buffer[NI_MAXHOST];
281 const int host_status = gethostname(buffer, sizeof(buffer));
282 if (host_status == 0) {
283 m_name = buffer;
284 } else { // both uname and gethostname screwed
285 m_name.clear();
286 } // check for host_name
287 } // uname failed.
288} // LocalHost
289
291{
292 // delete s_instance;
293}
294
296
297const std::string&
299{
300 return m_os_name;
301} // os_name
302
304
305const std::string&
307{
308 return m_release;
309} // os_release
310
312
313const std::string&
315{
316 return m_version;
317} // os_version
318
320
321const std::string&
323{
324 return m_machine;
325} // machine
326
331
332const std::string&
334{
335 if (m_description.empty()) {
336 std::ostringstream stream;
337 stream << m_os_name << " " << m_release << "/" << m_machine;
338 m_description = stream.str();
339 }
340 return m_description;
341} // description
342
349
350const char*
352{
353 const std::string& str = OksSystem::LocalHost::full_local_name();
354 return str.c_str();
355} // full_host_name
356
362
363bool
365{
366 return true;
367}
368
374
375bool
377{
378 return false;
379}
380
381std::ostream&
382operator<<(std::ostream& stream, const OksSystem::Host& host)
383{
384 stream << host.full_name();
385 return stream;
386} // operator<<
void operator<<(TraceStreamer &x, const ers::Issue &r)
Definition Logger.hxx:128
Network host.
Definition Host.hpp:34
virtual ~Host()
destructor
Definition Host.cpp:131
std::string ip_string() const
get ip in string mode
Definition Host.cpp:198
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:165
static std::string expand(const std::string &name)
expands to full name
Definition Host.cpp:79
struct sockaddr_in ip() const
get ip address
Definition Host.cpp:175
static std::string to_string(struct sockaddr_in ip_addr)
ip to string conversion
Definition Host.cpp:94
bool equals(const Host &other) const
equality method
Definition Host.cpp:152
const std::string & full_name() const
get fully qualified name
Definition Host.cpp:185
std::string m_name
name of the host
Definition Host.hpp:36
Host()
constructor for current host
Definition Host.cpp:106
std::string m_full_name
cached fully qualified host name
Definition Host.hpp:37
Network host - localhost.
Definition Host.hpp:69
std::string m_os_name
name of the operating system
Definition Host.hpp:73
std::string m_version
version of the operating system
Definition Host.hpp:75
const std::string & description() const
machine type *‍/
Definition Host.cpp:333
static LocalHost * s_instance
singleton for local host
Definition Host.hpp:72
const std::string & os_release() const
release of the operating system
Definition Host.cpp:306
static const std::string & full_local_name()
fully qualified local host name
Definition Host.cpp:243
std::string m_machine
machine type
Definition Host.hpp:76
const std::string & os_name() const
name of the operating system
Definition Host.cpp:298
static const LocalHost * instance()
pointer to the singleton local host
Definition Host.cpp:255
const std::string & os_version() const
version of the operating system
Definition Host.cpp:314
static const std::string & local_name()
localhostname
Definition Host.cpp:232
std::string m_release
release of the operating system
Definition Host.hpp:74
const std::string & machine() const
version of the operating system
Definition Host.cpp:322
std::string m_description
machine description (cached)
Definition Host.hpp:77
bool operator!=(const Host &a, const Host &b)
inequality operator
Definition Host.cpp:211
const char * getfullhost()
get fully qualified host name
Definition Host.cpp:351
bool operator==(const Host &a, const Host &b)
equality operator
Definition Host.cpp:205