Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
Psbc.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 #include "cheetah/psbc/Psbc.h"
25 #include "panda/Log.h"
26 #include "panda/Error.h"
27 
28 namespace ska {
29 namespace cheetah {
30 namespace psbc {
31 
32 
33 template<typename Handler>
34 Psbc<Handler>::Psbc(Config const& config, Handler& h)
35  : _duration(0 * data::seconds)
36  , _dump_time(config.dump_time())
37  , _data(DmTimeType::make_shared())
38  , _handler(h)
39  , _mutex(std::unique_ptr<std::mutex>(new std::mutex))
40 {
41 }
42 
43 template<typename Handler>
45 {
46 }
47 
48 template<typename Handler>
49 void Psbc<Handler>::operator()(DataType const& data)
50 {
51  PANDA_LOG_DEBUG << "Psbc::operator() invoked";
52  std::unique_lock<std::mutex> lk(*_mutex);
53  PANDA_LOG_DEBUG << "Psbc::operator() lock acquired";
54 
55  auto const& dmtrials = *data;
56 
57  if (!_data->blocks().empty())
58  {
59  auto const& previous_dmtrials = *(_data->blocks().back());
60  if (!previous_dmtrials.is_compatible(dmtrials))
61  {
62  throw panda::Error("Incompatible data block passed to Psbc instance.");
63  }
64  }
65 
66  if (dmtrials.size() == 0)
67  {
68  PANDA_LOG_WARN << "Psbc was passed an empty dmtrials block.";
69  return;
70  }
71  else
72  {
73  _duration += dmtrials.duration();
74  _data->add(data);
75  PANDA_LOG_DEBUG << "Psbc::operator() data added to buffer";
76  PANDA_LOG_DEBUG << "Duration: " << _duration
77  << " Dump time:" << _dump_time;
78  if(_duration >= _dump_time) {
79  PANDA_LOG_DEBUG << "Psbc::operator() invoking handler";
80  if (!this->is_contiguous())
81  {
82  //throw panda::Error("Not all Psbc buffer blocks are contiguous.");
83  PANDA_LOG_DEBUG << "Not contiguous";
84  }
85  _handler(_data);
86  _duration = 0 * data::seconds;
87  _data = DmTimeType::make_shared();
88  }
89  }
90 }
91 
92 template<typename Handler>
94 {
95  auto const& blocks = _data->blocks();
96  auto prev = blocks.cbegin();
97  auto next = std::next(blocks.cbegin(), 1);
98  for (std::size_t idx=0; idx<blocks.size()-1; ++idx)
99  {
100  if (!((*prev)->is_contiguous(**next)))
101  {
102  return false;
103  }
104  ++prev;
105  ++next;
106  }
107  return true;
108 }
109 
110 } // namespace psbc
111 } // namespace cheetah
112 } // namespace ska
A class for buffering DmTrials objects.
Definition: Psbc.h:57
Some limits and constants for FLDO.
Definition: Brdz.h:35
A wrapper class for a list of DmTime instances.
Configuration for the data collection buffer.
Definition: Config.h:41
Psbc(Config const &config, Handler &handler)
Construct a new instance.
Definition: Psbc.cpp:34