1 | #include "DimNetwork.h"
|
---|
2 |
|
---|
3 | StateClient::StateClient(const std::string &name, MessageImp &imp) :
|
---|
4 | MessageDimRX(name, imp), fState(-2),
|
---|
5 | fInfoState((name + "/STATE").c_str(), const_cast<char*>(""), 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 | //
|
---|
14 | void StateClient::infoHandler()
|
---|
15 | {
|
---|
16 | DimInfo *curr = getInfo(); // get current DimInfo address
|
---|
17 | if (!curr)
|
---|
18 | return;
|
---|
19 |
|
---|
20 | if (curr==&fInfoState)
|
---|
21 | {
|
---|
22 | const char *txt = fInfoState.getString();
|
---|
23 |
|
---|
24 | fState = strlen(txt)==0 ? -2 : fInfoState.getQuality();
|
---|
25 | fStateTime = Time(fInfoState.getTimestamp(),
|
---|
26 | fInfoState.getTimestampMillisecs());
|
---|
27 |
|
---|
28 | const string name = fInfoState.getName();
|
---|
29 |
|
---|
30 | fMsg.StateChanged(fStateTime, name.substr(0, name.length()-6), txt, fState);
|
---|
31 |
|
---|
32 | return;
|
---|
33 | }
|
---|
34 |
|
---|
35 | MessageDimRX::infoHandler();
|
---|
36 | }
|
---|
37 |
|
---|
38 | // ==========================================================================
|
---|
39 |
|
---|
40 | // --------------------------------------------------------------------------
|
---|
41 | //
|
---|
42 | //! Delete all StateClient objects from teh list and clear the list.
|
---|
43 | //
|
---|
44 | void DimNetwork::DeleteClientList()
|
---|
45 | {
|
---|
46 | for (ClientList::iterator i=fClientList.begin();
|
---|
47 | i!=fClientList.end(); i++)
|
---|
48 | delete i->second;
|
---|
49 |
|
---|
50 | fClientList.clear();
|
---|
51 | }
|
---|
52 |
|
---|
53 | // --------------------------------------------------------------------------
|
---|
54 | //
|
---|
55 | //! Adds the StateClient for the given server. Don't forget to
|
---|
56 | //! call this function if it is overwritten in a derived class.
|
---|
57 | //!
|
---|
58 | //! @param s
|
---|
59 | //! server which should be added
|
---|
60 | //!
|
---|
61 | //! @throws
|
---|
62 | //! a runtime_error is the server is already in the list
|
---|
63 | //
|
---|
64 | void DimNetwork::AddServer(const string &s)
|
---|
65 | {
|
---|
66 | DimServiceInfoList::AddServer(s);
|
---|
67 | if (s=="DIM_DNS")
|
---|
68 | return;
|
---|
69 |
|
---|
70 | // Check if this server is already in the list.
|
---|
71 | // This should never happen if Dim works reliable
|
---|
72 | const ClientList::iterator v = fClientList.find(s);
|
---|
73 | if (v!=fClientList.end())
|
---|
74 | {
|
---|
75 | stringstream err;
|
---|
76 | err << "Server '" << s << "' in list not as it ought to be.";
|
---|
77 | throw runtime_error(err.str());
|
---|
78 | }
|
---|
79 |
|
---|
80 | // Add the new server to the server list
|
---|
81 | fClientList[s] = new StateClient(s, *this);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // --------------------------------------------------------------------------
|
---|
85 | //
|
---|
86 | //! Removes the StateClient for the given server. Don't forget to
|
---|
87 | //! call this function if it is overwritten in a derived class.
|
---|
88 | //!
|
---|
89 | //! @param s
|
---|
90 | //! server which should be removed
|
---|
91 | //!
|
---|
92 | //! @throws
|
---|
93 | //! a runtime_error is the server to be removed is not in the list
|
---|
94 | //
|
---|
95 | void DimNetwork::RemoveServer(const string &s)
|
---|
96 | {
|
---|
97 | DimServiceInfoList::RemoveServer(s);
|
---|
98 | if (s=="DIM_DNS")
|
---|
99 | return;
|
---|
100 |
|
---|
101 | const ClientList::iterator v = fClientList.find(s);
|
---|
102 | if (v==fClientList.end())
|
---|
103 | {
|
---|
104 | stringstream err;
|
---|
105 | err << "Server '" << s << "' not in list as it ought to be.";
|
---|
106 | throw runtime_error(err.str());
|
---|
107 | }
|
---|
108 |
|
---|
109 | // Remove the server from the server list
|
---|
110 | delete v->second;
|
---|
111 |
|
---|
112 | fClientList.erase(v);
|
---|
113 | }
|
---|
114 |
|
---|
115 | // --------------------------------------------------------------------------
|
---|
116 | //
|
---|
117 | //! RemovesAll StateClients. Don't forget to call this function if it
|
---|
118 | //! is overwritten in a derived class.
|
---|
119 | //!
|
---|
120 | void DimNetwork::RemoveAllServers()
|
---|
121 | {
|
---|
122 | DimServiceInfoList::RemoveAllServers();
|
---|
123 | DeleteClientList();
|
---|
124 | }
|
---|
125 |
|
---|
126 | // --------------------------------------------------------------------------
|
---|
127 | //
|
---|
128 | //! @param server
|
---|
129 | //! server for which the current state should be returned
|
---|
130 | //!
|
---|
131 | //! @returns
|
---|
132 | //! the current state of the given server, -2 if the server was not found
|
---|
133 | //!
|
---|
134 | int DimNetwork::GetCurrentState(const string &server) const
|
---|
135 | {
|
---|
136 | const ClientList::const_iterator v = fClientList.find(server);
|
---|
137 | return v==fClientList.end() ? -2 : v->second->GetState();
|
---|
138 | }
|
---|