My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
HorizonsCommunicator.h
Go to the documentation of this file.
1 #ifndef GCPP_UTIL_HORIZONSCOMMUNICATOR_H
2 #define GCPP_UTIL_HORIZONSCOMMUNICATOR_H
3 
11 #include <list>
12 #include <map>
13 #include <string>
14 #include <sstream>
15 #include <fstream>
16 
17 #include "gcp/util/common/AntNum.h"
18 #include "gcp/util/common/Date.h"
19 #include "gcp/util/common/Communicator.h"
20 #include "gcp/util/common/GenericTask.h"
21 #include "gcp/util/common/GenericTaskMsg.h"
22 #include "gcp/util/common/SpawnableTask.h"
23 #include "gcp/util/common/String.h"
24 #include "gcp/util/common/TcpClient.h"
25 #include "gcp/util/common/TimeOut.h"
26 
27 #include "gcp/control/code/unix/libunix_src/common/genericregs.h"
28 
29 #define HORIZONS_FILENAME_MAX 100
30 #define HORIZONS_HANDLER(fn) void (fn)(void* args, std::string srcName, std::string fileName, bool error)
31 
32 namespace gcp {
33  namespace util {
34 
35  class SshTunnel;
36 
37  //------------------------------------------------------------
38  // A utility class for sending messages to the
39  // HorizonsCommunicator task
40  //------------------------------------------------------------
41 
43  public:
44 
45  enum MsgType {
46  GET_EPHEM,
47  CHECK_EPHEM,
48  ADD_HANDLER,
49  REM_HANDLER
50  };
51 
52  union {
53 
54  struct {
55  char fileName[HORIZONS_FILENAME_MAX];
56  char sourceId[SRC_LEN];
57  double mjdStart;
58  double mjdStop;
59  } getEphem;
60 
61  struct {
62  HORIZONS_HANDLER(*fn);
63  void* args;
64  } addHandler;
65 
66  struct {
67  HORIZONS_HANDLER(*fn);
68  } remHandler;
69 
70  } body;
71 
72  // A type for this message
73  MsgType type;
74  };
75 
76  //------------------------------------------------------------
77  // A utility class used to store handlers
78  //------------------------------------------------------------
79 
80  class Handler {
81  public:
82  HORIZONS_HANDLER(*fn_);
83  void* args_;
84  };
85 
86  //-----------------------------------------------------------------------
87  // Main class definition
88  //-----------------------------------------------------------------------
89 
91  public SpawnableTask<HorizonsCommunicatorMsg> {
92  public:
93 
94  // An enumerated type corresponding to the current state of the
95  // state machine
96 
97  enum CommState {
98  STATE_INIT, // Initial state
99  STATE_WAIT, // We are waiting to check if an ephemeris needs
100  // updating
101  STATE_COMM, // We are waiting for a response from the server
102  STATE_CHECK, // We are communicating with the server during an
103  // automated check of source ephemerides
104  };
105 
106  // Constructor.
107 
108  HorizonsCommunicator(unsigned intervalInSeconds=300,
109  bool useSshTunnel=false,
110  std::string gateway="");
111 
112 
113  HorizonsCommunicator(FdSet* fdSetPtr,
114  unsigned intervalInSeconds=300,
115  bool useSshTunnel=false,
116  std::string getway="");
117 
118  // Destructor.
119 
120  virtual ~HorizonsCommunicator();
121 
122  //------------------------------------------------------------
123  // Public control methods for this object
124  //------------------------------------------------------------
125 
126  // Fetch an ephemeris from the horizons server
127 
128  void getEphem(std::string source,
129  std::string fileName,
130  Date start, Date stop);
131 
132  // Add a callback function to be called whenever the ephemeris
133  // file is updated from the USNO server
134 
135  void addHandler(HORIZONS_HANDLER(*handler), void* args=0);
136 
137  // Remove a callback function from the list to be called
138 
139  void removeHandler(HORIZONS_HANDLER(*handler));
140 
141  // Register a source to be watched for ephemeris updates
142 
143  void registerEphemeris(std::string sourceName, std::string fileName);
144 
145  // Deregister a source to be watched for ephemeris updates
146 
147  void deregisterEphemeris(std::string sourceName);
148 
149  // Clear all ephemerides to be watched
150 
151  void clearEphemeris();
152 
153  void loadFile(std::string name);
154 
155  private:
156 
157  //------------------------------------------------------------
158  // Private variables pertaining to our connection with the server
159  //------------------------------------------------------------
160 
161  static const std::string horizonsHost_;
162  static const unsigned horizonsPort_;
163  bool useSshTunnel_;
164  std::string gateway_;
165  SshTunnel* tunnel_;
166 
167  CommState state_;
168 
169  //------------------------------------------------------------
170  // Variables for managing a list of sources to be checked for
171  // ephemeris updates
172  //------------------------------------------------------------
173 
174  // The map of sources registered with this object
175 
176  std::map<std::string, std::string> sourceMap_;
177 
178  // The map of sources that need ephemeris updates
179 
180  std::map<std::string, std::string> updateMap_;
181 
182  // Iterator to the current source needing an update
183 
184  std::map<std::string, std::string>::iterator updateMapIter_;
185 
186  // A mutex for protecting access to sourceMap_
187 
188  Mutex mapGuard_;
189 
190  //------------------------------------------------------------
191  // Internal variables pertaining to the current ephemeris being
192  // fetched
193  //------------------------------------------------------------
194 
195  std::string fileName_; // The output filename
196  std::string srcName_; // The source name
197  std::string horizonsSrcName_; // The HORIZONS name for this source
198  std::string interval_; // The ephemeris interval for this source
199  std::string startUtc_; // The start UTC of the ephemeris being fetched
200  std::string stopUtc_; // The start UTC of the ephemeris being fetched
201 
202  //------------------------------------------------------------
203  // Const variable of this class
204  //------------------------------------------------------------
205 
206  std::map<std::string, std::string> horizonsSrcNames_;
207  std::map<std::string, std::string> intervals_;
208 
209  // How long before the current ephemeris expires do we start
210  // trying to get a new one?
211 
212  double expiryThresholdInDays_;
213 
214  // How old can the current ephemeris file be before we start
215  // checking for a new one?
216 
217  double updateThresholdInDays_;
218 
219  //------------------------------------------------------------
220  // Variables pertaining to communications with the server
221  //------------------------------------------------------------
222 
223  FdSet* fdSetPtr_;
224 
225  TimeOut retryTimeOut_; // A timeout on which we will check the ephemeris file
226  TimeOut initialTimeOut_; // An initial timeout on startup, before checking
227  TimeOut commTimeOut_; // A timeout while communicating with the FTP server
228  struct timeval* timeOutPtr_; // A pointer to one of the above
229 
230  // A list of handlers to be called when the ephemeris is updated
231 
232  std::vector<Handler> handlers_;
233 
234  //-----------------------------------------------------------------------
235  // Generic methods
236  //-----------------------------------------------------------------------
237 
238  // Initialize pertinent members of this class to sensible
239  // defaults
240 
241  void initialize(bool useSshTunnel, std::string gateway);
242 
243  // Initialize the various timeouts used by this class
244 
245  void setupTimeOuts(unsigned timeOutIntervalInSeconds);
246 
247  // Advance to the requested state
248 
249  void stepState(CommState state);
250 
251  //-----------------------------------------------------------------------
252  // Private methods used in our select() loop
253  //-----------------------------------------------------------------------
254 
255  void serviceMsgQ();
256  void processMsg(HorizonsCommunicatorMsg* msg);
257 
258  // React to a timeoutu in select
259 
260  void registerTimeOut();
261 
262  // React to a failure of the server to reply during a
263  // communications session with the server
264 
265  void registerCommTimeOut();
266 
267  // Check connection to the gateway or server, and establish an
268  // ssh tunnel if necessary
269 
270  bool initiateSshConnection();
271 
272  // Terminate any ssh tunnel that may have been initiated
273 
274  void terminateSshConnection();
275 
276  // Terminate a command sequence to the horizons server
277 
278  void terminateCommSequence(bool error);
279 
280  //-----------------------------------------------------------------------
281  // Methods called in response to messages received on our message queue
282  //-----------------------------------------------------------------------
283 
284  void executeAddHandler(HORIZONS_HANDLER(*handler), void* args=0);
285  void executeRemoveHandler(HORIZONS_HANDLER(*handler));
286 
287  //-----------------------------------------------------------------------
288  // Methods used in communicating with the server
289  //-----------------------------------------------------------------------
290 
291  // Initiate sending commands to the horizons server
292 
293  void initiateGetEphemerisCommSequence(std::string body,
294  std::string fileName,
295  std::string startUtc,
296  std::string stopUtc);
297 
298  // Compile the state machine we will use for communicating with
299  // the horizons
300 
301  void compileGetEphemerisStateMachine(std::string& body,
302  std::string& startUtc,
303  std::string& stopUtc);
304 
305  // Callback function while reading ephemeris lines from the
306  // server
307 
308  static COMM_PARSER_FN(readEphemeris);
309  void readEphemeris(gcp::util::String& str);
310 
311  // Callback function when the server is done sending ephemeris
312  // lines
313 
314  static COMM_END_PARSER_FN(endEphemeris);
315  void endEphemeris();
316 
317  // Write a newly received ephemeris to a file
318 
319  void writeEphem();
320  void printHeader(std::ofstream& fout);
321  void printEphem(std::ofstream& fout);
322 
323  // Callback to disconnect from the server when we are done
324  // comomunicating with it
325 
326  static COMM_PARSER_FN(quitFromServer);
327  void quitFromServer();
328 
329  // Called to initiate communications with the server to get the
330  // ephemeris for the next source
331 
332  void checkNextSource();
333 
334  // Call any handler that were registered on succesful read of
335  // an ephemeris
336 
337  void callHandlers(bool error);
338 
339  //------------------------------------------------------------
340  // Private methods to set/access internal variables
341  //------------------------------------------------------------
342 
343  public:
344  void setFilename(std::string fileName);
345  void testFn();
346 
347  private:
348  void setSource(std::string src);
349 
350  void setEphemerisDates();
351 
352  std::string getSource();
353  std::string getFilename();
354  std::string getHorizonsSource();
355  std::string getInterval();
356 
357  //------------------------------------------------------------
358  // Ephemeris checking
359  //------------------------------------------------------------
360 
361  // Return true if any registered ephemeris file needs updating
362 
363  bool ephemerisNeedsUpdating();
364 
365  // Return true if the ephemeris file exists
366 
367  bool ephemExists(std::string ephemFileName);
368 
369  // Return true if the ephemeris is with expiryThresholdInDays_
370  // of running out
371 
372  bool ephemIsAboutToRunOut(std::string ephemFileName);
373 
374  // Return the MJD of the last entry in the ephemeris file
375 
376  double getLastEphemMjd(std::string ephemFileName);
377 
378  // Return true if the ephemeris hasn't been updated in more than
379  // updateThresholdInDays_
380 
381  bool ephemFileIsOutOfDate(std::string ephemFileName);
382 
383  }; // End class HorizonsCommunicator
384 
385  } // End namespace UTIL
386 } // End namespace gcp
387 
388 
389 
390 #endif // End #ifndef GCP_UTIL_HORIZONSCOMMUNICATOR_H
void deregisterEphemeris(std::string sourceName)
Definition: HorizonsCommunicator.cc:161
Definition: GenericTaskMsg.h:31
Definition: HorizonsCommunicator.h:42
Definition: SshTunnel.h:40
void getEphem(std::string source, std::string fileName, Date start, Date stop)
Definition: HorizonsCommunicator.cc:667
void setFilename(std::string fileName)
Definition: HorizonsCommunicator.cc:903
Definition: FdSet.h:16
Definition: SpawnableTask.h:31
Definition: HorizonsCommunicator.h:80
Definition: Communicator.h:26
Definition: TimeOut.h:20
Definition: Date.h:16
Definition: Mutex.h:16
Definition: String.h:16
void loadFile(std::string name)
Definition: HorizonsCommunicator.cc:1019
virtual ~HorizonsCommunicator()
Definition: HorizonsCommunicator.cc:132
void clearEphemeris()
Definition: HorizonsCommunicator.cc:171
void addHandler(HORIZONS_HANDLER(*handler), void *args=0)
Definition: HorizonsCommunicator.cc:645
HorizonsCommunicator(unsigned intervalInSeconds=300, bool useSshTunnel=false, std::string gateway="")
Definition: HorizonsCommunicator.cc:37
void registerEphemeris(std::string sourceName, std::string fileName)
Definition: HorizonsCommunicator.cc:150
Definition: HorizonsCommunicator.h:90