Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
DadaClientBase.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/psrdada/DadaClientBase.h"
25 #include "panda/Log.h"
26 #include "panda/Error.h"
27 #include <fstream>
28 #include <sstream>
29 #include <random>
30 
31 namespace ska {
32 namespace cheetah {
33 namespace psrdada {
34 namespace detail {
35 
36 membuf::membuf(char* begin, char* end)
37 {
38  this->setg(begin, begin, end);
39  this->setp(begin, end);
40 }
41 
42 char* membuf::reader_position() const
43 {
44  return this->gptr();
45 }
46 
47 char* membuf::writer_position() const
48 {
49  return this->pptr();
50 }
51 
52 std::string generate_logger_tag(std::string const& basename)
53 {
54  auto gen = std::mt19937{std::random_device{}()};
55  auto dist = std::uniform_int_distribution<int>{100000000, 999999999};
56  std::stringstream name;
57  name << basename << "_" << dist(gen);
58  return name.str();
59 }
60 
61 } // namespace detail
62 
63 DadaClientBase::DadaClientBase(key_t key, std::string const& logger_name)
64  : _key(key)
65  , _log(detail::generate_logger_tag(logger_name))
66 {
67  std::stringstream _key_string_stream;
68  _key_string_stream.exceptions(std::ifstream::failbit);
69  _key_string_stream << "["<< std::hex << _key << std::dec << "]["<<_log.name()<<"] ";
70  _id = _key_string_stream.str();
71  connect();
72 }
73 
74 DadaClientBase::~DadaClientBase()
75 {
76  if (_connected)
77  {
78  disconnect();
79  }
80 }
81 
83 {
84  return ipcbuf_get_bufsz((ipcbuf_t *) _hdu->data_block);
85 }
86 
88 {
89  return ipcbuf_get_bufsz(_hdu->header_block);
90 }
91 
93 {
94  return ipcbuf_get_nbufs((ipcbuf_t *) _hdu->data_block);
95 }
96 
98 {
99  return ipcbuf_get_nbufs(_hdu->header_block);
100 }
101 
103 {
104  PANDA_LOG_DEBUG << this->id() << "Connecting to dada buffer";
105  _hdu = dada_hdu_create(_log.native_handle());
106  dada_hdu_set_key(_hdu, _key);
107  if (dada_hdu_connect (_hdu) < 0)
108  {
109  _log.write(LOG_ERR, "could not connect to hdu\n");
110  throw panda::Error("Unable to connect to hdu");
111  }
112  PANDA_LOG_DEBUG << this->id() << "Header buffer is " << header_buffer_count()
113  << " x " << header_buffer_size() << " bytes";
114  PANDA_LOG_DEBUG << this->id() << "Data buffer is " << data_buffer_count()
115  << " x " << data_buffer_size() << " bytes";
116  _connected = true;
117 }
118 
120 {
121  PANDA_LOG_DEBUG << this->id() << "Disconnecting from dada buffer";
122  if (dada_hdu_disconnect (_hdu) < 0)
123  {
124  _log.write(LOG_ERR, "could not disconnect from hdu\n");
125  throw panda::Error("Unable to disconnect from hdu");
126  }
127  dada_hdu_destroy(_hdu);
128  _connected = false;
129 }
130 
132 {
133  disconnect();
134  connect();
135 }
136 
137 std::string const& DadaClientBase::id() const
138 {
139  return _id;
140 }
141 
142 } // namespace psrdada
143 } // namespace cheetah
144 } // namespace ska
void disconnect()
Disconnect from ring buffer.
std::size_t data_buffer_size() const
Get the sizes of each data block in the ring buffer.
void reconnect()
Reconnect to the ring buffer.
std::string const & name() const
Return the name of the logger.
Definition: MultiLog.cpp:62
DadaClientBase(key_t key, std::string const &logger_name)
Create a new basic DADA client instance.
Some limits and constants for FLDO.
Definition: Brdz.h:35
std::size_t data_buffer_count() const
Get the number of data blocks in the ring buffer.
void connect()
Connect to ring buffer.
std::size_t header_buffer_size() const
Get the sizes of each header block in the ring buffer.
multilog_t * native_handle()
Get a native handle to the wrapped multilog_t pointer.
Definition: MultiLog.cpp:57
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
std::size_t header_buffer_count() const
Get the number of header blocks in the ring buffer.