Merge pull request #2216 from jwillemsen/jwi-cxxversionchecks
[ACE_TAO.git] / ACE / apps / drwho / Multicast_Manager.cpp
blob536a99140b5ecd8dcd60e58e363a672e4065281f
1 #include "Multicast_Manager.h"
2 #include "ace/Mem_Map.h"
3 #include "ace/Log_Msg.h"
4 #include "ace/OS_NS_string.h"
5 #include "ace/OS_NS_arpa_inet.h"
6 #include "ace/OS_NS_netdb.h"
7 #include "ace/OS_Memory.h"
8 #include "ace/OS_NS_ctype.h"
10 // Initialize all the static member vars.
11 int Multicast_Manager::received_host_count = 0;
12 Host_Elem *Multicast_Manager::drwho_list = 0;
13 Host_Elem *Multicast_Manager::current_ptr = 0;
15 // Names of hosts to query for friend info.
16 const char *Multicast_Manager::host_names[] =
18 "tango.cs.wustl.edu",
19 0 // The NULL entry...
22 void
23 Multicast_Manager::insert_default_hosts ()
25 // Enter the static list of hosts into the dynamic table!
27 for (const char **np = host_names;
28 *np != 0;
29 np++)
30 Multicast_Manager::add_host (*np);
33 // Inserts all the names in FILENAME into the list of hosts to
34 // contact.
36 int
37 Multicast_Manager::insert_hosts_from_file (const char *filename)
39 //FUZZ: disable check_for_lack_ACE_OS
40 ACE_Mem_Map mmap (filename);
41 //FUZZ: enable check_for_lack_ACE_OS
43 char *host_ptr = (char *) mmap.addr ();
45 if (host_ptr == 0)
46 return -1;
47 else
49 for (char *end_ptr = host_ptr + mmap.size ();
50 host_ptr < end_ptr;
53 Multicast_Manager::add_host (host_ptr);
55 while (*host_ptr != '\n')
56 host_ptr++;
58 *host_ptr++ = '\0';
61 return 0;
65 // Returns the IP host address for the next unexamined host in the
66 // list. If no more unexamined hosts remain a 0 is returned, else a
67 // 1.
69 int
70 Multicast_Manager::get_next_host_addr (in_addr &host_addr)
72 for (Multicast_Manager::current_ptr = Multicast_Manager::current_ptr == 0 ? Multicast_Manager::drwho_list : Multicast_Manager::current_ptr->next;
74 Multicast_Manager::current_ptr != 0;
75 Multicast_Manager::current_ptr = Multicast_Manager::current_ptr->next)
77 const char *host_name = Multicast_Manager::current_ptr->host_name;
78 hostent *hp = Multicast_Manager::get_host_entry (host_name);
80 if (hp == 0)
82 ACE_ERROR ((LM_ERROR,
83 "%s: host unknown.\n",
84 host_name));
85 continue;
88 Multicast_Manager::received_host_count++;
89 ACE_OS::memcpy (&host_addr,
90 hp->h_addr,
91 sizeof host_addr);
92 ACE_OS::memcpy (&Multicast_Manager::current_ptr->host_addr,
93 hp->h_addr,
94 sizeof host_addr);
95 return 1;
98 return 0;
101 // This function attempts to get the internet address for either a
102 // hostname or hostnumber. The function makes the simplifying
103 // assumption that hostnames begin with an alphabetic character!
105 hostent *
106 Multicast_Manager::get_host_entry (const char *host)
108 static hostent host_entry;
109 hostent *hp;
111 if (ACE_OS::ace_isdigit (*host)) // IP address.
113 u_long ia = ACE_OS::inet_addr (host);
115 if (ia == (u_long) -1)
116 hp = 0;
117 else
118 hp = ACE_OS::gethostbyaddr ((char *) &ia,
119 sizeof ia,
120 AF_INET);
122 else
123 // Host name.
124 hp = ACE_OS::gethostbyname (host);
127 return hp == 0 ? 0 : (hostent *) ACE_OS::memcpy (&host_entry, hp, sizeof *hp);
130 // Adds an additional new host to the list of host machines.
132 void
133 Multicast_Manager::add_host (const char *host_name)
135 ACE_NEW (Multicast_Manager::drwho_list,
136 Host_Elem (host_name,
137 Multicast_Manager::drwho_list));
140 void
141 Multicast_Manager::checkoff_host (in_addr host_addr)
143 for (Host_Elem *tmp = Multicast_Manager::drwho_list;
144 tmp != 0;
145 tmp = tmp->next)
146 if (ACE_OS::memcmp (&tmp->host_addr.s_addr,
147 &host_addr.s_addr,
148 sizeof host_addr.s_addr) == 0)
150 tmp->checked_off = 1;
151 Multicast_Manager::received_host_count--;
152 return;
157 Multicast_Manager::get_next_non_responding_host (const char *&host_name)
159 for (Multicast_Manager::current_ptr = Multicast_Manager::current_ptr == 0 ? Multicast_Manager::drwho_list : Multicast_Manager::current_ptr->next;
160 Multicast_Manager::current_ptr != 0;
161 Multicast_Manager::current_ptr = Multicast_Manager::current_ptr->next)
162 if (Multicast_Manager::current_ptr->checked_off == 0)
164 host_name = Multicast_Manager::current_ptr->host_name;
165 return 1;
168 return 0;
171 Host_Elem::Host_Elem (const char *h_name,
172 Host_Elem *n)
173 : host_name (h_name),
174 checked_off (0),
175 next (n)
180 Multicast_Manager::outstanding_hosts_remain ()
182 return Multicast_Manager::received_host_count > 0;