OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bfd_client_session.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 CodiLime, Inc. All rights reserved.
3  */
4 
6 
7 #include "base/logging.h"
8 #include "http/http_session.h"
9 #include "bfd/bfd_session.h"
12 
13 namespace BFD {
14 
16  client_id_(client_id), server_(server), changed_(true) {
17 }
18 
19 Session *RESTClientSession::GetSession(const boost::asio::ip::address &ip,
20  const SessionIndex &index) const {
21  return GetSession(SessionKey(ip, index));
22 }
23 
25  return server_->SessionByKey(key);
26 }
27 
29  LOG(DEBUG, "Notify: " << client_id_);
30  if (!bfd_sessions_.empty() && http_sessions_.empty()) {
31  changed_ = true;
32  return;
33  }
34  changed_ = false;
35 
36  if (http_sessions_.empty())
37  return;
38 
40  for (Sessions::iterator it = bfd_sessions_.begin();
41  it != bfd_sessions_.end(); ++it) {
42  Session *session = GetSession(*it);
43  map.states[session->key().remote_address] = session->local_state();
44  }
45 
46  std::string json;
47  map.EncodeJsonString(&json);
48  for (HttpSessionSet::iterator it = http_sessions_.begin();
49  it != http_sessions_.end(); ++it) {
50  if (false == it->get()->IsClosed()) {
51  LOG(DEBUG, "Notify: " << client_id_ << " Send notification to "
52  << it->get()->ToString());
53  REST::SendResponse(it->get(), json);
54  }
55  }
56  http_sessions_.clear();
57 }
58 
59 // typedef std::map<HttpSession *, ClientId> HttpSessionMap;
60 // HttpSessionMap http_session_map_;
62  enum TcpSession::Event event) {
63  if (event == TcpSession::CLOSE) {
64  if (!http_sessions_.erase(session))
65  LOG(ERROR, "Unable to find client session");
66  }
67 }
68 
70  http_sessions_.insert(session);
72  this, _1, _2));
73  if (changed_)
74  Notify();
75 }
76 
78  const SessionConfig &config) {
79  Session *session = GetSession(key);
80  if (session)
81  return kResultCode_Ok;
82  Discriminator discriminator;
83  ResultCode result = server_->ConfigureSession(key, config, &discriminator);
84  session = GetSession(key);
85  if (!session)
86  return kResultCode_Error;
88  boost::bind(&RESTClientSession::Notify, this));
89  Notify();
90  return result;
91 }
92 
94  Session *session = GetSession(key);
95  if (!session)
98  return result;
99 }
100 
102  for (Sessions::iterator it = bfd_sessions_.begin();
103  it != bfd_sessions_.end(); ++it) {
104  Session *session = server_->SessionByKey(*it);
107  }
108 }
109 
110 } // namespace BFD
uint32_t Discriminator
Definition: bfd_common.h:17
const SessionKey & key() const
Definition: bfd_session.cc:271
ResultCode AddBFDConnection(const SessionKey &key, const SessionConfig &config)
void UnregisterChangeCallback(ClientId client_id)
Definition: bfd_session.cc:309
void SendResponse(HttpSession *session, const std::string &msg, int status_code)
Session * SessionByKey(const boost::asio::ip::address &address, const SessionIndex &index=SessionIndex())
Definition: bfd_server.cc:165
void OnHttpSessionEvent(HttpSession *session, enum TcpSession::Event event)
uint32_t ClientId
Definition: bfd_common.h:19
RESTClientSession(Server *server, ClientId client_id)
boost::asio::ip::address remote_address
Definition: bfd_common.h:115
void RegisterChangeCallback(ClientId client_id, ChangeCb cb)
Definition: bfd_session.cc:305
ResultCode
Definition: bfd_common.h:41
ResultCode RemoveSessionReference(const SessionKey &key)
Definition: bfd_server.cc:242
void AddMonitoringHttpSession(HttpSession *session)
BFDState local_state() const
Definition: bfd_session.cc:143
#define LOG(_Level, _Msg)
Definition: logging.h:33
HttpSessionSet http_sessions_
Session * GetSession(const boost::asio::ip::address &ip, const SessionIndex &index=SessionIndex()) const
ResultCode DeleteBFDConnection(const SessionKey &key)
void EncodeJsonString(std::string *json)
ResultCode ConfigureSession(const SessionKey &key, const SessionConfig &config, Discriminator *assignedDiscriminator)
Definition: bfd_server.cc:235
void RegisterEventCb(SessionEventCb cb)