summaryrefslogtreecommitdiff
path: root/apps/drwho/Multicast_Manager.cpp
blob: c3385b633d3b05b4c610ffadefafe2bd26a683ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// $Id$

#include "Multicast_Manager.h"
#include "ace/Mem_Map.h"

// Initialize all the static member vars.
int Multicast_Manager::received_host_count = 0;
Host_Elem *Multicast_Manager::drwho_list = 0;
Host_Elem *Multicast_Manager::current_ptr = 0;

// Names of hosts to query for friend info. 
char *Multicast_Manager::host_names[] =
{
  "tango.cs.wustl.edu",
  0 // The NULL entry... 
};

void
Multicast_Manager::insert_default_hosts (void)
{
  // Enter the static list of hosts into the dynamic table!

  for (char **np = host_names;
       *np != 0;
       np++)
    Multicast_Manager::add_host (*np);
}

// Inserts all the names in FILENAME into the list of hosts to
// contact.

int    
Multicast_Manager::insert_hosts_from_file (const char *filename)
{
  ACE_Mem_Map mmap (filename);
  char *host_ptr = (char *) mmap.addr ();

  if (host_ptr == 0)
    return -1;
  else
    {
      for (char *end_ptr = host_ptr + mmap.size ();
           host_ptr < end_ptr;
           )
	{
	  Multicast_Manager::add_host (host_ptr);

	  while (*host_ptr != '\n')
	    host_ptr++;

	  *host_ptr++ = '\0';
	}

      return 0;
    }
}

// Returns the IP host address for the next unexamined host in the
// list.  If no more unexamined hosts remain a 0 is returned, else a
// 1.

int	
Multicast_Manager::get_next_host_addr (in_addr &host_addr)
{
  for (Multicast_Manager::current_ptr = Multicast_Manager::current_ptr == 0 ? Multicast_Manager::drwho_list : Multicast_Manager::current_ptr->next;
       
       Multicast_Manager::current_ptr != 0;
       Multicast_Manager::current_ptr = Multicast_Manager::current_ptr->next)
    {
      const char *host_name = Multicast_Manager::current_ptr->host_name;
      hostent *hp = Multicast_Manager::get_host_entry (host_name);

      if (hp == 0)
        {
          ACE_ERROR ((LM_ERROR, 
                      "%s: host unknown.\n",
                      host_name));
          continue;
        }

      Multicast_Manager::received_host_count++;
      ACE_OS::memcpy (&host_addr,
                      hp->h_addr,
                      sizeof host_addr);
      ACE_OS::memcpy (&Multicast_Manager::current_ptr->host_addr,
                      hp->h_addr,
                      sizeof host_addr);
      return 1;
    }

  return 0;
}

// This function attempts to get the internet address for either a
// hostname or hostnumber.  The function makes the simplifying
// assumption that hostnames begin with an alphabetic character!

hostent *
Multicast_Manager::get_host_entry (const char *host)
{
  static hostent host_entry;
  hostent *hp;
  
  if (isdigit (*host)) // IP address.
    { 
      u_long ia = ACE_OS::inet_addr (host);

      if (ia == (u_long) -1)
        hp = 0;
      else
        hp = ACE_OS::gethostbyaddr ((char *) &ia,
                                    sizeof ia,
                                    AF_INET);
    } 
  else 
    // Host name. 
    hp = ACE_OS::gethostbyname (host);


  return hp == 0 ? 0 : (hostent *) memcpy (&host_entry, hp, sizeof *hp);
}

// Adds an additional new host to the list of host machines. 

void 
Multicast_Manager::add_host (const char *host_name)
{
  ACE_NEW (Multicast_Manager::drwho_list,
           Host_Elem (host_name,
                      Multicast_Manager::drwho_list));
}

void 
Multicast_Manager::checkoff_host (in_addr host_addr)
{
  for (Host_Elem *tmp = Multicast_Manager::drwho_list;
       tmp != 0;
       tmp = tmp->next)
    if (ACE_OS::memcmp (&tmp->host_addr.s_addr,
                        &host_addr.s_addr,
                        sizeof host_addr.s_addr) == 0)
      {
	tmp->checked_off = 1;
	Multicast_Manager::received_host_count--;
	return;
      }
}

int 
Multicast_Manager::get_next_non_responding_host (const char *&host_name)
{
  for (Multicast_Manager::current_ptr = Multicast_Manager::current_ptr == 0 ? Multicast_Manager::drwho_list : Multicast_Manager::current_ptr->next;
       Multicast_Manager::current_ptr != 0;
       Multicast_Manager::current_ptr = Multicast_Manager::current_ptr->next)
    if (Multicast_Manager::current_ptr->checked_off == 0)
      {
	host_name = Multicast_Manager::current_ptr->host_name;
	return 1;
      }
  
  return 0;
}

Host_Elem::Host_Elem (const char *h_name,
                      Host_Elem *n)
  : host_name (h_name),
    checked_off (0),
    next (n)
{
}

int 
Multicast_Manager::outstanding_hosts_remain (void)
{
  return Multicast_Manager::received_host_count > 0;
}