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

Last change on this file since 18051 was 13836, checked in by tbretz, 13 years ago
Added some workaround to make these classes not derive from DimInfoHandler - this clashes with the new StateMachineDim -- this must be fixed, it is still asnychronous with the event loop\!
File size: 4.5 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@Bugs When a server silently disappears and reappears the service has
22not correctly been deleted from the list and an exception is thrown.
23It is not clear what a better handling here is. Maybe a server is
24first removed from the list before it gets re-added if already in the list?
25
26*/
27// **************************************************************************
28#include "DimServerList.h"
29
30#include <sstream>
31#include <algorithm>
32#include <stdexcept>
33
34using namespace std;
35
36
37
38// --------------------------------------------------------------------------
39//
40//! Constructs the DimServerList. Subscribes a DimInfo to
41//! DIS_DNS/SERVER_LIST.
42//
43DimServerList::DimServerList(DimServerListImp *list) : fList(list),
44 fDimServers("DIS_DNS/SERVER_LIST", const_cast<char*>(""), this)
45{
46}
47
48// --------------------------------------------------------------------------
49//
50//! Whenever the service with the server list is updated this functions
51//! changes the contents of the list and calls RemoveAllServers(),
52//! RemoveServer() and AddServer() where appropriate.
53//
54void DimServerList::infoHandler()
55{
56 if (getInfo()!=&fDimServers)
57 return;
58
59 // Get the received string from the handler
60 const string str = fDimServers.getString();
61
62 // Check if it starts with + or -
63 if (str[0]!='-' && str[0]!='+')
64 {
65 fList->RemoveAllServers();
66 fServerList.clear();
67 }
68
69 // Create a stringstream to tokenize the received string
70 stringstream stream(str);
71
72 // Loop over the seperating tokens
73 string buffer;
74 while (getline(stream, buffer, '|'))
75 {
76 // The first part before the first @ is the server name
77 const string server = buffer.substr(0, buffer.find_first_of('@'));
78 if (server.empty())
79 continue;
80
81 // If it starts with a - we have to remove an entry
82 if (server[0]=='-')
83 {
84 const string trunc = server.substr(1);
85
86 // Check if this server is not found in the list.
87 // This should never happen if Dim works reliable
88 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
89 if (v==fServerList.end())
90 {
91 //stringstream err;
92 //err << "DimServerList: Server '" << trunc << "' not in list as it ought to be.";
93 //throw runtime_error(err.str());
94 }
95
96 fList->RemoveServer(trunc);
97 fServerList.erase(v);
98
99 continue;
100 }
101
102 // If it starts with a + we have to add an entry
103 if (server[0]=='+')
104 {
105 const string trunc = server.substr(1);
106
107 // Check if this server is already in the list.
108 // This should never happen if Dim works reliable
109 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
110 if (v!=fServerList.end())
111 {
112 fList->RemoveServer(trunc);
113 fServerList.erase(v);
114
115 //stringstream err;
116 //err << "DimServerList: Server '" << trunc << "' in list not as it ought to be.";
117 //throw runtime_error(err.str());
118 }
119
120 fServerList.push_back(trunc);
121 fList->AddServer(trunc);
122
123 continue;
124 }
125
126 // In any other case we just add the entry to the list
127 fServerList.push_back(server);
128 fList->AddServer(server);
129 }
130}
131
132// --------------------------------------------------------------------------
133//
134//! @param server
135//! server-name to check for
136//!
137//! @returns
138//! whether the server with the given name is online or not
139//!
140bool DimServerList::HasServer(const std::string &server) const
141{
142 return find(fServerList.begin(), fServerList.end(), server)!=fServerList.end();
143}
Note: See TracBrowser for help on using the repository browser.