FACT++  1.0
DimServerList.cc
Go to the documentation of this file.
1 // **************************************************************************
27 // **************************************************************************
28 #include "DimServerList.h"
29 
30 #include <sstream>
31 #include <algorithm>
32 #include <stdexcept>
33 
34 using namespace std;
35 
36 
37 
38 // --------------------------------------------------------------------------
39 //
42 //
44  fDimServers("DIS_DNS/SERVER_LIST", const_cast<char*>(""), this)
45 {
46 }
47 
48 // --------------------------------------------------------------------------
49 //
53 //
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  {
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 //
140 bool DimServerList::HasServer(const std::string &server) const
141 {
142  return find(fServerList.begin(), fServerList.end(), server)!=fServerList.end();
143 }
char * getString()
Definition: dic.hxx:108
virtual void RemoveAllServers()
Definition: DimServerList.h:45
char str[80]
Definition: test_client.c:7
STL namespace.
virtual void AddServer(const std::string &)
Definition: DimServerList.h:43
DimServerListImp * fList
Definition: DimServerList.h:17
bool HasServer(const std::string &server) const
virtual void RemoveServer(std::string)
Definition: DimServerList.h:44
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
void infoHandler()
A DimInfo to retrieve the SERVER_LIST from teh DNS server.
DimInfo * getInfo()
Definition: dic.hxx:26
DimInfo fDimServers
A list with the available servers.
Definition: DimServerList.h:19
DimServerList(DimServerListImp *list)
ServerList fServerList
Definition: DimServerList.h:18