LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Host.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2015 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
33 #include <limits.h>
34 #include <string>
35 #include <string.h>
36 #include <netdb.h>
37 #include "boost/scoped_array.hpp"
38 #include <unistd.h>
39 
40 #include "lsst/ctrl/events/Host.h"
41 #include "lsst/pex/exceptions.h"
42 
43 namespace pexExceptions = lsst::pex::exceptions;
44 
45 namespace lsst {
46 namespace ctrl {
47 namespace events {
48 
49 Host const& Host::getHost() {
50  if (thisHost == 0) {
51 
52  // create the _IPAddr here, rather than
53  // reconstructing it every time we create an
54  // identificationId
55 
56  long int host_len = sysconf(_SC_HOST_NAME_MAX)+1; // add one for the null
57 
58  std::vector<char> vec;
59  vec.resize(host_len);
60 
61  struct hostent *ent;
62  unsigned char a,b,c,d;
63 
64  if (gethostname(vec.data(), vec.size()) == 0) {
65  _hostname = std::string(vec.data());
66  } else {
67  std::string msg("call to gethostname() failed");
68  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
69  }
70 
71  ent = (struct hostent *)gethostbyname(_hostname.c_str());
72  if (ent == NULL) {
73  std::string msg("call to gethostbyname() failed");
74  throw LSST_EXCEPT(pexExceptions::RuntimeError, msg);
75  }
76 
77  a = ent->h_addr_list[0][0] & 0xFF;
78  b = ent->h_addr_list[0][1] & 0xFF;
79  c = ent->h_addr_list[0][2] & 0xFF;
80  d = ent->h_addr_list[0][3] & 0xFF;
81 
82  _IPAddr = (a << 24) | (b << 16) | (c << 8) | d;
83 
84  // create the default EventSystem object
85  thisHost = new Host();
86  }
87  return *thisHost;
88 
89 }
90 
91 Host* Host::thisHost = 0;
92 unsigned int Host::_IPAddr = 0;
93 std::string Host::_hostname;
94 
95 unsigned int const Host::getIPAddress() {
96  return _IPAddr;
97 }
98 
99 std::string const& Host::getHostName() {
100  return _hostname;
101 }
102 
103 }}}
std::string const & getHostName()
get the host name
Definition: Host.cc:99
defines the Host class
Include files required for standard LSST Exception handling.
static Host const & getHost()
get the Host object, which can access the host and ip address.
Definition: Host.cc:49
This object represents the host that is being used to transmit events, giving quick access to often-u...
Definition: Host.h:48
static std::string _hostname
Definition: Host.h:72
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
afw::table::Key< double > b
static Host * thisHost
Definition: Host.h:70
static unsigned int _IPAddr
Definition: Host.h:73
unsigned int const getIPAddress()
get the IP address
Definition: Host.cc:95