FACT++  1.0
StateMachineDim.cc
Go to the documentation of this file.
1 // **************************************************************************
17 // **************************************************************************
18 #include "StateMachineDim.h"
19 
20 #include "tools.h"
21 
22 #include "EventDim.h"
23 #include "ServiceDim.h"
24 
25 using namespace std;
26 
27 const int StateMachineDim::fVersion = 42;
28 
29 // --------------------------------------------------------------------------
30 //
62 //
63 StateMachineDim::StateMachineDim(ostream &out, const std::string &name)
64  : DimLog(out, name), DimStart(name, DimLog::fLog), StateMachineImp(out, name),
65  fDescriptionStates(name+"/STATE_LIST", "C",
66  "Provides a list with descriptions for each service."
67  "|StateList[string]:A \\n separated list of the form id:name=description"),
68  fSrvState(name+"/STATE", "C",
69  "Provides the state of the state machine as quality of service."
70  "|Text[string]:A human readable string sent by the last state change.")
71  // fSrvVersion((name+"/VERSION").c_str(), const_cast<int&>(fVersion)),
72 {
74 }
75 
76 // --------------------------------------------------------------------------
77 //
84 EventImp *StateMachineDim::CreateEvent(const string &name, const string &fmt)
85 {
86  return new EventDim(GetName()+"/"+name, fmt, this);
87 }
88 
90 {
91  return new ServiceDim(name, this);
92 }
93 
94 // --------------------------------------------------------------------------
95 //
109 bool StateMachineDim::AddStateName(const int state, const std::string &name, const std::string &doc)
110 {
111  if (name.empty())
112  return false;
113 
114  const bool rc = HasState(state) || GetStateIndex(name)!=kSM_NotAvailable;
115 
116  StateMachineImp::AddStateName(state, name, doc);
117 
118  string str;
119  for (auto it=fStateNames.begin(); it!=fStateNames.end(); it++)
120  str += to_string(it->first)+':'+it->second.first+'='+it->second.second+'\n';
121 
123 
124  return rc;
125 }
126 
127 // --------------------------------------------------------------------------
128 //
135 string StateMachineDim::SetCurrentState(int state, const char *txt, const std::string &cmd)
136 {
137  const string msg = StateMachineImp::SetCurrentState(state, txt, cmd);
138  if (msg.empty())
139  return "";
140 
141  fSrvState.setQuality(state);
142  fSrvState.Update(msg);
143 
144  return msg;
145 }
146 
147 // --------------------------------------------------------------------------
148 //
152 {
153  dim_lock();
154 }
155 
156 // --------------------------------------------------------------------------
157 //
161 {
162  dim_unlock();
163 }
164 
166 {
167  DimInfo *inf = getInfo();
168  if (!inf)
169  return;
170 
171  const EventImp *evt = dynamic_cast<EventImp*>(inf);
172 
173  if (HasEvent(evt))
174  PostEvent(*evt);
175 }
176 
177 // --------------------------------------------------------------------------
178 //
193 //
195 {
196  DimCommand *cmd = getCommand();
197  if (!cmd)
198  return;
199 
200  const EventImp *evt = dynamic_cast<EventImp*>(cmd);
201 
202  if (HasEvent(evt))
203  PostEvent(*evt);
204 }
205 
206 // --------------------------------------------------------------------------
207 //
210 //
211 int StateMachineDim::Write(const Time &time, const string &txt, int qos)
212 {
213  return DimLog::fLog.Write(time, txt, qos);
214 }
215 
216 // --------------------------------------------------------------------------
217 //
225 //
227 {
228  Out() << " -- " << Time().GetAsStr() << " - EXIT(" << code << ") command received." << endl;
229  if (code<0) // negative values reserved for internal use
230  {
231  Out() << " -- " << Time().GetAsStr() << ": ignored." << endl;
232  return;
233  }
234 
235  Stop(code);
236  if (code==42)
237  exit(128);
238  if (code==0x42)
239  abort();
240 }
int GetStateIndex(const std::string &name) const
DimDescribedService fDescriptionStates
Version number.
virtual void Stop(int code=0)
Request to stop the mainloop.
void commandHandler()
Overwritten DimCommand::exitHandler.
A general base-class describing events issues in a state machine.
Definition: EventImp.h:11
Base class for a state machine implementation.
bool PostEvent(std::ostream &lout, const std::string &str)
Post an event to the event queue.
void setQuality(int quality)
Definition: discpp.cxx:1256
const std::string & GetName() const
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
StateMachineDim(std::ostream &out=std::cout, const std::string &name="DEFAULT")
void infoHandler()
Overwritten DimCommand::commandHandler.
int Write(const Time &t, const std::string &txt, int qos=kInfo)
Definition: MessageDim.cc:83
STL namespace.
Definition: dic.hxx:31
static const int fVersion
DimCommand * getCommand()
Definition: dis.hxx:41
std::ostream & Out() const
Definition: MessageImp.h:73
void exitHandler(int code)
DimService offering fCurrentState.
EventImp * CreateService(const std::string &name)
bool HasEvent(const EventImp *cmd)
DimDescribedService fSrvState
DimService propagating the state descriptions.
Concerete implementation of an EventImp as a DimCommand.
Definition: EventDim.h:24
uint16_t qos
Definition: HeadersGPS.h:29
int Write(const Time &time, const std::string &txt, int qos=kMessage)
Redirect our own logging to fLog.
void dim_unlock()
Definition: dim_thr.c:472
MessageDimTX fLog
Warning because the service this data corrsponds to might have been last updated longer ago than Local time
Definition: smartfact.txt:92
EventImp * CreateEvent(const std::string &name, const std::string &fmt)
Overwritten DimInfo::infoHandler.
bool HasState(int index) const
Ensures that the MessageDimTX is initialized before errors could be redirected to it...
Ensures calling DimServer::start() in its constructor and DimServer::stop() in its destructor...
void dim_lock()
Definition: dim_thr.c:455
StateNames fStateNames
Human readable names associated with the states.
virtual std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="")
Possible return value for GetStateIndex.
DimInfo * getInfo()
Definition: dic.hxx:26
virtual bool AddStateName(const int state, const std::string &name, const std::string &doc="")
std::string GetAsStr(const char *fmt="%Y-%m-%d %H:%M:%S") const
Definition: Time.cc:240
bool AddStateName(const int state, const std::string &name, const std::string &doc="")
std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="")