#ifndef FACT_RemoteControl #define FACT_RemoteControl // ************************************************************************** /** @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" template class RemoteControl : public T, public RemoteControlImp { private: 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 infoHandler() { RemoteControlImp::infoHandler(); if (!fCurrentServer.empty() && !HasServer(fCurrentServer)) { fCurrentServer = ""; T::UpdatePrompt(); } } public: // Redirect asynchronous output to the output window RemoteControl(const char *name) : T(name), RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()) { } bool PrintGeneralHelp() { T::PrintGeneralHelp(); lout << " " << kUnderline << "Specific commands:" << endl; lout << kBold << " h,help " << kReset << "List help text for given server or command." << endl; // lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl; lout << kBold << " svc,services " << kReset << "List all services in the network." << endl; lout << kBold << " st,states " << kReset << "List all states in the network." << endl; lout << kBold << " # " << kReset << "Echo to the output stream" << endl; lout << kBold << " .s " << kReset << "Wait for the state-machine to change to the given state." << endl; lout << " " " .s [ []]" << endl; lout << " " " The server for which state to wait (e.g. FTM_CONTROL)" << endl; lout << " " " The state id (see 'states') for which to wait (e.g. 3)" << endl; lout << " " " A timeout in millisenconds how long to wait (e.g. 500)" << endl; lout << endl; return true; } bool PrintCommands() { lout << endl << kBold << "List of commands:" << endl; PrintDescription(lout, true); return true; } // returns whether a command should be put into the history bool Process(const std::string &str) { if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ") { const size_t p1 = str.find_first_of(' '); const string svc = str.substr(p1+1); const size_t p3 = svc.find_first_of('/'); const string s = svc.substr(0, p3); const string c = p3==string::npos?"":svc.substr(p3+1); lout << endl; if (!fCurrentServer.empty()) { if (PrintDescription(lout, true, fCurrentServer, svc)==0) lout << " " << svc << ": " << endl; } else { if (PrintDescription(lout, true, s, c)==0) lout << " " <> server >> state >> ms; if (state==-100) { lout << kRed << "Couldn't parse state id." << endl; return true; } const ClientList::const_iterator l = fClientList.find(server); if (l==fClientList.end()) { lout << kRed << "Server '" << server << "' not found." << endl; return true; } const Time timeout = ms<=0 ? Time(Time::none) : Time()+boost::posix_time::millisec(ms); while (l->second->GetState()!=state && timeout>Time()) usleep(1); return true; } if (str[0]=='#') { lout << Tools::Trim(str.substr(1)) << endl; return true; } if (ReadlineColor::Process(lout, str)) return true; if (T::Process(str)) return true; if (str=="services" || str=="svc") { PrintDescription(lout, false); return true; } if (str=="states" || str=="st") { PrintStates(lout); return true; } return ProcessCommand(str); } }; // ************************************************************************** /** @class RemoteConsole @brief Derives the RemoteControl from Control and adds a proper prompt This is basically a RemoteControl, which derives through the template argument from the Console class. It enhances the functionality of the remote control with a proper updated prompt. */ // ************************************************************************** #include "Console.h" class RemoteConsole : public RemoteControl { public: RemoteConsole(const char *name, bool continous=false) : RemoteControl(name) { SetContinous(continous); } string GetUpdatePrompt() const; }; // ************************************************************************** /** @class RemoteShell @brief Derives the RemoteControl from Shell and adds colored prompt This is basically a RemoteControl, which derives through the template argument from the Shell class. It enhances the functionality of the local control with a proper updated prompt. */ // ************************************************************************** #include "Shell.h" class RemoteShell : public RemoteControl { public: RemoteShell(const char *name, bool = false) : RemoteControl(name) { } string GetUpdatePrompt() const; }; #endif