source: trunk/FACT++/src/RemoteControl.cc@ 10380

Last change on this file since 10380 was 10341, checked in by tbretz, 14 years ago
Implemented help for a single server or command.
File size: 5.3 KB
Line 
1#include "RemoteControl.h"
2
3StateClient::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
9void 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
38void 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
70bool RemoteControlImp::ProcessCommand(const std::string &str)
71{
72 if (fCurrentServer.empty())
73 {
74 const size_t p1 = str.find_first_of(' ');
75 const size_t p2 = str.find_first_of('/');
76
77 string s = str;
78 if (p2!=string::npos && p1>p2)
79 s = str.substr(0, p2);
80
81 if (p2<p1)
82 {
83 const string c = str.substr(p2+1);
84 return !fCommandList.SendDimCommand(lout, s, c);
85 }
86
87 if(fServiceList.HasServer(s))
88 {
89 fCurrentServer = s;
90 return false;
91 }
92 lout << kRed << "Unkown server '" << s << "'" << endl;
93 return true;
94 }
95
96 if (!fCurrentServer.empty() && str=="..")
97 {
98 fCurrentServer = "";
99 return false;
100 }
101
102 return !fCommandList.SendDimCommand(lout, fCurrentServer, str);
103}
104
105// ==========================================================================
106#include <boost/regex.hpp>
107
108#include "tools.h"
109
110string RemoteConsole::GetUpdatePrompt() const
111{
112 // If we are continously flushing the buffer omit the buffer size
113 // If we are buffering show the buffer size
114 const string beg = "\n" + GetLinePrompt();
115
116 // If we have not cd'ed to a server show only the line start
117 if (fCurrentServer.empty())
118 return beg + "> ";
119
120 // Check if we have cd'ed to a valid server
121 const ClientList::const_iterator l = fClientList.find(fCurrentServer);
122 if (l==fClientList.end())
123 return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
124
125 // The server is valid, check for the state from the last status message
126 const string msg = l->second->GetMsg();
127
128 static const boost::regex expr("(.+)\\[[0-9]+\\].*");
129
130 // The server
131 const string serv = Form("\033[34m\033[1m%s\033[0m", fCurrentServer.c_str());
132
133 boost::smatch what;
134 const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
135
136 // If no match found or something wrong found just output the server
137 if (!rc || what.size()!=2)
138 return beg + " " + serv + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
139
140 // If everything found add the state to the server
141 const string state = what[1];
142 return beg + " " + serv + Form(":\033[32m\033[1m%s\033[0m> ", state.c_str());
143}
144
145string RemoteShell::GetUpdatePrompt() const
146{
147 // If we are continously flushing the buffer omit the buffer size
148 // If we are buffering show the buffer size
149 const string beg = GetLinePrompt();
150
151 // If we have not cd'ed to a server show only the line start
152 if (fCurrentServer.empty())
153 return beg + "> ";
154
155 // Check if we have cd'ed to a valid server
156 const ClientList::const_iterator l = fClientList.find(fCurrentServer);
157 if (l==fClientList.end())
158 return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str());
159
160 // The server is valid, check for the state from the last status message
161 const string msg = l->second->GetMsg();
162
163 static const boost::regex expr("(.+)\\[[0-9]+\\].*");
164
165 boost::smatch what;
166 const int rc = boost::regex_match(msg, what, expr, boost::match_extra);
167
168 // If no match found or something wrong found just output the server
169 if (!rc || what.size()!=2)
170 return beg + " " + fCurrentServer + "> ";//Form("[%d-%s] ", GetLine(), msg.c_str());
171
172 // If everything found add the state to the server
173 const string state = what[1];
174 return beg + " " + fCurrentServer + ":" + state + "> ";
175}
Note: See TracBrowser for help on using the repository browser.