Changeset 10390
- Timestamp:
- 04/18/11 14:32:50 (14 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/RemoteControl.cc
r10341 r10390 1 1 #include "RemoteControl.h" 2 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 address12 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 3 // ========================================================================== 37 38 void RemoteControlImp::infoHandler()39 {40 const vector<string> list = fServiceList.GetServerList();41 42 // Remover StateClients which are not available anymore43 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 new57 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 4 70 5 bool RemoteControlImp::ProcessCommand(const std::string &str) … … 82 17 { 83 18 const string c = str.substr(p2+1); 84 return ! fCommandList.SendDimCommand(lout, s, c);19 return !SendDimCommand(lout, s, c); 85 20 } 86 21 87 if (fServiceList.HasServer(s))22 if (HasServer(s)) 88 23 { 89 24 fCurrentServer = s; … … 100 35 } 101 36 102 return ! fCommandList.SendDimCommand(lout, fCurrentServer, str);37 return !SendDimCommand(lout, fCurrentServer, str); 103 38 } 104 39 105 40 // ========================================================================== 106 #include <boost/regex.hpp>107 41 108 42 #include "tools.h" … … 123 57 return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str()); 124 58 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]+\\].*"); 59 const State state = GetState(fCurrentServer, l->second->GetState()); 129 60 130 61 // The server 131 62 const string serv = Form("\033[34m\033[1m%s\033[0m", fCurrentServer.c_str()); 132 63 133 boost::smatch what;134 const int rc = boost::regex_match(msg, what, expr, boost::match_extra);135 136 64 // 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());65 if (state.index<-1) 66 return beg + " " + serv + "> "; 139 67 140 68 // 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()); 69 return beg + " " + serv + ":\033[32m\033[1m" + state.name + "\033[0m> "; 143 70 } 144 71 … … 158 85 return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str()); 159 86 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); 87 const State state = GetState(fCurrentServer, l->second->GetState()); 167 88 168 89 // 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());90 if (state.index<-1) 91 return beg + " " + fCurrentServer + "> "; 171 92 172 93 // If everything found add the state to the server 173 const string state = what[1]; 174 return beg + " " + fCurrentServer + ":" + state + "> "; 94 return beg + " " + fCurrentServer + ":" + state.name + "> "; 175 95 } -
trunk/FACT++/src/RemoteControl.h
r10341 r10390 1 1 #ifndef FACT_RemoteControl 2 2 #define FACT_RemoteControl 3 4 // **************************************************************************5 /** @class StateClient6 7 @brief A simple Dim client diriving from MessageDimRX subscribing to the STATE service8 9 This is a simple dim client which subscribes to the MESSAGE and STATE10 service of a server. It stores the last state and its time as well as11 the last message sent.12 13 **/14 // **************************************************************************15 #include "MessageDim.h"16 #include "Time.h"17 18 class StateClient : public MessageDimRX19 {20 private:21 Time fStateTime; /// Combine into one MTime (together with value)22 int fState; /// -2 not initialized, -1 not connected, 0>= state of client23 24 std::string fStateMsg; /// The message received with the last state change25 26 DimStampedInfo fInfoState; /// The dim service subscription27 28 protected:29 void infoHandler();30 31 public:32 StateClient(const std::string &name, MessageImp &imp);33 34 bool IsConnected() const { return fState>=0; }35 int GetState() const { return fState; }36 std::string GetMsg() const { return fStateMsg; }37 };38 3 39 4 // ************************************************************************** … … 51 16 **/ 52 17 // ************************************************************************** 53 #include "ServiceList.h" 54 #include "DimErrorRedirecter.h" 55 56 using namespace std; 57 58 class RemoteControlImp : public MessageImp, public DimErrorRedirecter, public DimInfoHandler 59 { 60 private: 18 #include "DimNetwork.h" 19 20 class RemoteControlImp : public DimNetwork 21 { 22 protected: 61 23 std::ostream &lout; /// Output stream for local synchrounous output 62 24 63 protected:64 typedef std::map<const std::string, StateClient*> ClientList;65 66 ServiceList fCommandList; /// An up-to-date list of all available commands67 ServiceList fServiceList; /// An up-to-date list of all available services68 ClientList fClientList; /// A list with all MESSAGE services to which we subscribed69 70 25 std::string fCurrentServer; /// The server to which we currently cd'ed 71 72 void infoHandler();73 26 74 27 protected: 75 28 // Redirect asynchronous output to the output window 76 29 RemoteControlImp(std::ostream &out, std::ostream &in) : 77 MessageImp(out), 78 DimErrorRedirecter(static_cast<MessageImp&>(*this)), 79 lout(in), fCommandList("CMD", out), fServiceList("", out) 80 { 81 fCommandList.SetHandler(this); 82 fServiceList.SetHandler(this); 30 DimNetwork(out), lout(in) 31 { 83 32 } 84 33 bool ProcessCommand(const std::string &str); 85 34 }; 35 36 86 37 87 38 // ************************************************************************** … … 110 61 { 111 62 private: 112 std::ostream &lout; /// Output stream for local synchrounous output113 114 63 static void append(std::string &str) 115 64 { … … 134 83 135 84 if (p1==string::npos && p2!=string::npos) 136 return T::Complete( fCommandList.GetServiceList(), text);137 138 std::vector<std::string> v = fCommandList.GetServerList();85 return T::Complete(GetCommandList(), text); 86 87 std::vector<std::string> v = GetServerList(); 139 88 for_each(v.begin(), v.end(), RemoteControl::append); 140 89 return T::Complete(v, text); 141 90 } 142 91 else 143 return T::Complete( fCommandList.GetServiceList(fCurrentServer), text);144 } 145 return T::Complete( fCommandList.GetServiceList(l), text);92 return T::Complete(GetCommandList(fCurrentServer), text); 93 } 94 return T::Complete(GetCommandList(l), text); 146 95 } 147 96 … … 149 98 { 150 99 RemoteControlImp::infoHandler(); 151 if (!fCurrentServer.empty() && ! fCommandList.HasServer(fCurrentServer))100 if (!fCurrentServer.empty() && !HasServer(fCurrentServer)) 152 101 { 153 102 fCurrentServer = ""; … … 159 108 // Redirect asynchronous output to the output window 160 109 RemoteControl(const char *name) : T(name), 161 RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()), 162 lout(T::GetStreamIn()) 163 { 164 } 165 110 RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()) 111 { 112 } 166 113 167 114 bool PrintGeneralHelp() … … 170 117 lout << " " << kUnderline << "Specific commands:" << endl; 171 118 lout << kBold << " h,help <arg> " << kReset << "List help text for given server or command." << endl; 172 lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl;119 // lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl; 173 120 lout << kBold << " svc,services " << kReset << "List all services in the network." << endl; 121 lout << kBold << " st,states " << kReset << "List all states in the network." << endl; 174 122 lout << endl; 175 123 return true; … … 179 127 { 180 128 lout << endl << kBold << "List of commands:" << endl; 181 fCommandList.PrintDescription(lout);129 PrintDescription(lout, true); 182 130 return true; 183 131 } … … 198 146 if (!fCurrentServer.empty()) 199 147 { 200 if ( fCommandList.PrintDescription(lout, fCurrentServer, svc)==0)148 if (PrintDescription(lout, true, fCurrentServer, svc)==0) 201 149 lout << " " << svc << ": <not found>" << endl; 202 150 } 203 151 else 204 152 { 205 if ( fCommandList.PrintDescription(lout, s, c)==0)153 if (PrintDescription(lout, true, s, c)==0) 206 154 lout << " <no matches found>" <<endl; 207 155 } … … 216 164 return true; 217 165 218 if (str=="servers" || str=="s")219 {220 fCommandList.PrintServerList(lout);221 return true;222 }223 224 166 if (str=="services" || str=="svc") 225 167 { 226 fServiceList.PrintDescription(lout); 168 PrintDescription(lout, false); 169 return true; 170 } 171 172 if (str=="states" || str=="st") 173 { 174 PrintStates(lout); 227 175 return true; 228 176 }
Note:
See TracChangeset
for help on using the changeset viewer.