FACT++  1.0
int StateMachineTimeCheck::Execute ( )
inlineprivatevirtual

Is called continously to execute actions in the current state.

This is what the state machine is doing in a certain state continously. In an idle state this might just be doing nothing.

In the tracking state of the drive system this might be sending new command values to the drive based on its current position.

The current state of the state machine can be accessed by GetCurrentState()

Returns
Usually it should just return the current state. However, sometimes execution might lead to a new state, e.g. when a hardware error is detected. In this case a new state can be returned to put the state machine into a different state. Note, that the function is responsible of doing all actions connected with the state change itself. If not overwritten it returns the current status.

Reimplemented from StateMachineImp.

Definition at line 36 of file timecheck.cc.

References Error(), Tools::Trim(), and DimDescribedService::Update().

37  {
38  Time now;
39  if (now-fLastUpdate<boost::posix_time::minutes(fInterval))
40  return kStateRunning;
41 
42  fLastUpdate=now;
43 
44  const string cmd = "ntpdate -q "+fServer;
45 
46  Info("Calling '"+cmd+"'");
47 
48  // !!!!! Warning: this is a blocking operation !!!!!
49  FILE *pipe = popen(cmd.c_str(), "r");
50  if (!pipe)
51  {
52  const string err = strerror(errno);
53  Error("Could not create pipe '"+cmd+"': "+err);
54  return 0x100;
55  }
56 
57  vector<string> args;
58 
59  string line;
60  while (1)
61  {
62  const int rc = fgetc(pipe);
63  if (rc==EOF || rc=='\n')
64  {
65  args.push_back(Tools::Trim(line));
66  break;
67  }
68 
69  if (rc==',')
70  {
71  args.push_back(Tools::Trim(line));
72  line = "";
73  continue;
74  }
75 
76  line += static_cast<unsigned char>(rc);
77  }
78  pclose(pipe);
79 
80  if (args.size()!=4)
81  {
82  Error("First returned line contains other than four arguments (separated by commas)");
83  return 0x100;
84  }
85 
86  if (args[2].substr(0, 7)!="offset ")
87  {
88  Error("Argument 3 '"+args[2]+"' is not what it ought to be.");
89  return 0x100;
90  }
91 
92  try
93  {
94  const float offset = stof(args[2].substr(7));
95  fService.Update(offset);
96 
97  const string msg = "NTP: "+fServer+" returned: "+args[2]+" ms";
98 
99  if (offset>=1000)
100  {
101  Warn(msg);
102  return kStateOutOfRange;
103  }
104 
105  Message(msg);
106 
107  }
108  catch (const exception &e)
109  {
110  Error("Converting offset '"+args[2]+"' to float failed: "+e.what());
111  return 0x100;
112  }
113 
114  return kStateRunning;
115  }
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
DimDescribedService fService
Definition: timecheck.cc:26
int Error(const std::string &str)
Definition: MessageImp.h:49
int Warn(const std::string &str)
Definition: MessageImp.h:48
int Info(const std::string &str)
Definition: MessageImp.h:47
int Message(const std::string &str)
Definition: MessageImp.h:46
std::string Trim(const std::string &str)
Definition: tools.cc:68

+ Here is the call graph for this function: