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