1 #ifndef FACT_RemoteControl 2 #define FACT_RemoteControl 36 bool ProcessCommand(
const std::string &
str,
bool change=
true);
38 virtual bool HasServer(
const std::string &) {
return false; }
39 virtual bool SendDimCommand(ostream &,
const std::string &,
const std::string &,
bool =
false) {
return false; }
81 void exitHandler(
int code) {
if (dynamic_cast<MainImp*>(fImp))
dynamic_cast<MainImp*
>(fImp)->Stop(code);
else exit(code); }
91 const size_t p = str.find_first_of(
'/');
93 str = str.substr(p+1);
101 if (T::fScript==
"java")
103 return T::Complete(JsGetCommandList(text, start), text);
107 const string b = string(T::GetBuffer());
108 const string s = b.substr(0, start);
112 if (fCurrentServer.empty())
114 const size_t p1 = b.find_first_of(
' ');
115 const size_t p2 = b.find_first_of(
'/');
117 if (p1==string::npos && p2!=string::npos)
118 return T::Complete(GetCommandList(), text);
120 std::vector<std::string> v = GetServerList();
122 return T::Complete(v, text);
126 std::vector<std::string> v = GetCommandList(fCurrentServer);
128 return T::Complete(v, text);
131 return T::Complete(GetCommandList(l), text);
136 if (fImp && !fImp->
HasServer(fCurrentServer))
139 T::EventHook(newline);
149 int PrintDescription(std::ostream &out,
bool iscmd,
const std::string &
serv=
"",
const std::string &service=
"")
const 151 bool SendDimCommand(ostream &out,
const std::string &server,
const std::string &
str,
bool do_throw=
false)
160 catch (
const runtime_error &e)
162 lout <<
kRed << e.what() << endl;
170 void JsStart(
const std::string &) { SetSection(-2); }
172 bool JsSend(
const std::string &
str) {
return ProcessCommand(str,
false); }
175 void JsResult(
const std::string &msg) { lin <<
kBlue <<
" = " << msg <<
'\n' << endl; }
212 int JsWait(
const string &server, int32_t state, uint32_t ms)
216 lout <<
kRed <<
"RemoteControl class not fully initialized." << endl;
221 if (!HasServer(server))
223 lout <<
kRed <<
"Server '" << server <<
"' not found." << endl;
238 lout <<
kRed <<
"Server '" << server <<
"' disconnected." << endl;
250 while (timeout>
Time() && !T::IsScriptStopped());
259 return fImp ? fImp->
GetDescription(service) : vector<Description>();
274 pair<uint64_t, EventImp *>
JsGetEvent(
const std::string &service)
277 const lock_guard<mutex> lock(fMutex);
279 const auto it = fInfo.find(service);
283 return make_pair(0, static_cast<EventImp*>(NULL));
285 EventInfo &info = it->second;
289 return make_pair(0, static_cast<EventImp*>(NULL));
291 return make_pair(info.counter-1, (
EventImp*)&info.data);
299 const auto it = fInfo.find(service);
308 EventInfo &info = it->second;
310 const uint64_t cnt = ++info.counter;
311 info.data =
static_cast<Event>(evt);
315 JsHandleEvent(evt, cnt, service);
326 const lock_guard<mutex> lock(fMutex);
329 if (fInfo.find(service)!=fInfo.end())
333 fInfo.insert(make_pair(service, EventInfo(ptr)));
343 const lock_guard<mutex> lock(fMutex);
345 const auto it = fInfo.find(service);
358 const lock_guard<mutex> lock(fMutex);
360 for (
auto it=fInfo.begin(); it!=fInfo.end(); it++)
379 lout <<
" " <<
kUnderline <<
"Specific commands:\n";
380 lout <<
kBold <<
" h,help <arg> " <<
kReset <<
"List help text for given server or command.\n";
381 lout <<
kBold <<
" svc,services " << kReset <<
"List all services in the network.\n";
382 lout <<
kBold <<
" st,states " << kReset <<
"List all states in the network.\n";
383 lout <<
kBold <<
" > <text> " << kReset <<
"Echo <text> to the output stream\n";
384 lout <<
kBold <<
" .s " << kReset <<
"Wait for the state-machine to change to the given state.\n";
385 lout <<
" " " .s <server> [<state> [<timeout> [<label>]]]\n";
386 lout <<
" " "<server> The server for which state to wait (e.g. FTM_CONTROL)\n";
387 lout <<
" " "<state> The state id (see 'states') for which to wait (e.g. 3)\n";
388 lout <<
" " "<imeout> A timeout in millisenconds how long to wait (e.g. 500)\n";
389 lout <<
" " "<label> A label (number) until which everything is skipped in case of timeout\n";
390 lout <<
kBold <<
" .js file " << kReset <<
"Execute a JavaScript\n";
392 lout <<
kBold <<
" .java " << kReset <<
"Start JavaScript interpreter\n";
399 lout << endl <<
kBold <<
"List of commands:" << endl;
400 PrintDescription(lout,
true);
407 if (str.substr(0, 2)==
"h " || str.substr(0, 5)==
"help ")
409 const size_t p1 = str.find_first_of(
' ');
410 const string svc = str.substr(p1+1);
412 const size_t p3 = svc.find_first_of(
'/');
413 const string s = svc.substr(0, p3);
414 const string c = p3==string::npos?
"":svc.substr(p3+1);
417 if (!fCurrentServer.empty())
419 if (PrintDescription(lout,
true, fCurrentServer, svc)==0)
420 lout <<
" " << svc <<
": <not found>" << endl;
424 if (PrintDescription(lout,
true, s, c)==0)
425 lout <<
" <no matches found>" <<endl;
431 if (str.substr(0, 4)==
".js ")
433 string opt(str.substr(4));
439 lout <<
kRed <<
"JavaScript filename missing." << endl;
441 lout <<
kRed <<
"Equal sign missing in argument '" << data.begin()->first <<
"'" << endl;
468 if (str.substr(0, 3)==
".s ")
470 istringstream in(str.substr(3));
472 int state=-100, ms=0;
475 in >> server >> state >> ms;
478 lout <<
kRed <<
"Couldn't parse state id in '" << str.substr(3) <<
"'" << endl;
483 const int rc = JsWait(server, state, ms);
491 if (in.fail() && !in.eof())
493 lout <<
kRed <<
"Invalid label in '" << str.substr(3) <<
"'" << endl;
514 if (str==
"services" || str==
"svc")
516 PrintDescription(lout,
false);
520 if (str==
"states" || str==
"st")
526 return !ProcessCommand(str);
571 SetContinous(continous);
573 string GetUpdatePrompt()
const;
596 string GetUpdatePrompt()
const;
int GetStateIndex(const std::string &name) const
Derives the RemoteControl from Shell and adds colored prompt.
int Comment(const std::string &str)
This is an extension to the Readline class provding a colored output.
void JsHandleState(const std::string &, const State &)
bool PrintGeneralHelp(std::ostream &out, const std::string &name)
void * JsSubscribe(const std::string &service)
static void chop(std::string &str)
bool HasServer(const std::string &server)
void JsOut(const std::string &msg)
int GetCurrentState() const
return the current state of the machine
void SetInterruptHandler(const std::function< int(const EventImp &)> &func=std::function< int(const EventImp &)>())
A general base-class describing events issues in a state machine.
bool JsSend(const std::string &str)
void EventHook(bool newline)
void JsException(const std::string &str)
void JsPrint(const std::string &msg)
std::vector< std::string > GetCommandList(const std::string &server)
bool SendDimCommand(const std::string &server, std::string str, std::ostream &lout)
This is an extension to the Readline class provding buffered output.
Adds some functionality to boost::posix_time::ptime for our needs.
State JsState(const std::string &server)
bool JsNewState(int s, const string &n, const string &c)
const std::string GetStateDescription(int state) const
This implements the basic functions of a remote control via dim.
int Alarm(const std::string &str)
virtual void JsEnd(const std::string &="")
bool JsHasState(const string &n) const
void JsLoad(const std::string &)
bool Process(const std::string &str)
void SetStateCallback(const std::function< void(const std::string &, const State &)> &func)
StateMachineDimControl * fImp
virtual bool HasServer(const std::string &)
bool SendDimCommand(ostream &out, const std::string &server, const std::string &str, bool do_throw=false)
void exitHandler(int code)
int PrintDescription(std::ostream &out, bool iscmd, const std::string &serv="", const std::string &service="")
const std::string GetStateName(int state) const
void SetReceiver(StateMachineDimControl &imp)
State JsGetCurrentState() const
vector< Description > JsDescription(const string &service)
RemoteControl(const char *name)
void JsResult(const std::string &msg)
int PrintStates(std::ostream &out, const std::string &serv="")
int Write(const Time &time, const std::string &txt, int qos=MessageImp::kMessage)
int JsWait(const string &server, int32_t state, uint32_t ms)
virtual ~RemoteControlImp()
RemoteConsole(const char *name, bool continous=false)
Implements a remote control based on a Readline class for the dim network.
vector< Description > JsGetDescription(const string &server)
Just a message, usually obsolete.
virtual int Write(const Time &time, const std::string &txt, int qos=kMessage)
int JsHandleInterrupt(const EventImp &)
Derives the RemoteControl from Control and adds a proper prompt.
std::ostream & lout
Output stream for local synchrounous output.
bool JsUnsubscribe(const std::string &service)
std::set< Service > GetServiceList()
vector< string > GetCommandList(const string &server) const
bool JsHasState(int s) const
int Write(const Time &time, const std::string &txt, int qos=kMessage)
Redirect our own logging to fLog.
vector< string > GetCommandList() const
void JsStart(const std::string &)
void JsWarn(const std::string &msg)
int Error(const std::string &str)
char ** Completion(const char *text, int start, int)
std::vector< std::string > GetServerList()
map< string, EventInfo > fInfo
std::string fCurrentServer
Output stream for local synchrounous output.
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
bool HasState(int index) const
vector< string > GetServerList() const
vector< State > JsGetStates(const string &server)
State GetServerState(const std::string &server)
int PrintDescription(std::ostream &out, bool iscmd, const std::string &serv="", const std::string &service="") const
void JsEnd(const std::string &)
bool Process(std::ostream &out, const std::string &str)
static void append(std::string &str)
virtual void JsLoad(const std::string &="")
int ChangeState(int qos, const Time &time, int scriptdepth, std::string scriptfile, std::string user)
EventImp & Subscribe(const std::string &name)
std::vector< Description > GetDescription(const std::string &service)
Concerete implementation of an EventImp stroring name, format, data and time.
RemoteStream(const char *name, bool null=false)
std::vector< State > GetStates(const std::string &server)
int JsGetState(const string &n) const
Possible return value for GetStateIndex.
virtual bool SendDimCommand(ostream &, const std::string &, const std::string &, bool=false)
set< Service > JsGetServices()
void Unsubscribe(EventImp *evt)
RemoteControlImp(std::ostream &out, std::ostream &in)
The server to which we currently cd'ed.
bool HasServer(const std::string &server)
int PrintStates(std::ostream &out, const std::string &serv="") const
bool AddStateName(const int state, const std::string &name, const std::string &doc="")
Do not initialize the time.
pair< uint64_t, EventImp * > JsGetEvent(const std::string &service)
int Handle(const EventImp &evt, const string &service)
RemoteShell(const char *name, bool=false)
Implementation of a console based user shell with an input and output window.
std::function< int(const EventImp &)> Wrap(const std::function< int(const EventImp &)> &func)
void JsAlarm(const std::string &msg)