My Project
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
IersCommunicator.h
Go to the documentation of this file.
1 #ifndef GCP_UTIL_IERSCOMMUNICATOR_H
2 #define GCP_UTIL_IERSCOMMUNICATOR_H
3 
11 #include <list>
12 #include <string>
13 #include <sstream>
14 
15 #include "gcp/util/common/Communicator.h"
16 #include "gcp/util/common/FdSet.h"
17 #include "gcp/util/common/GenericTask.h"
18 #include "gcp/util/common/GenericTaskMsg.h"
19 #include "gcp/util/common/SpawnableTask.h"
20 #include "gcp/util/common/String.h"
21 #include "gcp/util/common/TcpClient.h"
22 #include "gcp/util/common/TimeOut.h"
23 
24 #define IERS_HANDLER(fn) void (fn)(void* args, std::string fileName)
25 #define IERS_FILENAME_MAX 100
26 
27 namespace gcp {
28  namespace util {
29 
30  //------------------------------------------------------------
31  // A utility class for sending messages to the ModemPager task
32  //------------------------------------------------------------
33 
35  public:
36 
37  enum MsgType {
38  GET_FILE,
39  UPDATE_FILENAME,
40  ADD_HANDLER,
41  REM_HANDLER
42  };
43 
44  union {
45 
46  bool enable;
47 
48  char fileName[IERS_FILENAME_MAX+1];
49 
50  struct {
51  IERS_HANDLER(*fn);
52  void* args;
53  } addHandler;
54 
55  struct {
56  IERS_HANDLER(*fn);
57  } remHandler;
58 
59  } body;
60 
61  // A type for this message
62 
63  MsgType type;
64  };
65 
66  //------------------------------------------------------------
67  // A utility class used to store handlers
68  //------------------------------------------------------------
69 
70  class Handler {
71  public:
72  IERS_HANDLER(*fn_);
73  void* args_;
74  };
75 
76  //------------------------------------------------------------
77  // Main class definition
78  //------------------------------------------------------------
79 
81  public SpawnableTask<IersCommunicatorMsg> {
82  public:
83 
84  // An enumerated type corresponding to the current state of the
85  // state machine
86 
87  enum CommState {
88  STATE_INIT, // Initial state
89  STATE_WAIT, // We are waiting to check if an ephemeris needs updating
90  STATE_COMM, // We are waiting for a response from the ftp server
91  };
92 
93  //-----------------------------------------------------------------------
94  // Methods for use of this object in a stand-alone thread
95  //-----------------------------------------------------------------------
96 
97  // Constructors for internal use of this class (ie, run in its
98  // own thread). If timeOutIntervalInSeconds is non-zero, this
99  // object will automatically retrieve the UT1-UTC ephemeris on
100  // the specified interval. Else it will do nothing until told
101  // to retrieve it.
102 
103  IersCommunicator(std::string fullPathToOutputFile,
104  unsigned timeOutIntervalInSeconds=0);
105 
106  IersCommunicator(std::string outputDir,
107  std::string outputFileName,
108  unsigned timeOutIntervalInSeconds=0);
109 
110  // Destructor.
111 
112  virtual ~IersCommunicator();
113 
114  //------------------------------------------------------------
115  // Public control methods for this object
116  //------------------------------------------------------------
117 
118  // Tell this object to retrieve the IERS bulletin
119 
120  void getIersBulletin();
121 
122  // Update the ephemeris file that this object will check
123 
124  void updateEphemerisFileName(std::string fileName);
125 
126  // Add a callback function to be called whenever the ephemeris
127  // file is updated from the USNO server
128 
129  void addHandler(IERS_HANDLER(*handler), void* args=0);
130 
131  // Remove a callback function from the list to be called
132 
133  void removeHandler(IERS_HANDLER(*handler));
134 
135  //------------------------------------------------------------
136  // Query functions
137  //------------------------------------------------------------
138 
139  // Get the ephemeris file name
140 
141  std::string ephemFileName();
142 
143  //------------------------------------------------------------
144  // Test functions
145  //------------------------------------------------------------
146 
147  // Load a ser7.dat file to be converted
148 
149  void loadFile(std::string name);
150 
151  private:
152 
153  // The current state of the state machine
154 
155  CommState state_;
156 
157  // A list of handlers to be called when the ephemeris is updated
158 
159  std::vector<Handler> handlers_;
160 
161  // How long before the current ephemeris expires do we start
162  // trying to get a new one?
163 
164  double expiryThresholdInDays_;
165 
166  // How old can the current ephemeris file be before we start
167  // checking for a new one?
168 
169  double updateThresholdInDays_;
170 
171  // An output stream used to store responses from the FTP server
172 
173  std::ostringstream ftpOs_;
174 
175  // The name of the file containing the ut1-utc ephemeris
176 
177  std::string outputFileName_;
178 
179  // A client for communicating wth the remote FTP server
180 
181  gcp::util::TcpClient* ftpClient_;
182 
183  // A pointer to an FdSet object, possibly external
184 
185  gcp::util::FdSet* fdSetPtr_;
186 
187  // Timeouts used by this class
188 
189  TimeOut retryTimeOut_; // A timeout on which we will check the ephemeris file
190  TimeOut initialTimeOut_; // An initial timeout on startup, before checking
191  TimeOut commTimeOut_; // A timeout while communicating with the FTP server
192  struct timeval* timeOutPtr_; // A pointer to one of the above
193 
194  //-----------------------------------------------------------------------
195  // Generic methods
196  //-----------------------------------------------------------------------
197 
198  // Initialize pertinent members of this class to sensible
199  // defaults
200 
201  void initialize(std::string fullPathToOutputFile);
202  void initialize(std::string outputDir, std::string outputFileName);
203 
204  // Initialize the various timeouts used by this class
205 
206  void setupTimeOuts(unsigned timeOutIntervalInSeconds);
207 
208  // Advance to the requested state
209 
210  void stepState(CommState state);
211 
212  //-----------------------------------------------------------------------
213  // Methods called in response to messages received on our message queue
214  //-----------------------------------------------------------------------
215 
216  void executeAddHandler(IERS_HANDLER(*handler), void* args=0);
217  void executeRemoveHandler(IERS_HANDLER(*handler));
218 
219  //-----------------------------------------------------------------------
220  // Run methods used by this class
221  //-----------------------------------------------------------------------
222 
223  void serviceMsgQ();
224  void processMsg(IersCommunicatorMsg* msg);
225 
226  // React to a timeout in select
227 
228  void registerTimeOut();
229 
230  // Return the fd corresponding to the communications connection
231  // to the FTP server
232 
233  int getFtpCommFd();
234 
235  // Return the fd corresponding to the data connection to the FTP
236  // server
237 
238  int getFtpDataFd();
239 
240  //-----------------------------------------------------------------------
241  // Private methods used to communicate with the USNO server
242  //-----------------------------------------------------------------------
243 
244  // Compile the list of communication/responses needed for
245  // retrieving the tipper log from the remote FTP server
246 
247  void compileGetIersBulletinStateMachine();
248 
249  // Initiate the comms sequence to retrieve the tipper log from
250  // the remote server
251 
252  void initiateGetIersBulletinCommSequence();
253 
254  // Read a line from the ftp server and determine what to do
255 
256  void concatenateString(std::ostringstream& os);
257 
258  // Process data received from the remote FTP server
259 
260  void processIersBulletin();
261 
262  // Parse a data port number received from the FTP server
263 
264  static COMM_PARSER_FN(parsePortNumber);
265  void parsePortNumber();
266 
267  // Quit from the FTP server.
268 
269  static COMM_PARSER_FN(quitFromServer);
270  void quitFromServer(bool error);
271 
272  // Terminate a connection with the FTP server
273 
274  void terminateFtpConnection(bool error);
275 
276  // React to a failure to reply from the FTP server once a
277  // connection has been established
278 
279  void registerCommTimeOut();
280 
281  //-----------------------------------------------------------------------
282  // Methods used to write a gcp-style ephemeris file
283  //-----------------------------------------------------------------------
284 
285  void writeEphem();
286  void printHeader(std::ofstream& fout);
287  void printEphem(std::ofstream& fout);
288 
289  //-----------------------------------------------------------------------
290  // Methods used to determine if the local ephemeris needs updating
291  //-----------------------------------------------------------------------
292 
293  // Return true if the UT1-UTC ephemeris needs updating
294 
295  bool ephemerisNeedsUpdating();
296 
297  // Return true if it is safe to update the ephemeris file.
298 
299  bool safeToUpdate();
300 
301  // Return true if the UT1-UTC ephemeris file exists
302 
303  bool ephemExists();
304 
305  // Return true if the ephemeris is with expiryThresholdInDays_
306  // of running out
307 
308  bool ephemIsAboutToRunOut();
309 
310  // Return the MJD of the last entry in the ephemeris file
311 
312  double getLastEphemMjd();
313 
314  // Return true if the ephemeris hasn't been updated in more than
315  // updateThresholdInDays_
316 
317  bool ephemFileIsOutOfDate();
318 
319  void callHandlers();
320 
321  }; // End class IersCommunicator
322 
323  } // End namespace util
324 } // End namespace gcp
325 
326 
327 
328 #endif // End #ifndef GCP_UTIL_IERSCOMMUNICATOR_H
Definition: GenericTaskMsg.h:31
void loadFile(std::string name)
Definition: IersCommunicator.cc:606
void getIersBulletin()
Definition: IersCommunicator.cc:466
virtual ~IersCommunicator()
Definition: IersCommunicator.cc:113
Definition: FdSet.h:16
Definition: SpawnableTask.h:31
Definition: HorizonsCommunicator.h:80
Definition: Communicator.h:26
Definition: TimeOut.h:20
Definition: IersCommunicator.h:80
Definition: TcpClient.h:18
void updateEphemerisFileName(std::string fileName)
Definition: IersCommunicator.cc:501
IersCommunicator(std::string fullPathToOutputFile, unsigned timeOutIntervalInSeconds=0)
Definition: IersCommunicator.cc:40
std::string ephemFileName()
Definition: IersCommunicator.cc:700
Definition: IersCommunicator.h:34
void addHandler(IERS_HANDLER(*handler), void *args=0)
Definition: IersCommunicator.cc:478