OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ovsdb_client_tcp.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
3  */
4 #ifndef SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_
5 #define SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_
6 #include <boost/asio.hpp>
7 #include <boost/asio/buffer.hpp>
8 
9 #include <base/timer.h>
10 #include <io/tcp_session.h>
11 #include <base/queue_task.h>
12 
13 #include <cmn/agent_cmn.h>
14 #include <cmn/agent.h>
15 #include <agent_types.h>
16 #include <ovsdb_client.h>
17 #include <ovsdb_client_idl.h>
18 #include <ovsdb_client_session.h>
19 
20 namespace OVSDB {
22 public:
25 
26 protected:
27  virtual int MsgLength(Buffer buffer, int offset);
28 
29  virtual const int GetHeaderLenSize() {
30  // We don't have any header
31  return 0;
32  }
33 
34  virtual const int GetMaxMessageSize() {
35  return kMaxMessageSize;
36  }
37 
38 private:
39  static const int kMaxMessageSize = 4096;
41 };
42 
44 public:
45  static const uint32_t TcpReconnectWait = 2000; // in msec
46 
48  TcpServer *server, Socket *sock, bool async_ready = true);
49  virtual ~OvsdbClientTcpSession();
50 
51  // Send message to OVSDB server
52  void SendMsg(u_int8_t *buf, std::size_t len);
53  // Receive message from OVSDB server
54  bool RecvMsg(const u_int8_t *buf, std::size_t len);
55 
56  int keepalive_interval();
57 
58  const boost::system::error_code &ovsdb_close_reason() const;
59 
63 
64  void set_status(std::string status) {status_ = status;}
65  std::string status() {return status_;}
66 
67  void OnCleanup();
68 
69  // method to trigger close of session
70  void TriggerClose();
71 
72  // method to return ip address of remoter endpoint
73  virtual Ip4Address remote_ip() const;
74  virtual uint16_t remote_port() const;
75 
76  // Dequeue event from workqueue for processing
77  bool ProcessSessionEvent(OvsdbSessionEvent event);
78 
79  void EnqueueEvent(TcpSession::Event event);
80  bool ReconnectTimerCb();
81 
82 protected:
83  virtual void OnRead(Buffer buffer);
84 
85  // the default io::ReaderTask task for TCP session has task exclusion
86  // defined with db::DBTable task, Overriding reader task id with
87  // OVSDB::IO task to run the message receive and keep alive reply
88  // independent of db::DBTable task.
89  virtual int reader_task_id() const {
90  return ovsdb_io_task_id_;
91  }
92 
93 private:
94  friend class OvsdbClientTcp;
95 
96  std::string status_;
101 };
102 
103 class OvsdbClientTcp : public TcpServer, public OvsdbClient {
104 public:
105  OvsdbClientTcp(Agent *agent, IpAddress tor_ip, int tor_port,
108  virtual ~OvsdbClientTcp();
109 
110  virtual TcpSession *AllocSession(Socket *socket);
111  void RegisterClients();
112  void OnSessionEvent(TcpSession *session, TcpSession::Event event);
113  const std::string protocol();
114  const std::string server();
115  uint16_t port();
116  Ip4Address tsn_ip();
117  const boost::asio::ip::tcp::endpoint &server_ep() const;
118 
119  // API to shutdown the TCP server
120  void shutdown();
121 
122  OvsdbClientSession *FindSession(Ip4Address ip, uint16_t port);
124  void AddSessionInfo(SandeshOvsdbClient &client);
125 
126 protected:
129  boost::asio::ip::tcp::endpoint server_ep_;
130 
131 private:
132  friend class OvsdbClientTcpSession;
134  bool shutdown_;
136 };
137 };
138 
139 #endif //SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_
140 
int ha_stale_route_interval() const
Definition: ovsdb_client.cc:62
boost::asio::ip::tcp::endpoint server_ep_
boost::asio::const_buffer Buffer
Definition: tcp_session.h:64
boost::function< bool(const uint8_t *, size_t)> ReceiveCallback
Definition: tcp_session.h:311
bool RecvMsg(const u_int8_t *buf, std::size_t len)
OvsdbClientSession * FindSession(Ip4Address ip, uint16_t port)
boost::asio::ip::tcp::socket Socket
Definition: tcp_server.h:31
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcp)
boost::asio::ip::address IpAddress
Definition: address.h:13
virtual uint16_t remote_port() const
OvsdbClientSession * NextSession(OvsdbClientSession *session)
boost::asio::ip::tcp::socket Socket
Definition: tcp_session.h:60
const boost::system::error_code & ovsdb_close_reason() const
void AddSessionInfo(SandeshOvsdbClient &client)
bool ProcessSessionEvent(OvsdbSessionEvent event)
virtual int reader_task_id() const
Definition: agent.h:358
OvsdbClientTcpSessionReader * reader_
boost::asio::const_buffer Buffer
Definition: tcp_session.h:310
const std::string server()
int keepalive_interval() const
Definition: ovsdb_client.cc:48
void set_status(std::string status)
virtual TcpSession * AllocSession(Socket *socket)
const std::string protocol()
virtual void OnRead(Buffer buffer)
void OnSessionEvent(TcpSession *session, TcpSession::Event event)
virtual int MsgLength(Buffer buffer, int offset)
boost::asio::ip::address_v4 Ip4Address
Definition: address.h:14
static const uint32_t TcpReconnectWait
virtual Ip4Address remote_ip() const
const boost::asio::ip::tcp::endpoint & server_ep() const
TcpServer * server()
Definition: tcp_session.h:88
void EnqueueEvent(TcpSession::Event event)
OvsdbClientTcp(Agent *agent, IpAddress tor_ip, int tor_port, IpAddress tsn_ip, int keepalive_interval, int ha_stale_route_interval, OvsPeerManager *manager)
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcpSessionReader)
WorkQueue< OvsdbSessionEvent > * session_event_queue_
KSyncObjectManager * ksync_obj_manager()
Definition: timer.h:54
OvsdbClientTcpSession(Agent *agent, OvsPeerManager *manager, TcpServer *server, Socket *sock, bool async_ready=true)
void SendMsg(u_int8_t *buf, std::size_t len)
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcpSession)
ConnectionStateTable * connection_table()
OvsdbClientTcpSessionReader(TcpSession *session, ReceiveCallback callback)