1 | #include "RemoteControl.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 | void StateClient::infoHandler()
|
---|
10 | {
|
---|
11 | DimInfo *curr = getInfo(); // get current DimInfo address
|
---|
12 | if (!curr)
|
---|
13 | return;
|
---|
14 |
|
---|
15 | if (curr==&fInfoState)
|
---|
16 | {
|
---|
17 | const char *txt = fInfoState.getString();
|
---|
18 |
|
---|
19 | fState = strlen(txt)==0 ? -2 : fInfoState.getQuality();
|
---|
20 | fStateTime = Time(fInfoState.getTimestamp(),
|
---|
21 | fInfoState.getTimestampMillisecs());
|
---|
22 | fStateMsg = txt;
|
---|
23 |
|
---|
24 | if (fState!=-2)
|
---|
25 | {
|
---|
26 | stringstream msg;
|
---|
27 | msg << fInfoState.getName() << ": Changed state to " << fState << " '" << txt << "' received.";
|
---|
28 | fMsg.Write(fStateTime, msg.str().c_str(), MessageImp::kInfo);
|
---|
29 | }
|
---|
30 | return;
|
---|
31 | }
|
---|
32 |
|
---|
33 | MessageDimRX::infoHandler();
|
---|
34 | }
|
---|
35 |
|
---|
36 | // ==========================================================================
|
---|
37 |
|
---|
38 | void RemoteControlImp::infoHandler()
|
---|
39 | {
|
---|
40 | const vector<string> &list = fServiceList.GetServerList();
|
---|
41 |
|
---|
42 | // Remover StateClients which are not available anymore
|
---|
43 | for (ClientList::iterator l=fClientList.begin(); l!=fClientList.end(); l++)
|
---|
44 | {
|
---|
45 | const string &server = l->first;
|
---|
46 |
|
---|
47 | // HasServer doesn't work here, I don't understand why.
|
---|
48 | if (find(list.begin(), list.end(), server)!=list.end())
|
---|
49 | continue;
|
---|
50 |
|
---|
51 | delete l->second;
|
---|
52 | fClientList.erase(l);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | // Add StateClients which are new
|
---|
57 | for (vector<string>::const_iterator l=list.begin(); l!=list.end(); l++)
|
---|
58 | {
|
---|
59 | if (*l=="DIS_DNS")
|
---|
60 | continue;
|
---|
61 |
|
---|
62 | const ClientList::const_iterator p = fClientList.find(*l);
|
---|
63 | if (p!=fClientList.end())
|
---|
64 | continue;
|
---|
65 |
|
---|
66 | fClientList[*l] = new StateClient(*l, *this);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | bool RemoteControlImp::ProcessCommand(const std::string &str)
|
---|
71 | {
|
---|
72 | if (fCurrentServer.empty())
|
---|
73 | {
|
---|
74 | if(fServiceList.HasServer(str))
|
---|
75 | {
|
---|
76 | fCurrentServer = str;
|
---|
77 | return true;
|
---|
78 | }
|
---|
79 | lout << kRed << "Unkown server '" << str << "'" << endl;
|
---|
80 | return false;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (!fCurrentServer.empty() && str=="..")
|
---|
84 | {
|
---|
85 | fCurrentServer = "";
|
---|
86 | return false;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return !fServiceList.SendDimCommand(lout, fCurrentServer, str);
|
---|
90 | }
|
---|
91 |
|
---|
92 | // ==========================================================================
|
---|
93 | #include <boost/regex.hpp>
|
---|
94 |
|
---|
95 | #include "tools.h"
|
---|
96 |
|
---|
97 | string RemoteConsole::GetUpdatePrompt() const
|
---|
98 | {
|
---|
99 | // If we are continously flushing the buffer omit the buffer size
|
---|
100 | // If we are buffering show the buffer size
|
---|
101 | const string beg = "\n" + GetLinePrompt();
|
---|
102 |
|
---|
103 | // If we have not cd'ed to a server show only the line start
|
---|
104 | if (fCurrentServer.empty())
|
---|
105 | return beg + "> ";
|
---|
106 |
|
---|
107 | // Check if we have cd'ed to a valid server
|
---|
108 | const ClientList::const_iterator l = fClientList.find(fCurrentServer);
|
---|
109 | if (l==fClientList.end())
|
---|
110 | return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
|
---|
111 |
|
---|
112 | // The server is valid, check for the state from the last status message
|
---|
113 | const string msg = l->second->GetMsg();
|
---|
114 |
|
---|
115 | static const boost::regex expr("(.+)\\[[0-9]+\\].*");
|
---|
116 |
|
---|
117 | // The server
|
---|
118 | const string serv = Form("\033[34m\033[1m%s\033[0m", fCurrentServer.c_str());
|
---|
119 |
|
---|
120 | boost::smatch what;
|
---|
121 | const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
|
---|
122 |
|
---|
123 | // If no match found or something wrong found just output the server
|
---|
124 | if (!rc || what.size()!=2)
|
---|
125 | return beg + " " + serv + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
|
---|
126 |
|
---|
127 | // If everything found add the state to the server
|
---|
128 | const string state = what[1];
|
---|
129 | return beg + " " + serv + Form(":\033[32m\033[1m%s\033[0m> ", state.c_str());
|
---|
130 | }
|
---|
131 |
|
---|
132 | string RemoteShell::GetUpdatePrompt() const
|
---|
133 | {
|
---|
134 | // If we are continously flushing the buffer omit the buffer size
|
---|
135 | // If we are buffering show the buffer size
|
---|
136 | const string beg = GetLinePrompt();
|
---|
137 |
|
---|
138 | // If we have not cd'ed to a server show only the line start
|
---|
139 | if (fCurrentServer.empty())
|
---|
140 | return beg + "> ";
|
---|
141 |
|
---|
142 | // Check if we have cd'ed to a valid server
|
---|
143 | const ClientList::const_iterator l = fClientList.find(fCurrentServer);
|
---|
144 | if (l==fClientList.end())
|
---|
145 | return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
|
---|
146 |
|
---|
147 | // The server is valid, check for the state from the last status message
|
---|
148 | const string msg = l->second->GetMsg();
|
---|
149 |
|
---|
150 | static const boost::regex expr("(.+)\\[[0-9]+\\].*");
|
---|
151 |
|
---|
152 | boost::smatch what;
|
---|
153 | const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
|
---|
154 |
|
---|
155 | // If no match found or something wrong found just output the server
|
---|
156 | if (!rc || what.size()!=2)
|
---|
157 | return beg + " " + fCurrentServer + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
|
---|
158 |
|
---|
159 | // If everything found add the state to the server
|
---|
160 | const string state = what[1];
|
---|
161 | return beg + " " + fCurrentServer + ":" + state + "> ";
|
---|
162 | }
|
---|