source: trunk/FACT++/src/DimServerList.cc@ 10727

Last change on this file since 10727 was 10657, checked in by tbretz, 13 years ago
Added or improved documentation.
File size: 4.0 KB
Line 
1// **************************************************************************
2/** @class DimServerList
3
4@brief Maintains a list of all servers based on DIS_DNS/SERVER_LIST
5
6This class describes to the service DIS_DNS/SERVER_LIST of the name server.
7Thus it always contains an up-to-date list of all servers connected
8to the dns server.
9
10Whenever a server is added or removed, or all servers are removed from the
11#list (the dns itself went offline), the follwoing virtual functions are
12called:
13
14- virtual void AddServer(const std::string &)
15- virtual void RemoveServer(const std::string &)
16- virtual void RemoveAllServers()
17
18If overwritten the implementations of the base class doesn't need to be
19called, because it's empty.
20
21*/
22// **************************************************************************
23#include "DimServerList.h"
24
25#include <sstream>
26#include <algorithm>
27#include <stdexcept>
28
29using namespace std;
30
31// --------------------------------------------------------------------------
32//
33//! Constructs the DimServerList. Subscribes a DimInfo to
34//! DIS_DNS/SERVER_LIST.
35//
36DimServerList::DimServerList() :
37 fDimServers("DIS_DNS/SERVER_LIST", const_cast<char*>(""), this)
38{
39}
40
41// --------------------------------------------------------------------------
42//
43//! Whenever the service with the server list is updated this functions
44//! changes the contents of the list and calls RemoveAllServers(),
45//! RemoveServer() and AddServer() where appropriate.
46//
47void DimServerList::infoHandler()
48{
49 if (getInfo()!=&fDimServers)
50 return;
51
52 // Get the received string from the handler
53 const string str = fDimServers.getString();
54
55 // Check if it starts with + or -
56 if (str[0]!='-' && str[0]!='+')
57 {
58 RemoveAllServers();
59 fServerList.clear();
60 }
61
62 // Create a stringstream to tokenize the received string
63 stringstream stream(str);
64
65 // Loop over the seperating tokens
66 string buffer;
67 while (getline(stream, buffer, '|'))
68 {
69 // The first part before the first @ is the server name
70 const string server = buffer.substr(0, buffer.find_first_of('@'));
71 if (server.empty())
72 continue;
73
74 // If it starts with a - we have to remove an entry
75 if (server[0]=='-')
76 {
77 const string trunc = server.substr(1);
78
79 // Check if this server is not found in the list.
80 // This should never happen if Dim works reliable
81 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
82 if (v==fServerList.end())
83 {
84 stringstream err;
85 err << "Server '" << trunc << "' not in list as it ought to be.";
86 throw runtime_error(err.str());
87 }
88
89 RemoveServer(trunc);
90 fServerList.erase(v);
91
92 continue;
93 }
94
95 // If it starts with a + we have to add an entry
96 if (server[0]=='+')
97 {
98 const string trunc = server.substr(1);
99
100 // Check if this server is already in the list.
101 // This should never happen if Dim works reliable
102 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
103 if (v!=fServerList.end())
104 {
105 stringstream err;
106 err << "Server '" << trunc << "' in list not as it ought to be.";
107 throw runtime_error(err.str());
108 }
109
110 fServerList.push_back(trunc);
111 AddServer(trunc);
112
113 continue;
114 }
115
116 // In any other case we just add the entry to the list
117 fServerList.push_back(server);
118 AddServer(server);
119 }
120}
121
122// --------------------------------------------------------------------------
123//
124//! @param server
125//! server-name to check for
126//!
127//! @returns
128//! whether the server with the given name is online or not
129//!
130bool DimServerList::HasServer(const std::string &server) const
131{
132 return find(fServerList.begin(), fServerList.end(), server)!=fServerList.end();
133}
Note: See TracBrowser for help on using the repository browser.