nds2-client - Developer 0.16.7
Loading...
Searching...
No Matches
channel_listing.hh
Go to the documentation of this file.
1//
2// Created by jonathan.hanks on 5/4/18.
3//
4
5#ifndef NDS2_CLIENT_NDS1_CHANNEL_LISTING_HH
6#define NDS2_CLIENT_NDS1_CHANNEL_LISTING_HH
7
8#include <algorithm>
9#include <array>
10#include <iterator>
11
12#include "common/utils.hh"
14
15namespace nds_impl
16{
17 namespace nds1
18 {
19 namespace v12_2
20 {
21
22 typedef std::array< char, 148 > raw_sc2_channel;
23
24 template < typename Reader, typename Transform >
26 {
27
28 public:
29 typedef typename std::result_of< decltype (
30 &Transform::operator( ) )(
31 Transform, raw_sc2_channel ) >::type value_type;
34 typedef std::size_t difference_type;
35 typedef std::input_iterator_tag iterator_category;
36
37 ChannelListIterator( Reader& r, Transform& t )
38 : pos_{ 0 }, end_{ 0 }, t_( t ), r_( r ), cur_{}
39 {
40 }
41 ChannelListIterator( Reader& r, Transform& t, int pos, int end )
42 : pos_{ pos }, end_{ end }, t_( t ), r_( r ),
43 cur_( t( read_in_sc2_channel( r ) ) )
44 {
45 }
46 ChannelListIterator( Reader& r, Transform& t, int end )
47 : pos_{ end }, end_{ end }, t_( t ), r_( r ), cur_{}
48 {
49 }
51 : pos_{ other.pos_ }, end_{ other.end_ }, t_( other.t_ ),
52 r_( other.r_ ), cur_{ std::move( other.cur_ ) }
53 {
54 }
55
58 : pos_{ other.pos_ }, end_{ other.end_ }, t_( other.t_ ),
59 r_( other.r_ ), cur_( other.cur_ )
60 {
61 }
62
63 bool
65 other ) const
66 {
67 return other.pos_ == pos_;
68 }
69
70 bool
72 other ) const
73 {
74 return other.pos_ != pos_;
75 }
76
78 {
79 return cur_;
80 }
81
83 {
84 pos_++;
85 if ( pos_ < end_ )
86 {
88 read_in_sc2_channel( r_, buf );
89 cur_ = t_( buf );
90 }
91 else
92 {
93 cur_ = value_type( );
94 }
95 return *this;
96 };
97
99 {
100 auto result =
102 this->operator++( );
103 return result;
104 };
105
106 private:
107 int pos_;
108 int end_;
109 Transform& t_;
110 Reader& r_;
112
113 static void
115 {
116 const size_t name_length = 60;
117 std::array< char, 1 > tmp;
118 r.read_exactly( buf.data( ), buf.data( ) + name_length );
119 // There is a bug in some versions of nds1 in some calls
120 // (mainly named channel listing, but lets handle this
121 // generically)
122 // where a extra '\t' is added after the name field.
123 // if there is a extra tab, just consume it and move on
124 r.peek( tmp.data( ), tmp.data( ) + 1 );
125 if ( tmp[ 0 ] == '\t' )
126 {
127 r.read_exactly( tmp.data( ), tmp.data( ) + 1 );
128 }
129 r.read_exactly( buf.data( ) + name_length,
130 buf.data( ) + buf.size( ) );
131 }
132
133 static raw_sc2_channel
135 {
136 raw_sc2_channel buf;
137 read_in_sc2_channel( r, buf );
138 return buf;
139 }
140 };
141
170 template <
171 typename Reader,
172 class OutIt,
173 class Transform =
175 class Predicate =
176 nds_impl::common::TruePredicate< typename std::result_of<
177 decltype ( &Transform::operator( ) )(
178 Transform, raw_sc2_channel ) >::type > >
179 void
181 OutIt it,
182 Transform t = Transform( ),
183 Predicate p = Predicate( ) )
184 {
186 basic_reader io( r );
187 auto count = io.read_uint32_hex( );
189 r, t, 0, count );
191 r, t, count );
192 std::copy_if( begin_range, end_range, it, p );
193 }
194 }
195 }
196}
197
199// Tests only after this point.
201
202#ifdef _NDS_IMPL_ENABLE_CATCH_TESTS_
203
204#include <array>
205#include <iterator>
206#include <string>
207#include <vector>
208#include <iostream>
209
210#include "socket/socket.hh"
211
213#include "common/status_codes.hh"
214#include "common/utils.hh"
215#include "nds1/common/utils.hh"
216
218#include "tests/dummy_socket.hh"
219#include "catch.hpp"
220
221TEST_CASE( "LIST Channels" )
222{
223 /*nds_impl::FullSocket::FullSocket client;
224 client.connect( "127.0.0.1:8088" );
225
226 nds_impl::FullSocket::BufferedReader< nds_impl::FullSocket::FullSocket > r{
227 client };
228
229 std::string cmd = "status channels 2;\n";
230 r.write_all( cmd.data( ), cmd.data( ) + cmd.size( ) );
231
232 {
233 std::array< char, 4 > buf;
234
235 client.read_available( buf.data( ), buf.data( ) + buf.size( ) );
236 std::string tmp( buf.data( ), buf.size( ) );
237 REQUIRE( tmp == "0000" );
238 }*/
239
241 340 ) };
243
244 std::vector< std::string > channels;
245
247
249 r,
250 std::back_inserter( channels ),
251 // TransformSC2(),
252 []( nds_impl::nds1::v12_2::raw_sc2_channel input ) -> std::string {
253 typedef nds_impl::common::Span< char > char_span;
254 char_span input_span( input.data( ), input.size( ) );
255 char_span name_span =
257 input.data( ), input.data( ) + 60 );
258 return std::string{ name_span.data( ), name_span.size( ) };
259 },
260 []( std::string& channel_name ) -> bool {
261 return true;
262 // return ( channel_name.find( "TP" ) != std::string::npos );
263 } );
264 REQUIRE( channels.size( ) == 340 );
265}
266
267#endif // _NDS_IMPL_ENABLE_CATCH_TESTS_
268
269#endif // NDS2_CLIENT_NDS1_CHANNEL_LISTING_HH
Definition buffered_reader.hh:18
Definition utils.hh:84
Definition basic_io.hh:27
Definition channel_listing.hh:26
ChannelListIterator(Reader &r, Transform &t)
Definition channel_listing.hh:37
bool operator==(const ChannelListIterator< Reader, Transform > &other) const
Definition channel_listing.hh:64
ChannelListIterator(ChannelListIterator &&other)
Definition channel_listing.hh:50
value_type & reference
Definition channel_listing.hh:32
ChannelListIterator(Reader &r, Transform &t, int end)
Definition channel_listing.hh:46
ChannelListIterator(Reader &r, Transform &t, int pos, int end)
Definition channel_listing.hh:41
reference operator*()
Definition channel_listing.hh:77
std::result_of< decltype(&Transform::operator())(Transform, raw_sc2_channel)>::type value_type
Definition channel_listing.hh:31
int end_
Definition channel_listing.hh:108
ChannelListIterator< Reader, Transform > & operator++()
Definition channel_listing.hh:82
static raw_sc2_channel read_in_sc2_channel(Reader &r)
Definition channel_listing.hh:134
value_type cur_
Definition channel_listing.hh:111
int pos_
Definition channel_listing.hh:107
Transform & t_
Definition channel_listing.hh:109
value_type * pointer
Definition channel_listing.hh:33
std::size_t difference_type
Definition channel_listing.hh:34
bool operator!=(const ChannelListIterator< Reader, Transform > &other) const
Definition channel_listing.hh:71
ChannelListIterator(const ChannelListIterator< Reader, Transform > &other)
Definition channel_listing.hh:56
ChannelListIterator< Reader, Transform > operator++(int)
Definition channel_listing.hh:98
static void read_in_sc2_channel(Reader &r, raw_sc2_channel &buf)
Definition channel_listing.hh:114
Reader & r_
Definition channel_listing.hh:110
std::input_iterator_tag iterator_category
Definition channel_listing.hh:35
nds_impl::common::Span< char > identify_padded_string(It begin, It end)
Definition utils.hh:19
void read_get_channels(Reader &r, OutIt it, Transform t=Transform(), Predicate p=Predicate())
Definition channel_listing.hh:180
std::array< char, 148 > raw_sc2_channel
Definition channel_listing.hh:22
Definition span_reader.hh:14
std::vector< char > generate_sc2_channel_stream(int count)
Definition nds1_test_utilities.cc:81
Definition dummy_socket.hh:46
TEST_CASE("daq_strlcpy copies strings safely when buffers are sufficiently large")
Definition test_bsd_string.cc:9