Jpp  15.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
recipient.hh
Go to the documentation of this file.
1 #ifndef RECIPIENT_HH
2 #define RECIPIENT_HH
3 
4 #include <boost/asio.hpp>
5 #include <FrameFactory/frame.hh>
6 #include <boost/circular_buffer.hpp>
7 #include "log.hh"
8 
9 /**
10  * \author cpellegrino
11  */
12 
13 typedef boost::circular_buffer<Frame> CircularBuffer;
14 
15 class Recipient
16 {
17  boost::asio::ip::tcp::socket m_sock;
18  boost::asio::ip::tcp::endpoint m_endpoint;
21 
22  public:
23 
24  Recipient(boost::asio::io_service& service,
25  const boost::asio::ip::tcp::endpoint& endpoint,
26  size_t circbuff_size)
27  :
28  m_sock(service),
29  m_endpoint(endpoint),
30  m_cbuffer(circbuff_size),
31  m_connected(false)
32  {
33  LOG_NOTICE << "Trying to connect to " << m_endpoint;
34  connect();
35  if (!m_connected) {
36  LOG_ERROR << "Connection to " << m_endpoint << " failed";
37  }
38  }
39 
40  void sock_reset()
41  {
42  stop();
43  connect();
44  }
45 
46  bool sendIfPossible(const Frame& data)
47  {
48 
49  if (! m_connected)
50  {
51  sock_reset();
52 
53  if (! m_connected)
54  {
55  m_cbuffer.push_back(data);
56  return false;
57  }
58  }
59 
60  while (m_cbuffer.size())
61  {
62  if (send(m_cbuffer.front()))
63  {
64  m_cbuffer.pop_front();
65  }
66  else
67  {
68  break;
69  }
70  }
71  if (! send(data))
72  {
73  m_cbuffer.push_back(data);
74  }
75 
76  return m_connected;
77  }
78 
80  {
81  stop();
82  }
83 
84  friend class RecipientsHandler;
85 
86  private:
87 
88  void connect()
89  {
90  boost::system::error_code ec;
91  m_sock.connect(m_endpoint, ec);
92  m_connected = !ec;
93 
94  if (m_connected) {
95  boost::asio::socket_base::send_buffer_size option(67108864);
96  m_sock.set_option(option);
97  LOG_NOTICE << "Connection to " << m_endpoint << " succeeded";
98  boost::system::error_code ec;
99  m_sock.shutdown(boost::asio::ip::tcp::socket::shutdown_receive, ec);
100  }
101  }
102 
103  void stop()
104  {
105  boost::system::error_code ec;
106  m_sock.shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);
107  m_sock.close(ec);
108  }
109 /**
110  * Send data
111  *
112  * \param data the Frame to send.
113  * \return true if OK; else false
114  */
115  bool send(const Frame& data)
116  {
117  boost::system::error_code ec;
118  boost::asio::write(m_sock, boost::asio::buffer(data.data(), data.getFrameLength()), ec);
119  m_connected = !ec;
120 
121  if (ec) {
122  LOG_ERROR << "Error transmitting data to " << m_endpoint << ": " << ec;
123  }
124  return m_connected;
125  }
126 };
127 
128 #endif // RECIPIENT_HH
boost::circular_buffer< Frame > CircularBuffer
Definition: recipient.hh:13
void stop()
Definition: recipient.hh:103
Recipient(boost::asio::io_service &service, const boost::asio::ip::tcp::endpoint &endpoint, size_t circbuff_size)
Definition: recipient.hh:24
CircularBuffer m_cbuffer
Definition: recipient.hh:19
boost::asio::ip::tcp::socket m_sock
Definition: recipient.hh:17
#define LOG_NOTICE
Definition: log.hh:112
void connect()
Definition: recipient.hh:88
bool send(const Frame &data)
Send data.
Definition: recipient.hh:115
#define LOG_ERROR
Definition: log.hh:111
bool sendIfPossible(const Frame &data)
Definition: recipient.hh:46
boost::asio::ip::tcp::endpoint m_endpoint
Definition: recipient.hh:18
~Recipient()
Definition: recipient.hh:79
unsigned int getFrameLength() const
Definition: frame.hh:34
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition: io_ascii.hh:154
Template Frame for ARS data.
Definition: frame.hh:12
void sock_reset()
Definition: recipient.hh:40
bool m_connected
Definition: recipient.hh:20