-
Notifications
You must be signed in to change notification settings - Fork 390
/
ovsdb_client_tcp.h
146 lines (114 loc) · 4.32 KB
/
ovsdb_client_tcp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#ifndef SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_
#define SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_
#include <boost/asio.hpp>
#include <boost/asio/buffer.hpp>
#include <base/timer.h>
#include <io/tcp_session.h>
#include <base/queue_task.h>
#include <cmn/agent_cmn.h>
#include <cmn/agent.h>
#include <agent_types.h>
#include <ovsdb_client.h>
#include <ovsdb_client_idl.h>
#include <ovsdb_client_session.h>
namespace OVSDB {
class OvsdbClientTcpSessionReader : public TcpMessageReader {
public:
OvsdbClientTcpSessionReader(TcpSession *session, ReceiveCallback callback);
virtual ~OvsdbClientTcpSessionReader();
protected:
virtual int MsgLength(Buffer buffer, int offset);
virtual const int GetHeaderLenSize() {
// We don't have any header
return 0;
}
virtual const int GetMaxMessageSize() {
return kMaxMessageSize;
}
private:
static const int kMaxMessageSize = 4096;
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcpSessionReader);
};
class OvsdbClientTcpSession : public OvsdbClientSession, public TcpSession {
public:
static const uint32_t TcpReconnectWait = 2000; // in msec
OvsdbClientTcpSession(Agent *agent, OvsPeerManager *manager,
TcpServer *server, Socket *sock, bool async_ready = true);
virtual ~OvsdbClientTcpSession();
// Send message to OVSDB server
void SendMsg(u_int8_t *buf, std::size_t len);
// Receive message from OVSDB server
bool RecvMsg(const u_int8_t *buf, std::size_t len);
int keepalive_interval();
ConnectionStateTable *connection_table();
KSyncObjectManager *ksync_obj_manager();
Ip4Address tsn_ip();
void set_status(std::string status) {status_ = status;}
std::string status() {return status_;}
void OnCleanup();
// method to trigger close of session
void TriggerClose();
// method to return ip address of remoter endpoint
virtual Ip4Address remote_ip() const;
virtual uint16_t remote_port() const;
// Dequeue event from workqueue for processing
bool ProcessSessionEvent(OvsdbSessionEvent event);
void EnqueueEvent(TcpSession::Event event);
bool ReconnectTimerCb();
protected:
virtual void OnRead(Buffer buffer);
// the default io::ReaderTask task for TCP session has task exclusion
// defined with db::DBTable task, Overriding reader task id with
// OVSDB::IO task to run the message receive and keep alive reply
// independent of db::DBTable task.
virtual int reader_task_id() const {
return ovsdb_io_task_id_;
}
private:
friend class OvsdbClientTcp;
std::string status_;
Timer *client_reconnect_timer_;
OvsdbClientTcpSessionReader *reader_;
WorkQueue<OvsdbSessionEvent> *session_event_queue_;
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcpSession);
};
class OvsdbClientTcp : public TcpServer, public OvsdbClient {
public:
typedef boost::function<void (OvsdbClientTcpSession *)> SessionEventCb;
OvsdbClientTcp(Agent *agent, IpAddress tor_ip, int tor_port,
IpAddress tsn_ip, int keepalive_interval,
OvsPeerManager *manager);
virtual ~OvsdbClientTcp();
virtual TcpSession *AllocSession(Socket *socket);
void RegisterClients();
void OnSessionEvent(TcpSession *session, TcpSession::Event event);
const std::string protocol();
const std::string server();
uint16_t port();
Ip4Address tsn_ip();
const boost::asio::ip::tcp::endpoint &server_ep() const;
// Used by Test Code to trigger events in specific order
void set_connect_complete_cb(SessionEventCb cb);
void set_pre_connect_complete_cb(SessionEventCb cb);
// API to shutdown the TCP server
void shutdown();
OvsdbClientSession *FindSession(Ip4Address ip, uint16_t port);
OvsdbClientSession *NextSession(OvsdbClientSession *session);
void AddSessionInfo(SandeshOvsdbClient &client);
protected:
Agent *agent_;
private:
friend class OvsdbClientTcpSession;
TcpSession *session_;
boost::asio::ip::tcp::endpoint server_ep_;
Ip4Address tsn_ip_;
bool shutdown_;
SessionEventCb connect_complete_cb_;
SessionEventCb pre_connect_complete_cb_;
DISALLOW_COPY_AND_ASSIGN(OvsdbClientTcp);
};
};
#endif //SRC_VNSW_AGENT_OVS_TOR_AGENT_OVSDB_CLIENT_OVSDB_CLIENT_TCP_H_