Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
DadaReadClient.cpp
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 #include "cheetah/psrdada/DadaReadClient.h"
26 #include "panda/Error.h"
27 #include "panda/Log.h"
28 #include <streambuf>
29 #include <functional>
30 
31 namespace ska {
32 namespace cheetah {
33 namespace psrdada {
34 
35 template <typename Iterator, typename DataType>
36 Iterator& DadaReadClient::read(Iterator& begin, Iterator const& end)
37 {
38  std::shared_ptr<bool> destructor_flag(_destructor_flag);
39  while (begin != end)
40  {
41  if(*destructor_flag)
42  {
43  return begin;
44  }
45  if (!_current_reader || _current_reader->empty())
46  {
47  if (!acquire_data_block())
48  {
49  return begin;
50  }
51  }
52  begin = _current_reader->read<DataType>(begin, end);
53  }
54  return begin;
55 }
56 
57 template <typename CallBackFunctor>
58 void DadaReadClient::next_sequence(CallBackFunctor callback)
59 {
60  flush();
61  _engine.post(std::bind(&DadaReadClient::do_next_sequence<CallBackFunctor>,this,_destructor_flag, callback));
62 }
63 
64 template <typename CallBack>
65 void DadaReadClient::do_next_sequence(std::shared_ptr<bool> destructor_flag, CallBack callback)
66 {
67  if (*destructor_flag)
68  {
69  return;
70  }
71 
72  PANDA_LOG_DEBUG << id() << "Acquiring next header block";
73  uint64_t nbytes = 0;
74  try
75  {
76  char* tmp;
77  do
78  {
79  tmp = ipcbuf_get_next_read(_hdu->header_block, &nbytes);
80  if (!tmp)
81  {
82  if (*destructor_flag)
83  {
84  return;
85  }
86  _log.write(LOG_ERR, "Could not acquire next header block\n");
87  }
88  } while(!tmp);
89  auto deleter = [&](std::istream* i)
90  {
91  delete i;
92  if (*destructor_flag)
93  {
94  return;
95  }
96  PANDA_LOG_DEBUG << id() << "Releasing header block";
97  if (ipcbuf_mark_cleared(_hdu->header_block) < 0)
98  {
99  _log.write(LOG_ERR, "Could not mark cleared header block\n");
100  PANDA_LOG_ERROR << "Could not mark cleared header block";
101  }
102  };
103  detail::membuf sbuf(tmp, tmp + nbytes);
104  std::unique_ptr<std::istream, decltype(deleter)> header(new std::istream(&sbuf), deleter);
105  callback(*header,std::current_exception());
106  }
107  catch (...)
108  {
109  std::stringstream sbuf(" ");
110  callback(sbuf, std::current_exception());
111  }
112 }
113 
114 std::shared_ptr<bool> DadaReadClient::stopped() const
115 {
116  return _destructor_flag;
117 }
118 
119 } // namespace psrdada
120 } // namespace cheetah
121 } // namespace ska
Some limits and constants for FLDO.
Definition: Brdz.h:35
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.
std::string const & id() const
Return a string identifier based on the buffer key and log name.
void write(int priority, const char *format, Args &&... args)
Write to the log.
Definition: MultiLog.cpp:34