Changeset 14126 for trunk/FACT++
- Timestamp:
- 06/08/12 18:05:57 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/RemoteControl.cc
r14050 r14126 49 49 50 50 // If we have not cd'ed to a server show only the line start 51 if (fCurrentServer.empty() )51 if (fCurrentServer.empty() || !fImp) 52 52 return beg + "> "; 53 53 54 54 // Check if we have cd'ed to a valid server 55 const ClientList::const_iterator l = fClientList.find(fCurrentServer);56 if ( l==fClientList.end())55 const pair<int32_t, string> state = fImp->GetServerState(fCurrentServer); 56 if (state.first==-256) 57 57 return beg + "> "; 58 59 const State state = GetState(fCurrentServer, l->second->GetState());60 58 61 59 // The server … … 63 61 64 62 // If no match found or something wrong found just output the server 65 if (state. index<-1)63 if (state.first<-1) 66 64 return beg + " " + serv + "> "; 67 65 68 66 // If everything found add the state to the server 69 return beg + " " + serv + ":\033[32m\033[1m" + state. name+ "\033[0m> ";67 return beg + " " + serv + ":\033[32m\033[1m" + state.second + "\033[0m> "; 70 68 } 71 69 … … 77 75 78 76 // If we have not cd'ed to a server show only the line start 79 if (fCurrentServer.empty() )77 if (fCurrentServer.empty() || !fImp) 80 78 return beg + "> "; 81 79 82 // Check if we have cd'ed to a valid server 83 const ClientList::const_iterator l = fClientList.find(fCurrentServer); 84 if (l==fClientList.end()) 80 const pair<int32_t, string> state = fImp->GetServerState(fCurrentServer); 81 if (state.first==-256) 85 82 return beg + "> ";//Form("\n[%d] \033[34m\033[1m%s\033[0m> ", GetLine(), fCurrentServer.c_str()); 86 83 87 const State state = GetState(fCurrentServer, l->second->GetState());88 89 84 // If no match found or something wrong found just output the server 90 if (state. index<-1)85 if (state.first<-1) 91 86 return beg + " " + fCurrentServer + "> "; 92 87 93 88 // If everything found add the state to the server 94 return beg + " " + fCurrentServer + ":" + state. name+ "> ";89 return beg + " " + fCurrentServer + ":" + state.second + "> "; 95 90 } -
trunk/FACT++/src/RemoteControl.h
r14060 r14126 1 1 #ifndef FACT_RemoteControl 2 2 #define FACT_RemoteControl 3 4 #include "InterpreterV8.h"5 3 6 4 // ************************************************************************** … … 18 16 **/ 19 17 // ************************************************************************** 20 #include "DimNetwork.h" 21 22 class RemoteControlImp : public DimNetwork 18 #include <string> 19 20 using namespace std; 21 22 class RemoteControlImp 23 23 { 24 24 protected: … … 29 29 protected: 30 30 // Redirect asynchronous output to the output window 31 RemoteControlImp(std::ostream & out, std::ostream &in) :32 DimNetwork(out), lout(in)33 {34 }31 RemoteControlImp(std::ostream &, std::ostream &in) : lout(in) 32 { 33 } 34 virtual ~RemoteControlImp() { } 35 35 bool ProcessCommand(const std::string &str); 36 37 virtual bool HasServer(const std::string &) { return false; } 38 virtual bool SendDimCommand(ostream &, std::string &, const std::string &) { return false; } 36 39 }; 37 38 39 40 40 41 // ************************************************************************** … … 55 56 **/ 56 57 // ************************************************************************** 57 #include "WindowLog.h" 58 #include "StateMachineDimControl.h" 59 60 #include "InterpreterV8.h" 58 61 #include "ReadlineColor.h" 62 #include "Event.h" 59 63 #include "tools.h" 60 #include "MainImp.h"61 64 62 65 template <class T> 63 66 class RemoteControl : public T, public RemoteControlImp, public InterpreterV8 64 67 { 65 pr ivate:66 MessageImp*fImp;68 protected: 69 StateMachineDimControl *fImp; 67 70 68 71 void SetSection(int s) { if (fImp) fImp->Write(Time(), "", s); } 69 72 70 int Write(const Time &time, const std::string &txt, int qos= kMessage)73 int Write(const Time &time, const std::string &txt, int qos=MessageImp::kMessage) 71 74 { 72 75 if (!fImp) … … 76 79 77 80 void exitHandler(int code) { if (dynamic_cast<MainImp*>(fImp)) dynamic_cast<MainImp*>(fImp)->Stop(code); else exit(code); } 81 82 // ==================== Readline tab-completion ===================== 78 83 79 84 static void append(std::string &str) … … 121 126 } 122 127 123 void RemoveServer(std::string s) 124 { 125 DimNetwork::RemoveServer(s); 126 if (fCurrentServer==s) 127 { 128 void EventHook() 129 { 130 if (fImp && !fImp->HasServer(fCurrentServer)) 128 131 fCurrentServer = ""; 129 T::UpdatePrompt(); 130 } 131 } 132 133 void RemoveAllServers() 134 { 135 DimNetwork::RemoveAllServers(); 136 fCurrentServer = ""; 137 T::UpdatePrompt(); 138 } 139 140 // =========================================================================== 141 142 virtual void JsLoad(const std::string &) { SetSection(-2); } 143 virtual void JsStart(const std::string &) { SetSection(-1); } 144 virtual void JsEnd(const std::string &) { SetSection(-3); } 145 virtual bool JsSend(const std::string &str) { return ProcessCommand(str); } 146 virtual void JsPrint(const std::string &msg) { fImp->Comment(msg); } 147 virtual void JsException(const std::string &str) { fImp->Error(str); } 148 149 int JsState(const string &server) 150 { 151 const ClientList::const_iterator l = fClientList.find(server); 152 return l==fClientList.end() ? -256 : l->second->GetState(); 153 } 154 155 string JsName(const string &server) 156 { 157 158 const ClientList::const_iterator l = fClientList.find(server); 159 if (l==fClientList.end()) 160 return ""; 161 162 return GetState(server, l->second->GetState()).name; 132 133 T::EventHook(); 134 } 135 136 // ===== Interface to access the DIM network through the StateMachine ==== 137 138 bool HasServer(const std::string &server) { return fImp ? fImp->HasServer(server) : false; } 139 vector<string> GetServerList() const { return fImp ? fImp->GetServerList() : vector<string>(); } 140 vector<string> GetCommandList(const string &server) const { return fImp ? fImp->GetCommandList(server) : vector<string>(); } 141 vector<string> GetCommandList() const { return fImp ? fImp->GetCommandList() : vector<string>(); } 142 int PrintStates(std::ostream &out, const std::string &serv="") const { return fImp ? fImp->PrintStates(out, serv) : 0; } 143 int PrintDescription(std::ostream &out, bool iscmd, const std::string &serv="", const std::string &service="") const 144 { return fImp ? fImp->PrintDescription(out, iscmd, serv, service) : 0; } 145 bool SendDimCommand(ostream &out, std::string &server, const std::string &str) 146 { 147 try 148 { 149 if (fImp) 150 fImp->SendDimCommand(server, str, out); 151 //lout << kGreen << "Command emitted successfully to " << server << "." << endl; 152 return true; 153 } 154 catch (const runtime_error &e) 155 { 156 lout << kRed << e.what() << endl; 157 return false; 158 } 159 } 160 161 // ============ Pseudo-callback interface for the JavaScrip engine ======= 162 163 virtual void JsLoad(const std::string &) { SetSection(-2); } 164 virtual void JsStart(const std::string &) { SetSection(-1); } 165 virtual void JsEnd(const std::string &) { UnsubscribeAll(); SetSection(-3); } 166 virtual bool JsSend(const std::string &str) { return ProcessCommand(str); } 167 virtual void JsPrint(const std::string &msg) { if (fImp) fImp->Comment(msg.empty()?" ":msg); } 168 virtual void JsException(const std::string &str) { if (fImp) fImp->Error(str.empty()?" ":str); } 169 170 pair<int32_t, string> JsState(const std::string &server) 171 { 172 return fImp ? fImp->GetServerState(server) : make_pair(-256, string()); 163 173 } 164 174 … … 167 177 const Time timeout = Time()+boost::posix_time::millisec(ms==0?1:ms); 168 178 169 T::GetStreamOut().Display(true); 170 T::GetStreamOut().SetBacklog(false); 171 T::GetStreamOut().SetNullOutput(false); 179 T::Lock(); 180 172 181 while (timeout>Time() && !T::IsScriptStopped()) 173 182 usleep(1); 174 T::GetStreamOut().SetNullOutput(true); 175 T:: GetStreamOut().SetBacklog(true);183 184 T::Unlock(); 176 185 } 177 186 178 187 int JsWait(const string &server, int32_t state, uint32_t ms) 179 188 { 180 const ClientList::const_iterator l = fClientList.find(server); 181 if (l==fClientList.end()) 189 if (!fImp) 190 { 191 lout << kRed << "RemoteControl class not fully initialized." << endl; 192 T::StopScript(); 193 return -1; 194 } 195 196 if (!HasServer(server)) 182 197 { 183 198 lout << kRed << "Server '" << server << "' not found." << endl; … … 186 201 } 187 202 203 T::Lock(); 204 188 205 const Time timeout = ms<=0 ? Time(Time::none) : Time()+boost::posix_time::millisec(ms); 189 206 190 T::GetStreamOut().Display(true); 191 T::GetStreamOut().SetBacklog(false); 192 T::GetStreamOut().SetNullOutput(false); 193 while (l->second->GetState()!=state && timeout>Time() && !T::IsScriptStopped()) 207 int rc = 0; 208 while (timeout>Time() && !T::IsScriptStopped()) 209 { 210 const pair<int32_t, string> st = fImp->GetServerState(server); 211 if (st.first==-256) 212 { 213 lout << kRed << "Server '" << server << "' disconnected." << endl; 214 T::StopScript(); 215 return -1; 216 } 217 if (st.first==state) 218 { 219 rc = 1; 220 break; 221 } 222 194 223 usleep(1); 195 T::GetStreamOut().SetNullOutput(true); 196 T::GetStreamOut().SetBacklog(true); 197 198 return l->second->GetState()==state; 199 } 200 224 } 225 T::Unlock(); 226 227 return rc; 228 } 229 230 vector<Description> JsDescription(const string &service) 231 { 232 return fImp ? fImp->GetDescription(service) : vector<Description>(); 233 } 234 235 // Keep a copy of the data for access by V8 236 map<string, EventImp *> fEvents; // For unsibscription 237 map<string, pair<uint64_t, Event>> fData; 238 239 std::mutex fMutex; 240 241 pair<uint64_t, EventImp *> JsGetEvent(const std::string &service) 242 { 243 const lock_guard<mutex> lock(fMutex); 244 245 const auto it = fData.find(service); 246 247 return it==fData.end() ? make_pair(uint64_t(0), (Event*)0) : make_pair(it->second.first, &it->second.second); 248 } 249 250 void Handle(const EventImp &evt, const string &service) 251 { 252 const lock_guard<mutex> lock(fMutex); 253 254 const auto it = fData.find(service); 255 if (it==fData.end()) 256 fData[service] = make_pair(0, static_cast<Event>(evt)); 257 else 258 { 259 it->second.first++; 260 it->second.second = static_cast<Event>(evt); 261 } 262 } 263 264 void *JsSubscribe(const std::string &service) 265 { 266 if (!fImp) 267 return 0; 268 269 // Do not subscribe twice 270 if (fEvents.find(service)!=fEvents.end()) 271 return 0; 272 273 return fEvents[service] = &fImp->Subscribe(service)(fImp->Wrap(bind(&RemoteControl<T>::Handle, this, placeholders::_1, service))); 274 } 275 276 bool JsUnsubscribe(const std::string &service) 277 { 278 if (!fImp) 279 return false; 280 281 const auto it = fEvents.find(service); 282 if (it==fEvents.end()) 283 return false; 284 285 fImp->Unsubscribe(it->second); 286 fEvents.erase(it); 287 288 return true; 289 } 290 291 void UnsubscribeAll() 292 { 293 for (auto it=fEvents.begin(); it!=fEvents.end(); it++) 294 fImp->Unsubscribe(it->second); 295 fEvents.clear(); 296 } 201 297 202 298 // =========================================================================== … … 215 311 lout << " " << kUnderline << "Specific commands:\n"; 216 312 lout << kBold << " h,help <arg> " << kReset << "List help text for given server or command.\n"; 217 // lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl;218 313 lout << kBold << " svc,services " << kReset << "List all services in the network.\n"; 219 314 lout << kBold << " st,states " << kReset << "List all states in the network.\n"; … … 342 437 } 343 438 344 void SetReceiver( MessageImp&imp) { fImp = &imp; }439 void SetReceiver(StateMachineDimControl &imp) { fImp = &imp; } 345 440 }; 346 441
Note:
See TracChangeset
for help on using the changeset viewer.