Jpp  18.3.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
recipients_handler.hh
Go to the documentation of this file.
1 #ifndef RECIPIENTS_HANDLER_HH
2 #define RECIPIENTS_HANDLER_HH
3 
4 #include "recipient.hh"
5 
6 #include <string>
7 #include <vector>
8 
9 #include <boost/asio.hpp>
10 
11 /**
12  * \author cpellegrino
13  */
14 
15 boost::asio::ip::tcp::endpoint make_endpoint(const std::string& address);
16 
18 {
20 
21  boost::asio::io_service m_service;
22  const size_t m_circbuff_size;
24 
25  public:
26 
27  RecipientsHandler(size_t circbuf_size = 10)
28  :
29  m_circbuff_size(circbuf_size)
30  {}
31 
33  {
34  wipe();
35  }
36 
37  bool add(const std::string& id)
38  {
39  if (find(make_endpoint(id)) == m_recipients.end())
40  {
41  m_recipients.push_back(
42  new Recipient(
43  m_service,
44  make_endpoint(id),
46  return true;
47  }
48 
49  return false;
50  }
51 
52  bool remove(const std::string& id)
53  {
54  container_t::iterator it = find(make_endpoint(id));
55 
56  if (it != m_recipients.end()) {
57  const Recipient* to_be_erased = *it;
58 
59  m_recipients.erase(it);
60 
61  // write on disk to_be_erased->m_cbuffer. If necessary...
62 
63  delete to_be_erased;
64  return true;
65  }
66 
67  return false;
68  }
69 
70  bool send(const Frame& data)
71  {
72  bool status = false;
73  if (m_recipients.size())
74  {
75  const size_t n = data.getSeqNumber() % m_recipients.size();
76  status = m_recipients[n]->sendIfPossible(data);
77  }
78  return status;
79  }
80 
81  void wipe()
82  {
83  for (size_t i = 0; i < m_recipients.size(); ++i)
84  {
85  delete m_recipients[i];
86  }
87  m_recipients.clear();
88  }
89 
90  private:
91 
92  container_t::iterator find(const boost::asio::ip::tcp::endpoint& ep)
93  {
94  container_t::iterator it = m_recipients.begin();
95  for (container_t::const_iterator et = m_recipients.end(); it != et; ++it)
96  {
97  if ((*it)->m_endpoint == ep)
98  {
99  break;
100  }
101  }
102 
103  return it;
104  }
105 
106 };
107 
108 #endif // RECIPIENTS_HANDLER_HPP
std::vector< Recipient * > container_t
bool add(const std::string &id)
const size_t m_circbuff_size
const int n
Definition: JPolint.hh:786
RecipientsHandler(size_t circbuf_size=10)
then awk string
boost::asio::io_service m_service
Template Frame for ARS data.
Definition: frame.hh:12
boost::asio::ip::tcp::endpoint make_endpoint(const std::string &address)
unsigned int getSeqNumber() const
Definition: frame.hh:29
container_t::iterator find(const boost::asio::ip::tcp::endpoint &ep)
bool send(const Frame &data)