nds2-client - ClientAdministrator  0.16.8
 All Functions Typedefs Enumerations Enumerator Groups 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 
136  struct frequency_range
137  {
138  NDS::channel::sample_rate_type
139  minimum;
140  NDS::channel::sample_rate_type
141  maximum;
142 
143  frequency_range( )
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;
154  frequency_range&
155  operator=( const frequency_range& other ) = default;
156  };
157 
168  class channel_predicate_object
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 
174  channel_predicate_object( )
175  : epoch_( "", 0, NDS::buffer::GPS_INF ), glob_( ),
176  channel_types_( ), data_types_( ), sample_rates_( )
177  {
178  }
179 
180  channel_predicate_object( const channel_predicate_object& other ) =
181  default;
182 
183  channel_predicate_object( channel_predicate_object&& other ) =
184  default;
185 
186  channel_predicate_object&
187  operator=( const channel_predicate_object& other ) = default;
188 
189  channel_predicate_object&
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
217  channel_type_mask( ) const
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
275  min_sample_rate( ) const
276  {
277  return sample_rates_.minimum;
278  }
279 
282  NDS::channel::sample_rate_type
283  max_sample_rate( ) const
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 
317  channel_predicate_object&
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 
342  channel_predicate_object&
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 
373  channel_predicate_object&
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 
402  channel_predicate_object&
403  set( frequency_range val )
404  {
405  sample_rates_ = val;
406  return *this;
407  }
414  void
415  set_frequency_range(
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  }
427  channel_predicate_object&
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 
514  class connection
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 
530  class already_closed_error : public error
531  {
532  public:
533  DLL_EXPORT
534  already_closed_error( );
535  };
536 
541  class minute_trend_error : public error
542  {
543  public:
544  DLL_EXPORT
545  minute_trend_error( );
546  };
547 
551  class transfer_busy_error : public error
552  {
553  public:
554  DLL_EXPORT
555  transfer_busy_error( );
556  };
557 
561  class unexpected_channels_received_error : public error
562  {
563  public:
564  DLL_EXPORT
565  unexpected_channels_received_error( );
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  {
606  PROTOCOL_INVALID = -1,
607  PROTOCOL_ONE = 1,
608  PROTOCOL_TWO = 2,
609  PROTOCOL_TRY = 3
610  } protocol_type;
611 
612  typedef std::string host_type;
613  typedef int port_type;
614  typedef size_t count_type;
615 
619  typedef channel::channel_names_type channel_names_type;
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
719  count_type count_channels( const channel_predicate_object& pred );
720 
759  DLL_EXPORT channels_type
760  find_channels( const channel_predicate_object& pred );
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 
929  class parameters
930  {
931  public:
932  DLL_EXPORT parameters( );
933  DLL_EXPORT explicit parameters(
934  const connection::host_type& host,
935  connection::port_type port = connection::DEFAULT_PORT,
936  connection::protocol_type protocol = connection::PROTOCOL_TRY );
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 */