File: services.cc

package info (click to toggle)
latd 1.30
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,352 kB
  • ctags: 1,046
  • sloc: cpp: 8,489; sh: 878; makefile: 85
file content (357 lines) | stat: -rw-r--r-- 11,469 bytes parent folder | download | duplicates (4)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/******************************************************************************
    (c) 2000-2004 Christine Caulfield                 christine.caulfield@googlemail.com
    (c) 2003 Dmitri Popov

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
******************************************************************************/

#include <sys/types.h>
#include <stdio.h>
#include <time.h>
#include <netinet/in.h>
#include <string.h>
#include <list>
#include <string>
#include <map>
#include <iterator>
#include <sstream>
#include <iomanip>

#include "lat.h"
#include "utils.h"
#include "services.h"
#include "dn_endian.h"

// Add or replace a node in the service table
bool LATServices::add_service(const std::string &node, const std::string &service, const std::string &ident,
			      int rating, int interface, unsigned char *macaddr)
{
    debuglog(("Got service. Node: %s, service %s, rating: %d\n",
	     node.c_str(), service.c_str(), rating));

    std::map<std::string, serviceinfo, std::less<std::string> >::iterator test = servicelist.find(service);
    if (test == servicelist.end())
    {
	servicelist[service] = serviceinfo(node, ident, macaddr, rating, interface);
    }
    else
    {
	servicelist[test->first].add_or_replace_node(node, ident, macaddr, rating, interface);
    }
    return true;
}

// Return the highest rated node providing a named service
bool LATServices::get_highest(const std::string &service, std::string &node, unsigned char *macaddr,
			      int *interface)
{
  std::map<std::string, serviceinfo, std::less<std::string> >::iterator test = servicelist.find(service);
  if (test != servicelist.end())
  {
      return servicelist[test->first].get_highest(node, macaddr, interface);
  }
  return false; // Not found
}

// Return the highest rated node providing this service
bool LATServices::serviceinfo::get_highest(std::string &node, unsigned char *macaddr, int *interface)
{
  int                  highest_rating=0;
  std::string          highest_node;
  const unsigned char *highest_macaddr = NULL;

  std::map<std::string, nodeinfo, std::less<std::string> >::iterator i;
  for (i=nodes.begin(); i != nodes.end(); i++)
  {
      if (nodes[i->first].is_available() &&
	  nodes[i->first].get_rating() > highest_rating)
      {
	  highest_rating  = nodes[i->first].get_rating();
	  highest_node    = i->first; // Just note the pointer
	  highest_macaddr = nodes[i->first].get_macaddr();
	  *interface = nodes[i->first].get_interface();
      }
  }

  if (highest_macaddr)
  {
      // OK copy it now.
      memcpy(macaddr, highest_macaddr, 6);
      node = highest_node;
      return true;
  }
  else
  {
      return false;
  }
}


// Return the node macaddress if the node provides this service
bool LATServices::get_node(const std::string &service, const std::string &node,
			   unsigned char *macaddr, int *interface)
{
  std::map<std::string, serviceinfo, std::less<std::string> >::iterator test = servicelist.find(service);
    std::map<std::string, serviceinfo, std::less<std::string> >::iterator c =
servicelist.begin();
    int i =0;
     for(;c != servicelist.end();c++) {
       i++;
debuglog(("LATServices::get_node : service %2d '%s' \n",
  i, c->first.c_str()));
}

  if (test != servicelist.end())
  {
//      std::map<std::string, serviceinfo, std::less<std::string> >::iterator c = test;
//	  int i =0;
//	   for(;c != servicelist.end();c++) { i++; };
debuglog(("LATServices::get_node : service found '%s', number: %d \n", service.c_str(), i));
      return servicelist[test->first].get_node(node, macaddr, interface);
  }
debuglog(("LATServices::get_node : no service '%s' \n", service.c_str()));

  return false; // Not found
}


// Return the node's macaddress
bool LATServices::serviceinfo::get_node(const std::string &node, unsigned char *macaddr, int *interface)
{
  std::map<std::string, nodeinfo, std::less<std::string> >::iterator test = nodes.find(node);

   std::map<std::string, nodeinfo, std::less<std::string> >::iterator c =
nodes.begin();
    int i =0;
     for(;c != nodes.end();c++) {
       i++;
debuglog(("LATServices::serinfo::get_node :node %2d '%s' \n",
  i, c->first.c_str()));
}

debuglog(("LATServices::serinfo::get_node looking for '%s' \n",
  node.c_str()));


  if (test != nodes.end())
  {
      memcpy(macaddr, nodes[node].get_macaddr(), 6);
      *interface = nodes[node].get_interface();
      return true;
  }
  else
  {
debuglog(("LATServices::serviceinfo::get_node : no node '%s' \n", node.c_str()));
      return false;
  }
}


// Remove node from all services...
// Actually just mark as unavailable in all services
bool LATServices::remove_node(const std::string &node)
{
    bool removed = false;
    std::map<std::string, serviceinfo, std::less<std::string> >::iterator s(servicelist.begin());

    for (; s != servicelist.end(); s++)
    {
	if (servicelist[s->first].remove_node(node))
	    removed=true;
    }
    return removed;
}


bool LATServices::serviceinfo::remove_node(const std::string &node)
{
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator test = nodes.find(node);
    if (test != nodes.end())
    {
	nodes[test->first].set_available(false);
    }
    return false;
}

// Return true if the service is available.
// This is the case if one node offering that service is available
bool LATServices::serviceinfo::is_available()
{
    bool avail = true;
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator n(nodes.begin());

    for (; n != nodes.end(); n++)
    {
	if (!nodes[n->first].is_available()) avail=false;
    }
    return avail;
}


void LATServices::serviceinfo::expire_nodes(time_t current_time)
{
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator n(nodes.begin());

    for (; n != nodes.end(); n++)
    {
	if (nodes[n->first].has_expired(current_time))
	    nodes[n->first].set_available(false);
    }
}

void LATServices::expire_nodes()
{
    std::map<std::string, serviceinfo, std::less<std::string> >::iterator s(servicelist.begin());
    time_t current_time = time(NULL);

    for (; s != servicelist.end(); s++)
    {
	// Skip dummy service
	if (s->first != "")
	{
	    servicelist[s->first].expire_nodes(current_time);
	}
    }
}


// Verbose listing of nodes in this service
void LATServices::serviceinfo::list_service(std::ostringstream &output)
{
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator n(nodes.begin());

    output << "Node Name        Status      Rating   Identification" << std::endl;
    for (; n != nodes.end(); n++)
    {
        output.width(17);
        output.setf(std::ios::left, std::ios::adjustfield);
	output << n->first.c_str() <<
	         (nodes[n->first].is_available()?"Reachable  ":"Unreachable") << "   ";
        output.width(4);
        output.setf(std::ios::right, std::ios::adjustfield);
	output << nodes[n->first].get_rating() << "   " <<
 	          nodes[n->first].get_ident() <<  std::endl;
    }
}

void LATServices::serviceinfo::list_nodes(std::ostringstream &output)
{
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator n(nodes.begin());
    const unsigned char *addr = NULL;

    output << "Node Name        Status       Address            Identification" << std::endl;
    for (; n != nodes.end(); n++)
    {
        addr = n->second.get_macaddr();
        output.width(17);
        output.setf(std::ios::left, std::ios::adjustfield);
        output << n->first.c_str() <<
            (n->second.check_respond_counter()?"Reachable  ":"Unreachable") << "  ";

        output.setf(std::ios::hex, std::ios::basefield);

        output << setiosflags(std::ios::right | std::ios::uppercase) << std::setfill('0')
           << std::setw(2) << (int)addr[0] << '-'
           << std::setw(2) << (int)addr[1] << '-'
           << std::setw(2) << (int)addr[2] << '-'
           << std::setw(2) << (int)addr[3] << '-'
           << std::setw(2) << (int)addr[4] << '-'
           << std::setw(2) << (int)addr[5]
           << resetiosflags(std::ios::right | std::ios::uppercase) << std::setfill(' ');

        output.setf(std::ios::right, std::ios::adjustfield);
        output << "  " << n->second.get_ident() <<  std::endl;
    }
}

bool LATServices::list_dummy_nodes(bool verbose, std::ostringstream &output)
{
    std::map<std::string, serviceinfo, std::less<std::string> >::iterator dummies =
             servicelist.find("");

    if ( dummies == servicelist.end()) {
        output << "No dummy nodes available." << std::endl;
        return true;
    }

    output << std::endl;
    output << "Service Name:    " << "Slave nodes" << std::endl;
    output << "Service Status:  " << (servicelist[dummies->first].is_available()?"Available ":"Unavailable") << "   " << std::endl;
    output << "Service Ident:   " << servicelist[dummies->first].get_ident() << std::endl << std::endl;
    dummies->second.list_nodes(output);
    output << "--------------------------------------------------------------------------------" << std::endl;


    return true;
}

bool LATServices::touch_dummy_node_respond_counter(const std::string &str_name)
{
    std::map<std::string, serviceinfo, std::less<std::string> >::iterator dummies =
             servicelist.find("");

    if ( dummies == servicelist.end()) {
        return false; // no node
    }

    return dummies->second.touch_dummy_node_respond_counter(str_name);
}

bool LATServices::serviceinfo::touch_dummy_node_respond_counter(const std::string &str_name)
{
    std::map<std::string, nodeinfo, std::less<std::string> >::iterator n = nodes.find(str_name);

    if (n == nodes.end()) {
        return false; // no node
    }

    debuglog(("touch_respond() : node: %s ", n->first.c_str()));
    n->second.touch_respond_counter();
    return true;
}


// List all known services
bool LATServices::list_services(bool verbose, std::ostringstream &output)
{
  std::map<std::string, serviceinfo, std::less<std::string> >::iterator s(servicelist.begin());

  for (; s != servicelist.end(); s++)
  {
      // Skip dummy service
      if (s->first != "")
      {
	  if (verbose)
	  {
	      output << std::endl;
	      output << "Service Name:    " << s->first << std::endl;
	      output << "Service Status:  " << (servicelist[s->first].is_available()?"Available ":"Unavailable") << "   " << std::endl;
	      output << "Service Ident:   " << servicelist[s->first].get_ident() << std::endl << std::endl;
	      servicelist[s->first].list_service(output);
	      output << "--------------------------------------------------------------------------------" << std::endl;

	  }
	  else
	  {
              output.width(28);
              output.setf(std::ios::left, std::ios::adjustfield);
	      output << s->first.c_str() << (servicelist[s->first].is_available()?"Available ":"Unavailable") << "   " <<
                        servicelist[s->first].get_ident() << std::endl;
	  }
      }

  }

  output << std::ends; // Trailing NUL for latcp's benefit.
  return true;
}

LATServices *LATServices::instance = NULL;