View on GitHub

Thrift Asio

Asynchronous client and server for apache thrift implemented via boost::asio

Download this project as a .zip file Download this project as a tar.gz file
thrift_asio_connection_management_mixin.hpp
1 //
2 // Created by Benjamin Schulz on 16/03/15.
3 //
4 
5 #ifndef _THRIFT_ASIO_THRIFT_ASIO_CONNECTION_MANAGEMENT_MIXIN_HPP_
6 #define _THRIFT_ASIO_THRIFT_ASIO_CONNECTION_MANAGEMENT_MIXIN_HPP_
7 
8 namespace betabugs{
9 namespace networking{
10 
38 template <typename ClientType>
40 {
41  public:
43  virtual void on_client_connected(boost::shared_ptr<apache::thrift::protocol::TProtocol> output_protocol)
44  {
45  assert( clients_.find(output_protocol) == clients_.end() );
46 
47  clients_.insert(
48  std::make_pair(
49  output_protocol,
50  std::make_shared<ClientType>(output_protocol)
51  )
52  );
53  }
54 
56  virtual void on_client_disconnected(const boost::shared_ptr<apache::thrift::protocol::TProtocol>& output_protocol, const boost::system::error_code& ec)
57  {
58  (void)ec;
59  assert( clients_.find(output_protocol) != clients_.end() );
60  clients_.erase(output_protocol);
61  }
62 
64  virtual void before_process(boost::shared_ptr<apache::thrift::protocol::TProtocol> output_protocol)
65  {
66  auto pos = clients_.find(output_protocol);
67  assert(pos != clients_.end());
68  current_client_ = pos->second;
69  }
70 
72  virtual void after_process()
73  {
74  current_client_.reset();
75  }
76  protected:
78  typedef boost::shared_ptr<apache::thrift::protocol::TProtocol> protocol_ptr;
79 
81  typedef std::shared_ptr<ClientType> client_ptr;
82 
84  typedef std::map<protocol_ptr, client_ptr> client_map;
85 
88 
91 };
92 
93 }
94 }
95 
96 #endif //_THRIFT_ASIO_THRIFT_ASIO_CONNECTION_MANAGEMENT_MIXIN_HPP_
client_map clients_
All connected clients.
Definition: thrift_asio_connection_management_mixin.hpp:87
std::map< protocol_ptr, client_ptr > client_map
maps protocol instances to clients
Definition: thrift_asio_connection_management_mixin.hpp:84
virtual void on_client_disconnected(const boost::shared_ptr< apache::thrift::protocol::TProtocol > &output_protocol, const boost::system::error_code &ec)
erases the client associated with output_protocol from clients_
Definition: thrift_asio_connection_management_mixin.hpp:56
virtual void before_process(boost::shared_ptr< apache::thrift::protocol::TProtocol > output_protocol)
sets client associated with output_protocol as the current_client_
Definition: thrift_asio_connection_management_mixin.hpp:64
client_ptr current_client_
Only valid while a request is processed.
Definition: thrift_asio_connection_management_mixin.hpp:90
Definition: thrift_asio_connection_management_mixin.hpp:39
boost::shared_ptr< apache::thrift::protocol::TProtocol > protocol_ptr
used as key_type in the client_map
Definition: thrift_asio_connection_management_mixin.hpp:78
virtual void after_process()
sets the current_client_ to zero
Definition: thrift_asio_connection_management_mixin.hpp:72
std::shared_ptr< ClientType > client_ptr
used as mapped_type in the client_map
Definition: thrift_asio_connection_management_mixin.hpp:81
virtual void on_client_connected(boost::shared_ptr< apache::thrift::protocol::TProtocol > output_protocol)
creates a ClientType objects and inserts it into clients_
Definition: thrift_asio_connection_management_mixin.hpp:43