Jpp  19.1.0-rc.1
the software that should make you happy
KM3NeT_Acoustic_Link.cc
Go to the documentation of this file.
1 #include "KM3NeT_Acoustic_Link.h"
2 
3 #ifdef DEBUG_KM3NET_ACOUSTIC_LINK
4 #include <stdlib.h>
5 #include <iostream>
6 #include <boost/date_time/posix_time/posix_time.hpp>
7 #include <Tools/KM3NeT_Debug.h>
8 #define DEBUG_MSG(str,lev) do { if (((uint8_t)lev)>=KM3NeT_Acoustic_Link::DEBUG_LEVEL) { DBG_MSG << boost::posix_time::microsec_clock::local_time() << " ACOUSTIC_LINK(" << (void*)this << "): "<< str << '\n'; } }while( false )
9 #else
10 #define DEBUG_MSG(str,lev) do { } while ( false )
11 #endif
12 
13 /**
14  * \author fsimeone
15  */
16 
17 KM3NeT_Acoustic_Link::KM3NeT_Acoustic_Link( boost::asio::io_service& io_service,\
19  uint32_t buffer_size\
20  ): strand_(io_service),\
21  socket_(io_service),\
22  dom_map_(doms)
23 {
24  DEBUG_MSG("Constructor",3);
27  buffer_=(uint8_t*)malloc(buffer_size_);
28  header_received_=false;
29 }
30 
32 {
33  DEBUG_MSG("Destructor",3);
34  Disconnect();
35  free(buffer_);
36 }
37 
39 {
40  DEBUG_MSG("Disconnect",3);
41  if (soft)
42  {
43  boost::system::error_code l_ignore;
44  socket_.shutdown(boost::asio::ip::tcp::socket::shutdown_both, l_ignore);
45  }
46  socket_.close();
47 }
48 
50 {
52  {
53  DEBUG_MSG("Realloc: free " << (void*) buffer_<<" - "<<buffer_size_<<" byte",1);
54  free(buffer_);
56  buffer_=(uint8_t*)malloc(buffer_size_);
57  DEBUG_MSG("Realloc: alloc " << (void*) buffer_<<" - "<<buffer_size_<<" byte",1);
58  }
59 
60  async_read(socket_,boost::asio::buffer(buffer_,buffer_size_),boost::asio::transfer_exactly(data_size_),strand_.wrap(boost::bind(&KM3NeT_Acoustic_Link::Read_completed, shared_from_this(), _1, _2)));
61 }
62 
63 void KM3NeT_Acoustic_Link::Read_completed(const boost::system::error_code& error, std::size_t transferred)
64 {
65  if (!error)
66  {
67  DEBUG_MSG("Read completed " << transferred << " bytes ",1);
68  if (!header_received_)
69  {
70  header_received_=true;
71  Audio_Header* l_pheader=(Audio_Header*)buffer_;
72  DEBUG_MSG("h seconds="<<l_pheader->Common_Header.Time_Stamp_seconds,0);
73  DEBUG_MSG("h tick="<<l_pheader->Common_Header.Time_Stamp_ticks,0);
76 
78  if (i!=dom_map_.end()) i->second->Check_time_async(*l_pheader);
79  }
80  else
81  {
83  if (i!=dom_map_.end())
84  {
85  uint8_t* tmp_buffer=(uint8_t*)malloc(transferred);
86  memcpy(tmp_buffer,buffer_,transferred);
87  i->second->Handle_samples_async(tmp_buffer,transferred);
88  }
90  header_received_=false;
91  }
92  Start_reading();
93  }
94  else
95  {
96  DEBUG_MSG("Read completed err " << error <<" "<< transferred << " bytes",3);
97  Disconnect();
98  }
99 }
100 
#define AUDIO_PACKET_FIXED_SIZE
static const size_t buffer_size
DAQ_Common_Header Common_Header
uint32_t Time_Stamp_seconds