source: trunk/FACT++/src/DimNetwork.cc@ 12105

Last change on this file since 12105 was 11730, checked in by tbretz, 13 years ago
When removing something from a list we have to pass a copy instead of a reference otherwise the removal might remove the source of the reference.
File size: 4.0 KB
Line 
1#include "DimNetwork.h"
2
3StateClient::StateClient(const std::string &name, MessageImp &imp) :
4 MessageDimRX(name, imp), fState(-2),
5 fInfoState((name + "/STATE").c_str(), (void*)NULL, 0, this)
6{
7}
8
9// --------------------------------------------------------------------------
10//
11//! Extract the information about the state and its message. Store the
12//! state and redirect the message to fMsg.
13//
14void StateClient::infoHandler()
15{
16 DimInfo *curr = getInfo(); // get current DimInfo address
17 if (!curr)
18 return;
19
20 if (curr==&fInfoState)
21 {
22 const bool disconnected = fInfoState.getSize()==0;
23
24 // Make sure getTimestamp is called _before_ getTimestampMillisecs
25 const int tsec = fInfoState.getTimestamp();
26 const int tms = fInfoState.getTimestampMillisecs();
27
28 fState = disconnected ? -2 : fInfoState.getQuality();
29 fStateTime = Time(tsec, tms*1000);
30
31 const string name = fInfoState.getName();
32
33 fMsg.StateChanged(fStateTime, name.substr(0, name.length()-6),
34 disconnected ? "" : fInfoState.getString(), fState);
35
36 return;
37 }
38
39 MessageDimRX::infoHandler();
40}
41
42// ==========================================================================
43
44// --------------------------------------------------------------------------
45//
46//! Delete all StateClient objects from teh list and clear the list.
47//
48void DimNetwork::DeleteClientList()
49{
50 for (ClientList::iterator i=fClientList.begin();
51 i!=fClientList.end(); i++)
52 delete i->second;
53
54 fClientList.clear();
55}
56
57// --------------------------------------------------------------------------
58//
59//! Adds the StateClient for the given server. Don't forget to
60//! call this function if it is overwritten in a derived class.
61//!
62//! @param s
63//! server which should be added
64//!
65//! @throws
66//! a runtime_error is the server is already in the list
67//
68void DimNetwork::AddServer(const string &s)
69{
70 DimServiceInfoList::AddServer(s);
71 if (s=="DIM_DNS")
72 return;
73
74 // Check if this server is already in the list.
75 // This should never happen if Dim works reliable
76 const ClientList::iterator v = fClientList.find(s);
77 if (v!=fClientList.end())
78 {
79 stringstream err;
80 err << "Server '" << s << "' in list not as it ought to be.";
81 throw runtime_error(err.str());
82 }
83
84 // Add the new server to the server list
85 fClientList[s] = new StateClient(s, *this);
86}
87
88// --------------------------------------------------------------------------
89//
90//! Removes the StateClient for the given server. Don't forget to
91//! call this function if it is overwritten in a derived class.
92//!
93//! @param s
94//! server which should be removed
95//!
96//! @throws
97//! a runtime_error is the server to be removed is not in the list
98//
99void DimNetwork::RemoveServer(string s)
100{
101 DimServiceInfoList::RemoveServer(s);
102 if (s=="DIM_DNS")
103 return;
104
105 const ClientList::iterator v = fClientList.find(s);
106 if (v==fClientList.end())
107 {
108 stringstream err;
109 err << "Server '" << s << "' not in list as it ought to be.";
110 throw runtime_error(err.str());
111 }
112
113 // Remove the server from the server list
114 delete v->second;
115
116 fClientList.erase(v);
117}
118
119// --------------------------------------------------------------------------
120//
121//! RemovesAll StateClients. Don't forget to call this function if it
122//! is overwritten in a derived class.
123//!
124void DimNetwork::RemoveAllServers()
125{
126 DimServiceInfoList::RemoveAllServers();
127 DeleteClientList();
128}
129
130// --------------------------------------------------------------------------
131//
132//! @param server
133//! server for which the current state should be returned
134//!
135//! @returns
136//! the current state of the given server, -2 if the server was not found
137//!
138int DimNetwork::GetCurrentState(const string &server) const
139{
140 const ClientList::const_iterator v = fClientList.find(server);
141 return v==fClientList.end() ? -2 : v->second->GetState();
142}
Note: See TracBrowser for help on using the repository browser.