#ifndef FACT_RemoteControl #define FACT_RemoteControl #include "InterpreterV8.h" // ************************************************************************** /** @class RemoteControlImp @brief This implements the basic functions of a remote control via dim Through a ServiceList object this object subscribes to all available SERVICE_LISTs in the dim network. This allows to keep an up-to-date list of all servers and services. Its ProcessCommand member function allows to emit commands according to the services found in the network. Its infoHandler() is called as an update notifier from the ClientList object. **/ // ************************************************************************** #include "DimNetwork.h" class RemoteControlImp : public DimNetwork { protected: std::ostream &lout; /// Output stream for local synchrounous output std::string fCurrentServer; /// The server to which we currently cd'ed protected: // Redirect asynchronous output to the output window RemoteControlImp(std::ostream &out, std::ostream &in) : DimNetwork(out), lout(in) { } bool ProcessCommand(const std::string &str); }; // ************************************************************************** /** @class RemoteControl @brief Implements a remote control based on a Readline class for the dim network This template implements all functions which overwrite any function from the Readline class. Since several derivatives of the Readline class implement different kind of Readline access, this class can be derived by any of them due to its template argument. However, the normal case will be deriving it from either Console or Shell. @tparam T The base class for RemoteControl. Either Readlien or a class deriving from it. This is usually either Console or Shell. **/ // ************************************************************************** #include "WindowLog.h" #include "ReadlineColor.h" #include "tools.h" #include "MainImp.h" template class RemoteControl : public T, public RemoteControlImp, public InterpreterV8 { private: MessageImp *fImp; void SetSection(int s) { if (fImp) fImp->Write(Time(), "", s); } int Write(const Time &time, const std::string &txt, int qos=kMessage) { if (!fImp) return 0; return fImp ? fImp->Write(time, txt, qos) : MessageImp::Write(time, txt, qos); } void exitHandler(int code) { if (dynamic_cast(fImp)) dynamic_cast(fImp)->Stop(code); else exit(code); } static void append(std::string &str) { str.append("/"); } static void chop(std::string &str) { const size_t p = str.find_first_of('/'); if (p!=string::npos) str = str.substr(p+1); } // This funtion defines which generator should be called. // If it returns 0 the standard reaqdline generator are called. // Otherwise set the right generator with rl_completion_matches. char **Completion(const char *text, int start, int) { // Get the whole buffer before the tab-position const string b = string(T::GetBuffer()); const string s = b.substr(0, start); const string l = Tools::Trim(s.c_str()); if (l.empty()) { if (fCurrentServer.empty()) { const size_t p1 = b.find_first_of(' '); const size_t p2 = b.find_first_of('/'); if (p1==string::npos && p2!=string::npos) return T::Complete(GetCommandList(), text); std::vector v = GetServerList(); for_each(v.begin(), v.end(), RemoteControl::append); return T::Complete(v, text); } else { std::vector v = GetCommandList(fCurrentServer); for_each(v.begin(), v.end(), RemoteControl::chop); return T::Complete(v, text); } } return T::Complete(GetCommandList(l), text); } void RemoveServer(std::string s) { DimNetwork::RemoveServer(s); if (fCurrentServer==s) { fCurrentServer = ""; T::UpdatePrompt(); } } void RemoveAllServers() { DimNetwork::RemoveAllServers(); fCurrentServer = ""; T::UpdatePrompt(); } // =========================================================================== virtual void JsLoad(const std::string &) { SetSection(-2); } virtual void JsStart(const std::string &) { SetSection(-1); } virtual void JsEnd(const std::string &) { SetSection(-3); } virtual bool JsSend(const std::string &str) { return ProcessCommand(str); } virtual void JsPrint(const std::string &msg) { fImp->Comment(msg); } virtual void JsException(const std::string &str) { fImp->Error(str); } int JsState(const string &server) { const ClientList::const_iterator l = fClientList.find(server); return l==fClientList.end() ? -256 : l->second->GetState(); } string JsName(const string &server) { const ClientList::const_iterator l = fClientList.find(server); if (l==fClientList.end()) return ""; return GetState(server, l->second->GetState()).name; } void JsSleep(uint32_t ms) { const Time timeout = Time()+boost::posix_time::millisec(ms==0?1:ms); T::GetStreamOut().Display(true); T::GetStreamOut().SetBacklog(false); T::GetStreamOut().SetNullOutput(false); while (timeout>Time() && !T::IsScriptStopped()) usleep(1); T::GetStreamOut().SetNullOutput(true); T::GetStreamOut().SetBacklog(true); } int JsWait(const string &server, int32_t state, uint32_t ms) { const ClientList::const_iterator l = fClientList.find(server); if (l==fClientList.end()) { lout << kRed << "Server '" << server << "' not found." << endl; T::StopScript(); return -1; } const Time timeout = ms<=0 ? Time(Time::none) : Time()+boost::posix_time::millisec(ms); T::GetStreamOut().Display(true); T::GetStreamOut().SetBacklog(false); T::GetStreamOut().SetNullOutput(false); while (l->second->GetState()!=state && timeout>Time() && !T::IsScriptStopped()) usleep(1); T::GetStreamOut().SetNullOutput(true); T::GetStreamOut().SetBacklog(true); return l->second->GetState()==state; } // =========================================================================== public: // Redirect asynchronous output to the output window RemoteControl(const char *name) : T(name), RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()), fImp(0) { } bool PrintGeneralHelp() { T::PrintGeneralHelp(); lout << " " << kUnderline << "Specific commands:\n"; lout << kBold << " h,help " << kReset << "List help text for given server or command.\n"; // lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl; lout << kBold << " svc,services " << kReset << "List all services in the network.\n"; lout << kBold << " st,states " << kReset << "List all states in the network.\n"; lout << kBold << " > " << kReset << "Echo to the output stream\n"; lout << kBold << " .s " << kReset << "Wait for the state-machine to change to the given state.\n"; lout << " " " .s [ [ [