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
|
/******************************************************************************
(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.
******************************************************************************/
class LATServices
{
public:
static LATServices *Instance()
{
if (!instance)
return (instance = new LATServices());
else
return instance;
}
// Get the highest rated node for this service name.
bool get_highest(const std::string &service,
std::string &node,
unsigned char *macaddr,
int *interface);
bool get_node(const std::string &service,
const std::string &node,
unsigned char *macaddr, int *interface);
// Add/update a service.
bool add_service(const std::string &node, const std::string &service, const std::string &ident,
int rating, int interface, unsigned char *macaddr);
bool remove_node(const std::string &node);
bool list_services(bool verbose, std::ostringstream &output);
void purge() {servicelist.clear(); }
void expire_nodes();
bool list_dummy_nodes(bool verbose, std::ostringstream &output);
bool touch_dummy_node_respond_counter(const std::string &str_name);
private:
LATServices()
{}; // Private constructor to force singleton
static LATServices *instance; // Singleton instance
class serviceinfo
{
public:
serviceinfo() {}
serviceinfo(const std::string &node, const std::string &_ident,
const unsigned char *macaddr, int rating, int interface)
{
ident = _ident;
nodes[node] = nodeinfo(macaddr, rating, ident, interface);
}
void add_or_replace_node(const std::string &node, const std::string &_ident,
const unsigned char *macaddr, int rating,
int interface)
{
ident = _ident;
nodes.erase(node); // Don't care if this fails
nodes[node] = nodeinfo(macaddr, rating, ident, interface);
}
bool get_highest(std::string &node, unsigned char *macaddr, int *interface);
bool get_node(const std::string &node, unsigned char *macaddr, int *interface);
const std::string get_ident() { return ident; }
bool is_available();
bool remove_node(const std::string &node);
void list_service(std::ostringstream &output);
void expire_nodes(time_t);
void list_nodes(std::ostringstream &output);
bool touch_dummy_node_respond_counter(const std::string &str_name);
private:
class nodeinfo
{
public:
nodeinfo() {}
nodeinfo(const unsigned char *_macaddr, int _rating, std::string _ident, int _interface):
rating(_rating),
interface(_interface),
available(true),
slave_reachable(5) // if it doesn't respond five times...
{
memcpy(macaddr, _macaddr, 6);
ident = _ident;
updated = time(NULL);
}
bool has_expired(time_t current_time)
{
return ( (current_time - updated) > 60);
}
int get_rating() { return rating; }
int get_interface() { return interface; }
const unsigned char *get_macaddr() { return macaddr; }
bool is_available() { return available; }
void set_available(bool a) { available = a; }
std::string get_ident() { return ident; }
int touch_respond_counter()
{
if (slave_reachable > 0)
{
slave_reachable--;
}
debuglog(("touch_respond_counter() %d\n", slave_reachable));
return slave_reachable;
}
bool check_respond_counter() { return slave_reachable > 0; }
private:
unsigned char macaddr[6];
int rating;
int interface;
bool available;
std::string ident;
time_t updated;
// for slave nodes
int slave_reachable;
};// class LATServices::service::nodeinfo
std::map<std::string, nodeinfo, std::less<std::string> > nodes;
std::string ident;
};// class LATServices::serviceinfo
std::map<std::string, serviceinfo, std::less<std::string> > servicelist;
};
|