Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
DadaReadClient.h
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23 */
24 
25 #ifndef SKA_CHEETAH_PSRDADA_DADAREADCLIENT_H
26 #define SKA_CHEETAH_PSRDADA_DADAREADCLIENT_H
27 
28 #include "cheetah/psrdada/DadaClientBase.h"
29 #include "cheetah/psrdada/detail/RawBytes.h"
30 #include "cheetah/psrdada/detail/RawBytesReader.h"
31 #include "panda/Engine.h"
32 #include <memory>
33 #include <cstddef>
34 
35 
36 namespace ska {
37 namespace cheetah {
38 namespace psrdada {
62 {
63  typedef std::function<void(std::istream&, std::exception_ptr)> NextSequenceCallback;
64 
65  public:
83  DadaReadClient(key_t key, panda::Engine& engine,NextSequenceCallback const& next_sequence_callback);
84  DadaReadClient(DadaReadClient const&) = delete;
85  ~DadaReadClient();
86 
100  template <typename Iterator, typename DataType=typename std::iterator_traits<Iterator>::value_type>
101  Iterator& read(Iterator& begin, Iterator const& end);
102 
110  void next_sequence();
111 
112 
118  template <typename CallBackFunctor>
119  void next_sequence(CallBackFunctor callback);
120 
121  /*
122  * @brief stops the read client stream
123  */
124  void stop();
125 
126  /*
127  * @brief returns the state of the stream
128  */
129  inline std::shared_ptr<bool> stopped() const;
130 
131  /*
132  * @brief: start the read client stream
133  */
134  void start();
135 
136  protected:
137  template <typename CallBack>
138  void do_next_sequence(std::shared_ptr<bool> destructor_flag, CallBack callback);
139 
140  private:
141  std::unique_ptr<detail::RawBytesReader>& acquire_data_block();
142  void release_data_block();
143  void flush();
144  void lock();
145  void unlock();
146  void reset();
147  bool eod() const;
148  char* open_block_read(ipcio_t* ipc, std::uint64_t *cursz, std::uint64_t *blk_id);
149  char* ipcbuf_get_next_read (ipcbuf_t* id, uint64_t* bytes);
150 
151  private:
152  NextSequenceCallback _next_sequence_callback;
153  std::unique_ptr<detail::RawBytes> _current_block;
154  std::unique_ptr<detail::RawBytesReader> _current_reader;
155  bool _locked;
156  panda::Engine& _engine;
157  mutable std::shared_ptr<bool> _destructor_flag;
158  std::mutex _block_mutex;
159 };
160 
161 } // namespace psrdada
162 } // namespace cheetah
163 } // namespace ska
164 
165 #include "cheetah/psrdada/detail/DadaReadClient.cpp"
166 
167 #endif // SKA_CHEETAH_PSRDADA_DADAREADCLIENT_H
DadaReadClient(key_t key, panda::Engine &engine, NextSequenceCallback const &next_sequence_callback)
Instatiate new DadaReadClient.
A general base class for DADA readers and writers.
Some limits and constants for FLDO.
Definition: Brdz.h:35
Class that provides means for reading from a DADA ring buffer.
void next_sequence()
Move to the next sequence in the ring buffer.
Iterator & read(Iterator &begin, Iterator const &end)
Read from the data ring buffer.