Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
SpCclSpeadReader.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 #ifndef SKA_CHEETAH_EXPORTERS_SPCCLSPEADREADER_H
25 #define SKA_CHEETAH_EXPORTERS_SPCCLSPEADREADER_H
26 
27 #ifdef ENABLE_SPEAD
28 
29 #include "detail/SpCclSpeadStreamTraits.h"
30 #include "SpCclSpeadReaderConfig.h"
31 #include "cheetah/data/SpCandidateData.h"
32 #include "pss/astrotypes/utils/TypeTraits.h"
33 #include <spead2/common_defines.h>
34 #include <spead2/recv_heap.h>
35 #include <spead2/recv_udp.h>
36 #include <functional>
37 #include <memory>
38 #include <vector>
39 #include <mutex>
40 
41 namespace ska {
42 namespace cheetah {
43 namespace exporters {
44 
53 template<typename TimeFrequencyT, typename Callback=std::function<void(std::shared_ptr<data::SpCandidateData<TimeFrequencyT>>)>
54  , typename DataFactory=std::function<data::SpCandidateData<TimeFrequencyT>*()>>
55 class SpCclSpeadReader : public SpCclSpeadStreamTraits
56 {
57  private:
58  typedef TimeFrequencyT TimeFrequencyType;
59  typedef data::SpCandidateData<TimeFrequencyType> SpCclType;
60  typedef typename TimeFrequencyType::FrequencyType FrequencyType;
61  typedef typename TimeFrequencyType::TimeType TimeType;
62  typedef typename TimeFrequencyType::DataType NumericalRep;
63 
64  class Stream : public spead2::recv::stream
65  {
66  typedef spead2::recv::stream BaseT;
67 
68  public:
69  Stream(SpCclSpeadReader& reader, boost::asio::io_service& engine, DataFactory const& factory, Callback callback);
70  Stream(Stream const&) = delete;
71  Stream(Stream&&);
72  ~Stream();
73  void heap_ready(spead2::recv::live_heap &&) override;
74  void stop_received() override;
75 
76  void process();
77 
78  private:
79  boost::asio::io_service& _engine;
80  SpCclSpeadReader& _reader;
81  DataFactory _data_factory;
82  Callback _callback;
83  };
84 
85  public:
86  SpCclSpeadReader(SpCclSpeadReaderConfig const&, Callback callback, DataFactory const& factory=[]() { return new SpCclType(); });
87  ~SpCclSpeadReader();
88  SpCclSpeadReader(SpCclSpeadReader const&) = delete;
89 
93  void start();
94 
98  void stop();
99 
103  static void fill(SpCclType& data, spead2::recv::heap const&);
104 
109  void process();
110 
114  void add_stop_callback(std::function<void()> const& fn);
115 
119  void add_start_callback(std::function<void()> const& fn);
120 
121  private:
122  void stream_reset();
123  void do_start();
124  void visit_stop_listeners();
125  void visit_start_listeners();
126 
127  private:
128  std::mutex _stop_mutex;
129  bool _destructor;
130  std::size_t _max_heaps;
131  std::unique_ptr<Stream> _spead_stream;
132  boost::asio::ip::udp::endpoint _spead_stream_endpoint;
133  std::vector<std::function<void()>> _stop_listeners;
134  std::vector<std::function<void()>> _start_listeners;
135 };
136 
137 
138 } // namespace exporters
139 } // namespace cheetah
140 } // namespace ska
141 #include "detail/SpCclSpeadReader.cpp"
142 #endif // ENABLE_SPEAD
143 
144 #endif // SKA_CHEETAH_EXPORTERS_SPCCLSPEADREADER_H
Some limits and constants for FLDO.
Definition: Brdz.h:35