ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/HostUpdate/Host.cpp
Revision: 900
Committed: 2007-04-29T02:26:40-07:00 (18 years, 1 month ago) by douglas
File size: 2575 byte(s)
Log Message:
Woo!

File Contents

# User Rev Content
1 Douglas Thrift 11 // Host Update
2     //
3     // Douglas Thrift
4     //
5     // $Id$
6    
7     #include "Host.hpp"
8    
9 douglas 900 Host::Host(const std::string& host, const std::string& name, const std::string& address, const std::string& platform)
10 Douglas Thrift 11 {
11 douglas 900 ::memset(&this->address, 0, sizeof(in_addr));
12 Douglas Thrift 22
13 Douglas Thrift 11 #ifdef _WIN32
14     if (count == 0)
15 douglas 900 if (::WSAStartup(MAKEWORD(2, 0), &data) != 0)
16 Douglas Thrift 11 {
17 douglas 900 std::cerr << "Host(): WSAStartup()\n";
18    
19     ::exit(1);
20 Douglas Thrift 11 }
21    
22     count++;
23     #endif
24    
25     setHost(host);
26 douglas 900
27     if (name != "")
28     setName(name, address == "");
29    
30     if (address != "")
31     setAddress(address, name == "");
32    
33 Douglas Thrift 12 setPlatform(platform);
34 Douglas Thrift 11 }
35    
36     Host::~Host()
37     {
38     #ifdef _WIN32
39     count--;
40    
41     if (count == 0)
42 douglas 900 if (::WSACleanup() != 0)
43 Douglas Thrift 11 {
44 douglas 900 std::cerr << "~Host(): WSACleanup()\n";
45    
46     ::exit(1);
47 Douglas Thrift 11 }
48     #endif
49     }
50    
51 douglas 900 void Host::setName(const std::string& name, bool lookup)
52 Douglas Thrift 11 {
53     this->name = name;
54    
55     if (lookup)
56     {
57 douglas 900 struct ::hostent* ent(::gethostbyname(this->name.c_str()));
58 Douglas Thrift 11
59 douglas 900 ::memcpy(&address, *(ent->h_addr_list), ent->h_length);
60 Douglas Thrift 11 }
61     }
62    
63 douglas 900 void Host::setAddress(const std::string& address, bool lookup)
64 Douglas Thrift 11 {
65 douglas 900 ::in_addr_t value = ::inet_addr(address.c_str());
66 Douglas Thrift 11
67     if (value == INADDR_NONE)
68 douglas 900 std::cerr << "Host.setAddress(): INADDR_NONE\n";
69 Douglas Thrift 11 else
70 douglas 900 ::memcpy(&this->address, &value, sizeof(::in_addr_t));
71 Douglas Thrift 11
72     if (lookup)
73     {
74 douglas 900 struct ::hostent* ent(::gethostbyaddr((char*)(&this->address), sizeof(in_addr), AF_INET));
75 Douglas Thrift 11
76 douglas 900 if (ent != NULL)
77     name = ent->h_name;
78 Douglas Thrift 11 }
79     }
80    
81 Douglas Thrift 13 bool Host::operator==(const Host& host) const
82 Douglas Thrift 11 {
83 Douglas Thrift 13 if (this->host == host.host)
84     if (name == host.name)
85 douglas 900 if (std::string(::inet_ntoa(address)) == ::inet_ntoa(host.address))
86 Douglas Thrift 13 return platform == host.platform;
87     else
88     return false;
89     else
90     return false;
91     else
92     return false;
93     }
94    
95     Host Host::operator++()
96     {
97 douglas 900 std::string file(std::string("hosts") + slash + host);
98     std::ifstream fin(file.c_str());
99 Douglas Thrift 15
100 Douglas Thrift 13 if (fin.is_open())
101     {
102 douglas 900 std::getline(fin, name);
103 Douglas Thrift 15
104 douglas 900 std::string address;
105 Douglas Thrift 19
106 douglas 900 std::getline(fin, address);
107 Douglas Thrift 15
108 douglas 900 ::in_addr_t value(::inet_addr(address.c_str()));
109 Douglas Thrift 15
110     if (value == INADDR_NONE)
111 douglas 900 std::cerr << "Host.operator++(): INADDR_NONE\n";
112 Douglas Thrift 15 else
113 douglas 900 ::memcpy(&this->address, &value, sizeof(in_addr_t));
114 Douglas Thrift 15
115 douglas 900 std::getline(fin, platform);
116 Douglas Thrift 13 fin.close();
117     }
118    
119     return *this;
120 Douglas Thrift 11 }
121    
122 Douglas Thrift 13 Host Host::operator++(int)
123 Douglas Thrift 11 {
124 Douglas Thrift 13 Host old = *this;
125    
126     ++*this;
127    
128     return old;
129     }
130    
131     Host Host::operator--()
132     {
133 douglas 900 std::string file = std::string("hosts") + slash + host;
134 Douglas Thrift 19
135 douglas 900 std::ofstream fout(file.c_str());
136 Douglas Thrift 11
137 douglas 900 fout << name << '\n' << ::inet_ntoa(address) << '\n' << platform << '\n';
138 Douglas Thrift 11
139     fout.close();
140 Douglas Thrift 13
141     return *this;
142 Douglas Thrift 11 }
143    
144 Douglas Thrift 13 Host Host::operator--(int)
145     {
146     Host old = *this;
147    
148     --*this;
149    
150     return old;
151     }
152    
153 Douglas Thrift 11 #ifdef _WIN32
154    
155 Douglas Thrift 192 unsigned Host::count(0);
156 Douglas Thrift 11 WSADATA Host::data;
157    
158     #endif

Properties

Name Value
svn:eol-style native
svn:keywords Id