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

Last change on this file since 10551 was 10513, checked in by tbretz, 14 years ago
Ensure Timestamp is correctly initialized.
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 // Make sure getTimestamp is called _before_ getTimestampMillisecs
50 getInfo()->getTimestamp();
51
52 if (getInfo()!=&fDimServers)
53 return;
54
55 // Get the received string from the handler
56 const string str = fDimServers.getString();
57
58 // Check if it starts with + or -
59 if (str[0]!='-' && str[0]!='+')
60 {
61 RemoveAllServers();
62 fServerList.clear();
63 }
64
65 // Create a stringstream to tokenize the received string
66 stringstream stream(str);
67
68 // Loop over the seperating tokens
69 string buffer;
70 while (getline(stream, buffer, '|'))
71 {
72 // The first part before the first @ is the server name
73 const string server = buffer.substr(0, buffer.find_first_of('@'));
74 if (server.empty())
75 continue;
76
77 // If it starts with a - we have to remove an entry
78 if (server[0]=='-')
79 {
80 const string trunc = server.substr(1);
81
82 // Check if this server is not found in the list.
83 // This should never happen if Dim works reliable
84 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
85 if (v==fServerList.end())
86 {
87 stringstream err;
88 err << "Server '" << trunc << "' not in list as it ought to be.";
89 throw runtime_error(err.str());
90 }
91
92 RemoveServer(trunc);
93 fServerList.erase(v);
94
95 continue;
96 }
97
98 // If it starts with a + we have to add an entry
99 if (server[0]=='+')
100 {
101 const string trunc = server.substr(1);
102
103 // Check if this server is already in the list.
104 // This should never happen if Dim works reliable
105 const ServerList::iterator v = find(fServerList.begin(), fServerList.end(), trunc);
106 if (v!=fServerList.end())
107 {
108 stringstream err;
109 err << "Server '" << trunc << "' in list not as it ought to be.";
110 throw runtime_error(err.str());
111 }
112
113 fServerList.push_back(trunc);
114 AddServer(trunc);
115
116 continue;
117 }
118
119 // In any other case we just add the entry to the list
120 fServerList.push_back(server);
121 AddServer(server);
122 }
123}
124
125// --------------------------------------------------------------------------
126//
127bool DimServerList::HasServer(const std::string &server) const
128{
129 return find(fServerList.begin(), fServerList.end(), server)!=fServerList.end();
130}
Note: See TracBrowser for help on using the repository browser.