OpenSDN source code
xmpp_channel_mux.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
7 
8 #include <boost/foreach.hpp>
9 
10 #include "base/task_annotations.h"
11 #include "xmpp/xmpp_init.h"
12 #include "xmpp/xmpp_connection.h"
13 
14 using namespace std;
15 using namespace xmsm;
16 
18  : connection_(connection), rx_message_trace_cb_(NULL),
19  tx_message_trace_cb_(NULL) {
20  last_received_ = 0;
21  last_sent_ = 0;
22 }
23 
25  assert(map_.empty());
26 }
27 
29  connection_->Clear();
30 }
31 
32 bool XmppChannelMux::LastReceived(time_t duration) const {
33  return (UTCTimestamp() - last_received_) <= duration;
34 }
35 
36 bool XmppChannelMux::LastSent(time_t duration) const {
37  return (UTCTimestamp() - last_sent_) <= duration;
38 }
39 
42  return (st == xmsm::ESTABLISHED) ? xmps::READY :
44 }
45 
46 void XmppChannelMux::WriteReady(const boost::system::error_code &ec) {
47  std::scoped_lock lock(mutex_);
48 
49  WriteReadyCbMap::iterator iter = map_.begin();
50  WriteReadyCbMap::iterator next = iter;
51  for (; iter != map_.end(); iter = next) {
52  ++next;
53  SendReadyCb cb = iter->second;
54  cb(ec);
55  map_.erase(iter);
56  }
57 }
58 
59 bool XmppChannelMux::Send(const uint8_t *msg, size_t msgsize,
60  const string *msg_str, xmps::PeerId id,
61  SendReadyCb cb) {
62  if (!connection_) return false;
63 
64  std::scoped_lock lock(mutex_);
66  bool res = connection_->Send(msg, msgsize, msg_str);
67  if (res == false) {
68  RegisterWriteReady(id, cb);
69  }
70  return res;
71 }
72 
74  return connection_->GetTaskInstance();
75 }
76 
78  referers_.insert(id);
79 }
80 
82  referers_.erase(id);
83 }
84 
86  rxmap_.insert(make_pair(id, cb));
87 }
88 
90  ReceiveCbMap::iterator it = rxmap_.find(id);
91  if (it != rxmap_.end()) {
92  rxmap_.erase(it);
93  }
94 
95  if (ReceiverCount())
96  return;
97 
98  XmppServerConnection *server_connection =
99  dynamic_cast<XmppServerConnection *>(connection_);
100 
101  // If GracefulRestart helper mode close process is complete, restart the
102  // state machine to form new session with the client.
103  if (!connection_->IsDeleted() && server_connection &&
104  server_connection->server()->IsGRHelperModeEnabled()) {
105  server_connection->state_machine()->Initialize();
106  return;
107  }
108 
110 }
111 
113  return referers_.size();
114 }
115 
117  return rxmap_.size();
118 }
119 
120 vector<string> XmppChannelMux::GetReceiverList() const {
121  vector<string> receivers;
122  for (const auto& value : rxmap_) {
123  receivers.push_back(xmps::PeerIdToName(value.first));
124  }
125  return receivers;
126 }
127 
128 //
129 // To be called after acquiring mutex
130 //
132  map_.insert(make_pair(id, cb));
133 }
134 
135 //
136 // To be called after acquiring mutex
137 //
139  map_.erase(id);
140 }
141 
142 const std::string &XmppChannelMux::ToString() const {
143  return connection_->ToString();
144 }
145 
146 const std::string &XmppChannelMux::FromString() const {
147  return connection_->FromString();
148 }
149 
150 std::string XmppChannelMux::StateName() const {
151  return connection_->StateName();
152 }
153 
154 std::string XmppChannelMux::AuthType() const {
156 }
157 
158 std::string XmppChannelMux::PeerAddress() const {
159  return connection_->endpoint_string();
160 }
161 
162 inline bool MatchCallback(string to, xmps::PeerId peer) {
163  if ((to.find(XmppInit::kBgpPeer) != string::npos) &&
164  (peer == xmps::BGP)) {
165  return true;
166  }
167  if ((to.find(XmppInit::kConfigPeer) != string::npos) &&
168  (peer == xmps::CONFIG)) {
169  return true;
170  }
171  if ((to.find(XmppInit::kDnsPeer) != string::npos) &&
172  (peer == xmps::DNS)) {
173  return true;
174  }
175  if ((to.find(XmppInit::kOtherPeer) != string::npos) &&
176  (peer == xmps::OTHER)) {
177  return true;
178  }
179  return false;
180 }
181 
184  ReceiveCbMap::iterator iter = rxmap_.begin();
185  for (; iter != rxmap_.end(); ++iter) {
186  if (MatchCallback(msg->to, iter->first)) {
187  ReceiveCb cb = iter->second;
188  cb(msg, GetPeerState());
189  }
190  }
191 }
192 
194  CHECK_CONCURRENCY("xmpp::StateMachine");
196  if (state == xmsm::ESTABLISHED) {
197  st = xmps::READY;
198  } else if (state == xmsm::ACTIVE) {
199  st = xmps::TIMEDOUT;
200  }
201 
202  if (connection_->IsClient()) {
203  XmppClient *client = static_cast<XmppClient *>(connection_->server());
204  client->NotifyConnectionEvent(this, st);
205  } else {
206  // Event to create the peer on server
207  XmppServer *server = static_cast<XmppServer *>(connection_->server());
208  server->NotifyConnectionEvent(this, st);
209  }
210 }
211 
212 std::string XmppChannelMux::LastStateName() const {
213  return connection_->LastStateName();
214 }
216  return connection_->LastStateChangeAt();
217 }
218 std::string XmppChannelMux::LastEvent() const {
219  return connection_->LastEvent();
220 }
221 uint32_t XmppChannelMux::rx_open() const {
222  return connection_->rx_open();
223 }
224 uint32_t XmppChannelMux::rx_close() const {
225  return connection_->rx_close();
226 }
227 uint32_t XmppChannelMux::rx_update() const {
228  return connection_->rx_update();
229 }
231  return connection_->rx_keepalive();
232 }
233 uint32_t XmppChannelMux::tx_open() const {
234  return connection_->tx_open();
235 }
236 uint32_t XmppChannelMux::tx_close() const {
237  return connection_->tx_close();
238 }
239 uint32_t XmppChannelMux::tx_update() const {
240  return connection_->tx_update();
241 }
243  return connection_->tx_keepalive();
244 }
245 uint32_t XmppChannelMux::FlapCount() const {
246  return connection_->flap_count();
247 }
248 std::string XmppChannelMux::LastFlap() const {
249  return connection_->last_flap_at();
250 }
251 
254 }
257 }
258 
259 bool XmppChannelMux::RxMessageTrace(const std::string &to_address,
260  int port,
261  int msg_size,
262  const std::string &msg,
263  const XmppStanza::XmppMessage *xmpp_msg) {
264  if (rx_message_trace_cb_) {
265  return rx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg);
266  }
267  return false;
268 }
269 
270 bool XmppChannelMux::TxMessageTrace(const std::string &to_address,
271  int port,
272  int msg_size,
273  const std::string &msg,
274  const XmppStanza::XmppMessage *xmpp_msg) {
275  if (tx_message_trace_cb_) {
276  return tx_message_trace_cb_(to_address, port, msg_size, msg, xmpp_msg);
277  }
278  return false;
279 }
virtual uint32_t FlapCount() const
virtual bool LastReceived(time_t duration) const
virtual void ProcessXmppMessage(const XmppStanza::XmppMessage *msg)
XmppConnection * connection_
bool RxMessageTrace(const std::string &to_address, int port, int msg_size, const std::string &msg, const XmppStanza::XmppMessage *xmpp_msg)
void HandleStateEvent(xmsm::XmState state)
virtual void UnRegisterWriteReady(xmps::PeerId id)
std::atomic< time_t > last_sent_
virtual void RegisterReceive(xmps::PeerId, ReceiveCb)
virtual void UnRegisterReceive(xmps::PeerId)
virtual bool Send(const uint8_t *msg, size_t msg_size, xmps::PeerId id, SendReadyCb cb)
TxMessageTraceCb tx_message_trace_cb_
virtual xmps::PeerState GetPeerState() const
virtual uint32_t tx_update() const
virtual uint32_t rx_open() const
size_t ReceiverCount() const
XmppChannelMux(XmppConnection *)
std::vector< std::string > GetReceiverList() const
RefererSet referers_
virtual bool LastSent(time_t duration) const
WriteReadyCbMap map_
virtual uint32_t tx_open() const
void RegisterWriteReady(xmps::PeerId, SendReadyCb)
virtual void RegisterReferer(xmps::PeerId)
void WriteReady(const boost::system::error_code &ec)
virtual std::string LastStateName() const
virtual std::string LastStateChangeAt() const
virtual uint32_t rx_update() const
virtual const std::string & FromString() const
bool TxMessageTrace(const std::string &to_address, int port, int msg_size, const std::string &msg, const XmppStanza::XmppMessage *xmpp_msg)
virtual void UnRegisterReferer(xmps::PeerId)
virtual std::string LastFlap() const
virtual std::string AuthType() const
virtual ~XmppChannelMux()
virtual std::string StateName() const
virtual uint32_t tx_keepalive() const
virtual void RegisterRxMessageTraceCallback(RxMessageTraceCb cb)
virtual uint32_t rx_close() const
virtual uint32_t tx_close() const
virtual std::string PeerAddress() const
virtual std::string LastEvent() const
RxMessageTraceCb rx_message_trace_cb_
size_t RefererCount() const
ReceiveCbMap rxmap_
virtual const std::string & ToString() const
virtual int GetTaskInstance() const
virtual void Close()
virtual void RegisterTxMessageTraceCallback(TxMessageTraceCb cb)
virtual uint32_t rx_keepalive() const
std::mutex mutex_
std::atomic< time_t > last_received_
boost::function< void(const XmppStanza::XmppMessage *, xmps::PeerState state) > ReceiveCb
Definition: xmpp_channel.h:39
boost::function< bool(const std::string &, int, int, const std::string &, const XmppStanza::XmppMessage *msg) > RxMessageTraceCb
Definition: xmpp_channel.h:45
boost::function< void(const boost::system::error_code &)> SendReadyCb
Definition: xmpp_channel.h:36
boost::function< bool(const std::string &, int, int, const std::string &, const XmppStanza::XmppMessage *msg) > TxMessageTraceCb
Definition: xmpp_channel.h:51
void NotifyConnectionEvent(XmppChannelMux *, xmps::PeerState)
Definition: xmpp_client.cc:209
uint32_t tx_update() const
uint32_t rx_keepalive() const
uint32_t rx_open() const
bool Send(const uint8_t *data, size_t size, const std::string *msg_str=NULL)
std::string LastEvent() const
int GetTaskInstance() const
std::string GetXmppAuthenticationType() const
bool IsDeleted() const
TcpServer * server()
std::string StateName() const
std::string LastStateName() const
const std::string & FromString() const
uint32_t tx_keepalive() const
uint32_t tx_close() const
uint32_t tx_open() const
virtual void RetryDelete()=0
virtual uint32_t flap_count() const =0
XmppStateMachine * state_machine()
bool IsClient() const
xmsm::XmState GetStateMcState() const
std::string endpoint_string() const
std::string LastStateChangeAt() const
const std::string & ToString() const
uint32_t rx_update() const
uint32_t rx_close() const
virtual const std::string last_flap_at() const =0
static const char * kConfigPeer
Definition: xmpp_init.h:24
static const char * kOtherPeer
Definition: xmpp_init.h:28
static const char * kDnsPeer
Definition: xmpp_init.h:26
static const char * kBgpPeer
Definition: xmpp_init.h:25
bool IsGRHelperModeEnabled() const
Definition: xmpp_server.cc:265
void NotifyConnectionEvent(XmppChannelMux *, xmps::PeerState)
Definition: xmpp_server.cc:464
string PeerIdToName(PeerId id)
Definition: xmpp_channel.cc:12
@ CONFIG
Definition: xmpp_channel.h:24
@ OTHER
Definition: xmpp_channel.h:27
@ READY
Definition: xmpp_channel.h:18
@ TIMEDOUT
Definition: xmpp_channel.h:20
@ NOT_READY
Definition: xmpp_channel.h:19
#define CHECK_CONCURRENCY(...)
static time_t UTCTimestamp()
Definition: time_util.h:24
bool MatchCallback(string to, xmps::PeerId peer)