nds2-client - ClientUser  0.16.8
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
nds_connection.hh
1 /* -*- mode: C++ ; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 
3 /*
4  * High-level NDS client C++ library
5  *
6  * Copyright (C) 2015 Edward Maros <ed.maros@ligo.org>
7  *
8  * This work is derived from the C NDS clientlibrary created by Leo Singer
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef SWIG__COMMON__NDS_CONNECTION_HH
26 #define SWIG__COMMON__NDS_CONNECTION_HH
27 
111 #include <memory>
112 #include <stdexcept>
113 #include <string>
114 #include <ostream>
115 
116 #include "nds_export.hh"
117 #include "nds_buffer.hh"
118 #include "nds_channel.hh"
119 #include "nds_data_iterator.hh"
120 #include "nds_availability.hh"
121 #include "nds_epoch.hh"
122 
123 namespace NDS
124 {
125  namespace detail
126  {
127  struct conn_p_type;
128  class parameter_block;
129  class parameter_accessor;
130  } // namespace detail
131  inline namespace abi_0
132  {
133  class parameters;
134 
137  {
138  NDS::channel::sample_rate_type
139  minimum;
140  NDS::channel::sample_rate_type
142 
144  : minimum( NDS::channel::MIN_SAMPLE_RATE ),
145  maximum( NDS::channel::MAX_SAMPLE_RATE )
146  {
147  }
148  frequency_range( NDS::channel::sample_rate_type Min,
149  NDS::channel::sample_rate_type Max )
150  : minimum( Min ), maximum( Max )
151  {
152  }
153  frequency_range( const frequency_range& other ) = default;
155  operator=( const frequency_range& other ) = default;
156  };
157 
169  {
170  public:
171  typedef std::vector< NDS::channel::channel_type > channel_type_list;
172  typedef std::vector< NDS::channel::data_type > data_type_list;
173 
175  : epoch_( "", 0, NDS::buffer::GPS_INF ), glob_( ),
176  channel_types_( ), data_types_( ), sample_rates_( )
177  {
178  }
179 
181  default;
182 
184  default;
185 
187  operator=( const channel_predicate_object& other ) = default;
188 
190  operator=( channel_predicate_object&& other ) = default;
191 
193  std::string
194  glob( ) const
195  {
196  return ( glob_.empty( ) ? "*" : glob_ );
197  }
198 
202  const channel_type_list
203  channel_types( ) const
204  {
205 
206  if ( channel_types_.empty( ) )
207  {
208  channel_type_list result{ all_channel_types };
209  return result;
210  }
211  return channel_types_;
212  }
213 
216  NDS::channel::channel_type
218  {
219  if ( channel_types_.empty( ) )
220  {
221  return NDS::channel::DEFAULT_CHANNEL_MASK;
222  }
223  NDS::channel::channel_type result;
224  for ( const NDS::channel::channel_type val : channel_types_ )
225  {
226  result = ( NDS::channel::channel_type )( result | val );
227  }
228  return result;
229  }
230 
234  const data_type_list
235  data_types( ) const
236  {
237  if ( data_types_.empty( ) )
238  {
239  data_type_list result{ all_data_types };
240  return result;
241  }
242  return data_types_;
243  }
244 
247  NDS::channel::data_type
248  data_type_mask( ) const
249  {
250  if ( data_types_.empty( ) )
251  {
252  return NDS::channel::DEFAULT_DATA_MASK;
253  }
254  NDS::channel::data_type result;
255  for ( const NDS::channel::data_type val : data_types_ )
256  {
257  result = ( NDS::channel::data_type )( result | val );
258  }
259  return result;
260  }
261 
266  const frequency_range&
267  sample_rates( ) const
268  {
269  return sample_rates_;
270  }
271 
274  NDS::channel::sample_rate_type
276  {
277  return sample_rates_.minimum;
278  }
279 
282  NDS::channel::sample_rate_type
284  {
285  return sample_rates_.maximum;
286  }
287 
290  const epoch&
291  time_span( ) const
292  {
293  return epoch_;
294  }
295 
298  NDS::buffer::gps_second_type
299  gps_start( ) const
300  {
301  return epoch_.gps_start;
302  }
303 
306  NDS::buffer::gps_second_type
307  gps_stop( ) const
308  {
309  return epoch_.gps_stop;
310  }
311 
318  set( std::string val )
319  {
320  glob_ = std::move( val );
321  return *this;
322  }
323 
329  void
330  set_glob( std::string val )
331  {
332  set( val );
333  }
334 
343  set( NDS::channel::channel_type val )
344  {
345  {
346  for ( auto entry : all_channel_types )
347  if ( (int)entry | (int)val )
348  {
349  channel_types_.push_back( val );
350  }
351  }
352  return *this;
353  }
354 
360  void
361  set_channel_type_mask( NDS::channel::channel_type val )
362  {
363  set( val );
364  }
365 
374  set( NDS::channel::data_type val )
375  {
376  for ( auto entry : all_data_types )
377  {
378  if ( (int)entry | (int)val )
379  {
380  data_types_.push_back( val );
381  }
382  }
383  return *this;
384  }
385 
391  void
392  set_data_type_mask( NDS::channel::data_type val )
393  {
394  set( val );
395  }
396 
404  {
405  sample_rates_ = val;
406  return *this;
407  }
414  void
416  NDS::channel::sample_rate_type min_sample_rate,
417  NDS::channel::sample_rate_type max_sample_rate )
418  {
419  frequency_range range( min_sample_rate, max_sample_rate );
420  set( range );
421  }
428  set( epoch val )
429  {
430  epoch_ = val;
431  return *this;
432  }
440  void
441  set_timespan( NDS::buffer::gps_second_type gps_start,
442  NDS::buffer::gps_second_type gps_stop )
443  {
444  NDS::epoch e( "", gps_start, gps_stop );
445  set( e );
446  }
447 
448  private:
449  DLL_EXPORT const static data_type_list all_data_types;
450  DLL_EXPORT const static channel_type_list all_channel_types;
451 
452  epoch epoch_;
453  std::string glob_;
454  channel_type_list channel_types_;
455  data_type_list data_types_;
456  frequency_range sample_rates_;
457  };
458 
459  inline void
460  build_channel_predicate( channel_predicate_object& pred )
461  {
462  }
463 
464  template < typename T >
465  void
466  build_channel_predicate( channel_predicate_object& pred, T val )
467  {
468  pred.set( val );
469  }
470 
471  template < typename T, typename... Args >
472  void
473  build_channel_predicate( channel_predicate_object& pred,
474  T val,
475  Args... args )
476  {
477  pred.set( val );
478  build_channel_predicate( pred, args... );
479  }
480 
491  template < typename... Args >
492  channel_predicate_object
493  channel_predicate( Args... args )
494  {
495  channel_predicate_object pred;
496  build_channel_predicate( pred, args... );
497  return pred;
498  }
499 
515  {
516  public:
520  class error : public std::runtime_error
521  {
522  public:
523  DLL_EXPORT
524  error( const std::string& What, int ErrNo = 0 );
525  };
526 
531  {
532  public:
533  DLL_EXPORT
535  };
536 
541  class minute_trend_error : public error
542  {
543  public:
544  DLL_EXPORT
546  };
547 
551  class transfer_busy_error : public error
552  {
553  public:
554  DLL_EXPORT
556  };
557 
562  {
563  public:
564  DLL_EXPORT
566  };
567 
571  class daq_error : public error
572  {
573  public:
574  DLL_EXPORT
575  explicit daq_error( int daq_code );
576 
577  DLL_EXPORT
578  daq_error( int daq_code,
579  const std::string& additional_information );
580 
581  DLL_EXPORT
582  daq_error( int daq_code, const char* additional_information );
583 
584  DLL_EXPORT
585  inline int
586  DAQCode( ) const
587  {
588  return error_code;
589  }
590 
591  private:
592  int index;
593  int error_code;
594 
595  static int counter_;
596 
597  static std::string format( int DAQCode,
598  const char* extra = nullptr );
599  };
600 
604  typedef enum
605  {
610  } protocol_type;
611 
612  typedef std::string host_type;
613  typedef int port_type;
614  typedef size_t count_type;
615 
620  typedef std::vector< std::string > parameters_type;
621 
622  static const port_type DEFAULT_PORT =
623  31200;
624 
628  DLL_EXPORT
629  connection( ) = delete;
630 
639  DLL_EXPORT
640  explicit connection( const host_type& host,
641  port_type port = DEFAULT_PORT,
642  protocol_type protocol = PROTOCOL_TRY );
643 
652  DLL_EXPORT
653  explicit connection( const NDS::parameters& params );
654 
658  connection( const NDS::connection& other ) = delete;
659 
663  connection& operator=( const NDS::connection& other ) = delete;
664 
668  DLL_EXPORT
669  ~connection( );
670 
674  DLL_EXPORT
675  void close( );
676 
718  DLL_EXPORT
720 
759  DLL_EXPORT channels_type
761 
773  DLL_EXPORT epochs_type get_epochs( );
774 
789  DLL_EXPORT availability_list_type
790  get_availability( const epoch& time_span,
791  const channel_names_type& channel_names );
792 
808  DLL_EXPORT bool check( buffer::gps_second_type gps_start,
809  buffer::gps_second_type gps_stop,
810  const channel_names_type& channel_names );
811 
830  DLL_EXPORT buffers_type
831  fetch( buffer::gps_second_type gps_start,
832  buffer::gps_second_type gps_stop,
833  const channel_names_type& channel_names );
834 
840  DLL_EXPORT NDS::parameters& parameters( ) const;
841 
853  DLL_EXPORT const channel::hash_type& hash( ) const;
854 
904  DLL_EXPORT NDS::data_iterable
905  iterate( NDS::request_period period,
906  const channel_names_type& channel_names );
907 
915  DLL_EXPORT bool request_in_progress( ) const;
916 
917  private:
918  std::shared_ptr< detail::conn_p_type > p_;
919  };
920 
921  DLL_EXPORT extern std::ostream& operator<<( std::ostream& os,
922  const connection& conn );
923 
930  {
931  public:
932  DLL_EXPORT parameters( );
933  DLL_EXPORT explicit parameters(
937 
938  DLL_EXPORT parameters( const parameters& other );
939  DLL_EXPORT parameters( parameters&& other ) noexcept;
940 
941  DLL_EXPORT parameters& operator=( const parameters& other );
942  DLL_EXPORT parameters& operator=( parameters&& other ) noexcept;
943 
944  DLL_EXPORT ~parameters( );
945 
950  DLL_EXPORT connection::host_type host( ) const;
956  DLL_EXPORT connection::port_type port( ) const;
961  DLL_EXPORT connection::protocol_type protocol( ) const;
962 
974  DLL_EXPORT std::string get( const std::string& key ) const;
975 
1089  DLL_EXPORT bool set( const std::string& key,
1090  const std::string& value );
1091 
1097  DLL_EXPORT connection::parameters_type parameter_list( ) const;
1098 
1099  private:
1100  friend class NDS::detail::parameter_accessor;
1101  std::unique_ptr< detail::parameter_block > p_;
1102  };
1103  } // namespace abi_0
1104 } // namespace NDS
1105 
1106 #endif /* SWIG__COMMON__NDS_CONNECTION_HH */
std::vector< std::string > channel_names_type
A vector of channel names.
Definition: nds_channel.hh:104
NDS::channel::sample_rate_type max_sample_rate() const
return the maximum sample rate to be selected. This is also available through the sample_rates method...
Definition: nds_connection.hh:283
NDS::buffer::gps_second_type gps_stop() const
Return the gps stop time that constrains this query. Note this is available via the time_span method...
Definition: nds_connection.hh:307
Signal that the connection has already been closed.
Definition: nds_connection.hh:530
DLL_EXPORT availability_list_type get_availability(const epoch &time_span, const channel_names_type &channel_names)
Given a list of channels return their availability over the current epoch.
Definition: nds_connection.cc:222
A list of availabilitys (channels, availability segments)
Definition: nds_availability.hh:126
DLL_EXPORT ~connection()
Destroy the connection.
Base class for NDS errors.
Definition: nds_connection.hh:520
static const port_type DEFAULT_PORT
Default NDS2 port number.
Definition: nds_connection.hh:622
const frequency_range & sample_rates() const
return the range of sample rates that are to be allowed in the query. defaults to (NDS::channel::MIN_...
Definition: nds_connection.hh:267
DLL_EXPORT void close()
Close the connection.
Definition: nds_connection.cc:216
Unknown or invalid connection.
Definition: nds_connection.hh:606
DLL_EXPORT bool check(buffer::gps_second_type gps_start, buffer::gps_second_type gps_stop, const channel_names_type &channel_names)
Check to see if data is avaiable.
Definition: nds_connection.cc:230
std::string host_type
Host name type.
Definition: nds_connection.hh:612
void set_channel_type_mask(NDS::channel::channel_type val)
Set the channel type mask.
Definition: nds_connection.hh:361
void set_frequency_range(NDS::channel::sample_rate_type min_sample_rate, NDS::channel::sample_rate_type max_sample_rate)
Set the [min, max) frequency range that limits this query.
Definition: nds_connection.hh:415
A transfer is already in progress.
Definition: nds_connection.hh:551
A predicate object to be used in limiting the number of channels returned when searching for channels...
Definition: nds_connection.hh:168
const data_type_list data_types() const
return a container of distinct data types that are to be matched (defaults to an expansion of NDS::ch...
Definition: nds_connection.hh:235
DLL_EXPORT channels_type find_channels(const channel_predicate_object &pred)
Retrieve a list of channels.
Definition: nds_connection.cc:272
channel_predicate_object & set(std::string val)
Set the glob string used in channel matching.
Definition: nds_connection.hh:318
NDS::channel::channel_type channel_type_mask() const
return a compact mask of the channel types selected. this is mainly provided for the SWIG language bi...
Definition: nds_connection.hh:217
DLL_EXPORT bool set(const std::string &key, const std::string &value)
Change the default behavior of the connection.
Definition: nds_connection.cc:144
frequency_range()
The maximum frequency in this range.
Definition: nds_connection.hh:143
Signals that a minute trend has been requested but the start/stop times are not divisible by 60...
Definition: nds_connection.hh:541
NDS::channel::sample_rate_type min_sample_rate() const
return the minimum sample rate to be selected. This is also available through the sample_rates method...
Definition: nds_connection.hh:275
channel_predicate_object & set(NDS::channel::channel_type val)
Set a channel type to search for.
Definition: nds_connection.hh:343
DLL_EXPORT buffers_type fetch(buffer::gps_second_type gps_start, buffer::gps_second_type gps_stop, const channel_names_type &channel_names)
Retreive data from the server.
Definition: nds_connection.cc:240
DLL_EXPORT NDS::data_iterable iterate(NDS::request_period period, const channel_names_type &channel_names)
Retreive data in segments.
long gps_second_type
Type second portion of a gps time.
Definition: nds_buffer.hh:33
channel_predicate_object & set(epoch val)
Definition: nds_connection.hh:428
DLL_EXPORT connection::parameters_type parameter_list() const
Return a list of supported parameters.
Definition: nds_connection.cc:150
NDS::channel::sample_rate_type maximum
The minimum frequency in this range.
Definition: nds_connection.hh:141
int port_type
Host port number type.
Definition: nds_connection.hh:613
std::string glob() const
Return the channel name glob (defaults to &quot;*&quot;)
Definition: nds_connection.hh:194
DLL_EXPORT connection::host_type host() const
Return the host that is currently connected/configured.
Definition: nds_connection.cc:120
A simple range for sample rates.
Definition: nds_connection.hh:136
channel_predicate_object & set(frequency_range val)
Definition: nds_connection.hh:403
Connect with NDS1 protocol.
Definition: nds_connection.hh:607
size_t count_type
Generic count time.
Definition: nds_connection.hh:614
NDS::channel::data_type data_type_mask() const
return a compact mask of the data types selected. this is mainly provided for the SWIG language bindi...
Definition: nds_connection.hh:248
an error state was returned by the NDS server
Definition: nds_connection.hh:571
The hash_type represents a channel hash. The channel hash is an hash of the NDS2 channel list...
Definition: nds_channel.hh:71
DLL_EXPORT const channel::hash_type & hash() const
Return the NDS2 channel hash.
Definition: nds_connection.cc:290
An unexpected channel or data was received.
Definition: nds_connection.hh:561
DLL_EXPORT epochs_type get_epochs()
Return a list of epochs.
Definition: nds_connection.cc:298
DLL_EXPORT NDS::parameters & parameters() const
Return the parameter/configuration object.
Definition: nds_connection.cc:284
DLL_EXPORT bool request_in_progress() const
Query the connection to see if a request is in progress.
Definition: nds_connection.cc:318
channel::channel_names_type channel_names_type
A list of channel names.
Definition: nds_connection.hh:619
void set_timespan(NDS::buffer::gps_second_type gps_start, NDS::buffer::gps_second_type gps_stop)
Set the [gps_start, gps_stop) time range that limits this query.
Definition: nds_connection.hh:441
DLL_EXPORT connection::port_type port() const
Return the port number that is currently connected/configured.
Definition: nds_connection.cc:126
NDS::buffer::gps_second_type gps_start() const
Return the gps start time that constrains this query. Note this is available via the time_span method...
Definition: nds_connection.hh:299
void set_data_type_mask(NDS::channel::data_type val)
Set the data type mask.
Definition: nds_connection.hh:392
channel_predicate_object & set(NDS::channel::data_type val)
Set a data type to search for.
Definition: nds_connection.hh:374
A connection to the NDS/NDS2 server.
Definition: nds_connection.hh:514
Represents a LIGO data channel.
Definition: nds_channel.hh:33
const channel_type_list channel_types() const
return a container of distinct channel types that are to be matched (defaults to an expansion of NDS:...
Definition: nds_connection.hh:203
The parameters class holds the configuration for a connection. This includes gap handling strategy an...
Definition: nds_connection.hh:929
const epoch & time_span() const
return the timespan/epoch that should constrain the query defaults to [0, NDS::buffer::GPS_INF) ...
Definition: nds_connection.hh:291
Autodetect server protocol.
Definition: nds_connection.hh:609
protocol_type
Connection constants.
Definition: nds_connection.hh:604
DLL_EXPORT connection::protocol_type protocol() const
Return the protocol version configured/in use.
Definition: nds_connection.cc:132
void set_glob(std::string val)
Set the glob string to match on.
Definition: nds_connection.hh:330
Connect with NDS2 protocol.
Definition: nds_connection.hh:608
DLL_EXPORT connection()=delete
Default constructor.
connection & operator=(const NDS::connection &other)=delete
Connection objects are not copyable.
DLL_EXPORT count_type count_channels(const channel_predicate_object &pred)
Return the count of channels matching the given predicate.
Definition: nds_connection.cc:250