Host.cpp

Go to the documentation of this file.
00001 
00014 // Host.cpp -- implementation of the Host class
00015 
00016 #include "Host.h"
00017 #include "NetworkEx.h"
00018 
00019 
00021 // class Host
00022 
00023 
00024 // static variable for local host
00025 std::auto_ptr<Host> Host::ipLocalhost;
00026 
00027 // standard c++ iostream insertor
00028 std::ostream& Host::operator<<( std::ostream& stream ) const
00029 {
00030         stream << "Host( ";
00031         if ( isDefined() ) {
00032                 stream << getName().c_str() << ",";
00033 
00034                 for ( std::vector<IpAddress>::const_iterator i = ipAddresses.begin(); i != ipAddresses.end(); ++i ) {
00035                         stream << " ";
00036                         (*i).operator<<(stream);
00037                 }
00038         }
00039         else {
00040                 stream << "network host is not defined";
00041     }
00042 
00043         stream << " )";
00044         return stream;
00045 }
00046 
00047 // Construct myself to be the host with name `name`.
00048 Host::Host( const std::string& name )
00049 {
00050         set(name);
00051 }
00052 
00053 // Construct myself to be the host with IP address `address`.
00054 Host::Host( const IpAddress& address )
00055 {
00056         set(address);
00057 }
00058 
00059 // Collection of IpAddresses for the local host
00060 const std::vector<IpAddress>& Host::getIpAddresses() const
00061 {
00062         return ipAddresses;
00063 }
00064 
00065 // Comparsion of host names
00066 bool Host::operator==( const Host& host ) const
00067 {
00068         return getName() == host.getName();
00069 }
00070 
00071 // Comparsion of host names
00072 bool Host::operator<( const Host& host ) const
00073 {
00074         return getName() < host.getName();
00075 }
00076 
00077 // Default constructor
00078 Host::Host() : hostName(), ipAliases(), ipAddresses()
00079 {
00080 }
00081 
00082 // Use the host
00083 Host::Host( const hostent& host )
00084 {
00085         set(host);
00086 }
00087 
00088 // Copy constructor
00089 Host::Host( const Host& host ) : hostName( host.hostName ), ipAliases( host.ipAliases ), ipAddresses( host.ipAddresses )
00090 {
00091 }
00092 
00093 // Verfication that the host has a name
00094 bool Host::isDefined() const
00095 {
00096         return !(getName().empty());
00097 }
00098 
00099 
00100 // Return a collection host aliases
00101 const std::vector<std::string>& Host::getAliases() const
00102 {
00103         return ipAliases;
00104 }
00105 
00106 // Accessor for name
00107 std::string Host::getName() const
00108 {
00109         return hostName;
00110 }
00111 
00112 // Assignment operator
00113 Host& Host::operator=(const Host& host)
00114 {
00115         if ( this != &host ) {
00116                 hostName = host.hostName;
00117                 ipAliases = host.ipAliases;
00118                 ipAddresses = host.ipAddresses;
00119         }
00120 
00121         return *this;
00122 }
00123 
00124 // Static function to retrieve the localhost
00125 const Host& Host::getLocalhost()
00126 {
00127         if (ipLocalhost.get() == 0 ) {
00128                 try {
00129                         char name[ 255 ];
00130                         ::gethostname(name,sizeof(name));
00131                         ipLocalhost = std::auto_ptr<Host>(new Host(name));
00132                 }
00133                 catch ( std::exception& ) { //os_network_toolkit_error& ) {
00134                         ipLocalhost = std::auto_ptr<Host>(new Host());
00135                         ipLocalhost->hostName = "Could not find ip address for the localhost";
00136                 }
00137         }
00138 
00139         return *(ipLocalhost.get());
00140 }
00141 
00142 
00143 // Accessor for setting host name
00144 void Host::set( const std::string& name )
00145 {
00146         struct hostent entry;
00147         if ( Host::find( name, &entry) )
00148                 set( entry );
00149         else
00150                 NetworkException::throwNetworkException(NetworkException::invalid_host,name.c_str());
00151 }
00152 
00153 // Accessor for setting host name
00154 void Host::set( const IpAddress& address )
00155 {
00156         struct hostent entry;
00157         if ( Host::find( address, &entry) )
00158                 set( entry );
00159         else {
00160                 std::stringstream stream;
00161                 address.operator <<(stream);
00162                 NetworkException::throwNetworkException(NetworkException::invalid_host,stream.str().c_str());
00163         }
00164 }
00165 
00166 // Accessor for setting host name
00167 void Host::set( const hostent& host )
00168 {
00169         hostName = host.h_name;
00170         char** ptr = host.h_aliases;
00171 
00172         while (*ptr)
00173                 ipAliases.push_back(std::string( *(ptr++) ));
00174  
00175         ptr = host.h_addr_list;
00176         while (*ptr)
00177                 ipAddresses.push_back(IpAddress( *(in_addr*) *(ptr++) ));
00178 }
00179 
00180 // Gather information on the named host
00181 bool Host::find(const std::string& name,struct hostent* data) throw()
00182 {
00183         struct hostent* result;
00184         if ( (result = ::gethostbyname( name.c_str() )) != 0 )
00185                 *data = *result;
00186         return result != 0;
00187 }
00188 
00189 // Gather host information for the given IpAddress
00190 bool Host::find(const IpAddress& addr,struct hostent* data) throw()
00191 {
00192         struct hostent* result;
00193     if((result = ::gethostbyaddr(reinterpret_cast<const char*>(&(in_addr)addr),sizeof(in_addr),AF_INET) ) != 0)
00194                 *data = *result;
00195         return result != 0;
00196 }

Generated on Tue Mar 28 09:10:14 2006 for Simple SOAP by  doxygen 1.4.6