0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SessionData.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
22 #ifndef Hyperspace_SessionData_h
23 #define Hyperspace_SessionData_h
24 
25 #include "Notification.h"
26 
27 #include <chrono>
28 #include <list>
29 #include <memory>
30 #include <mutex>
31 #include <set>
32 
33 namespace Hyperspace {
34 
35  using namespace Hypertable;
36 
37  class SessionData {
38  public:
39  SessionData(const sockaddr_in &_addr, uint32_t lease_interval, uint64_t _id)
40  : addr(_addr), m_lease_interval(lease_interval), id(_id) {
41  expire_time = std::chrono::steady_clock::now() +
42  std::chrono::milliseconds(lease_interval);
43  }
44 
45  void add_notification(Notification *notification) {
46  std::lock_guard<std::mutex> lock(mutex);
47 
48  if (expired) {
49  notification->event_ptr->decrement_notification_count();
50  delete notification;
51  }
52  else
53  notifications.push_back(notification);
54  }
55 
56  void purge_notifications(std::set<uint64_t> &delivered_events) {
57  std::lock_guard<std::mutex> lock(mutex);
58  std::list<Notification *>::iterator iter = notifications.begin();
59  while (iter != notifications.end()) {
60  if (delivered_events.count((*iter)->event_ptr->get_id()) > 0) {
61  (*iter)->event_ptr->decrement_notification_count();
62  delete *iter;
63  iter = notifications.erase(iter);
64  }
65  else
66  ++iter;
67  }
68  }
69 
70  const char* get_name() const
71  {
72  return name.c_str();
73  }
74 
76  {
77  std::lock_guard<std::mutex> lock(mutex);
78  std::list<Notification *>::iterator iter;
79  CommBuf *cbuf =0;
80  for (iter = notifications.begin(); iter != notifications.end(); ++iter) {
81  len += 8; // handle
82  len += (*iter)->event_ptr->encoded_length();
83  }
84 
85  cbuf = new CommBuf(header, len);
86  cbuf->append_i64(id);
87  cbuf->append_i32(Error::OK);
88  cbuf->append_i32(notifications.size());
89  for (iter = notifications.begin(); iter != notifications.end(); ++iter) {
90  cbuf->append_i64((*iter)->handle);
91  (*iter)->event_ptr->encode(cbuf);
92  }
93  return cbuf;
94  }
95 
96  bool renew_lease() {
97  std::lock_guard<std::mutex> lock(mutex);
98  auto now = std::chrono::steady_clock::now();
99  if (expire_time < now)
100  return false;
101  expire_time = now + std::chrono::milliseconds(m_lease_interval);
102  return true;
103  }
104 
105  uint64_t get_id() const { return id; }
106 
107  const struct sockaddr_in& get_addr() const { return addr; }
108 
109  void extend_lease(std::chrono::milliseconds millis) {
110  std::lock_guard<std::mutex> lock(mutex);
111  expire_time += millis;
112  }
113 
114  bool is_expired(std::chrono::steady_clock::time_point now) {
115  std::lock_guard<std::mutex> lock(mutex);
116  return expired || expire_time < now;
117  }
118 
119  void expire() {
120  std::lock_guard<std::mutex> lock(mutex);
121  if (expired)
122  return;
123  expired = true;
124  std::list<Notification *>::iterator iter = notifications.begin();
125  while (iter != notifications.end()) {
126  (*iter)->event_ptr->decrement_notification_count();
127  delete *iter;
128  iter = notifications.erase(iter);
129  }
130  }
131 
133  std::lock_guard<std::mutex> lock(mutex);
134  expire_time = std::chrono::steady_clock::now();
135  }
136 
137  void set_name(const String &name_) {
138  std::lock_guard<std::mutex> lock(mutex);
139  name = name_;
140  }
141 
142  friend struct LtSessionData;
143 
144  private:
145 
147  struct sockaddr_in addr;
148  std::chrono::steady_clock::time_point expire_time;
149  uint32_t m_lease_interval {};
150  uint64_t id;
151  bool expired {};
152  std::list<Notification *> notifications;
154  };
155 
156  typedef std::shared_ptr<SessionData> SessionDataPtr;
157 
158  struct LtSessionData {
159  bool operator()(const SessionDataPtr &x, const SessionDataPtr &y) const {
160  return std::chrono::operator>(x->expire_time, y->expire_time);
161  }
162  };
163 
164 }
165 
166 #endif // Hyperspace_SessionData_h
uint64_t get_id() const
Definition: SessionData.h:105
const struct sockaddr_in & get_addr() const
Definition: SessionData.h:107
static std::mutex mutex
Definition: Logger.cc:43
struct sockaddr_in addr
Definition: SessionData.h:147
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
std::list< Notification * > notifications
Definition: SessionData.h:152
std::shared_ptr< SessionData > SessionDataPtr
Definition: SessionData.h:156
bool is_expired(std::chrono::steady_clock::time_point now)
Definition: SessionData.h:114
SessionData(const sockaddr_in &_addr, uint32_t lease_interval, uint64_t _id)
Definition: SessionData.h:39
const char * get_name() const
Definition: SessionData.h:70
Hyperspace definitions
void extend_lease(std::chrono::milliseconds millis)
Definition: SessionData.h:109
void append_i32(uint32_t ival)
Appends a 32-bit integer to the primary buffer.
Definition: CommBuf.h:230
void append_i64(uint64_t lval)
Appends a 64-bit integer to the primary buffer.
Definition: CommBuf.h:239
Hypertable definitions
void purge_notifications(std::set< uint64_t > &delivered_events)
Definition: SessionData.h:56
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
void set_name(const String &name_)
Definition: SessionData.h:137
HyperspaceEventPtr event_ptr
Definition: Notification.h:37
Message buffer for holding data to be transmitted over a network.
Definition: CommBuf.h:79
CommBuf * serialize_notifications_for_keepalive(CommHeader &header, uint32_t &len)
Definition: SessionData.h:75
std::chrono::steady_clock::time_point expire_time
Definition: SessionData.h:148
void add_notification(Notification *notification)
Definition: SessionData.h:45
bool operator>(const directory< _Key, _Tp, _Compare, _Allocator > &__x, const directory< _Key, _Tp, _Compare, _Allocator > &__y)
Definition: directory.h:852
bool operator()(const SessionDataPtr &x, const SessionDataPtr &y) const
Definition: SessionData.h:159