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