0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Event.h
Go to the documentation of this file.
1 /* -*- c++ -*-
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 
28 #ifndef AsyncComm_Event_h
29 #define AsyncComm_Event_h
30 
31 #include "Clock.h"
32 #include "CommHeader.h"
33 
34 #include <Common/Error.h>
35 #include <Common/InetAddr.h>
36 #include <Common/Logger.h>
37 #include <Common/String.h>
38 #include <Common/Time.h>
39 
40 #include <iostream>
41 #include <memory>
42 
43 namespace Hypertable {
44 
54  class Event {
55 
56  public:
57 
60  enum Type {
66  };
67 
74  Event(Type type_, const InetAddr &addr_, int error_=Error::OK)
75  : type(type_), addr(addr_), error(error_) {
76  proxy = 0;
77  }
78 
86  Event(Type type_, const sockaddr_in &addr_, const String &proxy_,
87  int error_=Error::OK)
88  : type(type_), addr(addr_), error(error_) {
89  set_proxy(proxy_);
90  }
91 
97  Event(Type type_, int error_=Error::OK)
98  : type(type_), error(error_) {
99  proxy = 0;
100  }
101 
108  Event(Type type_, const String &proxy_, int error_=0)
109  : type(type_), error(error_) {
110  set_proxy(proxy_);
111  }
112 
115  ~Event() {
116  if (payload_aligned)
117  free((void *)payload);
118  else
119  delete [] payload;
121  delete [] proxy_buf;
122  }
123 
130  void load_message_header(const uint8_t *buf, size_t len) {
131  header.decode(&buf, &len);
132  group_id = header.gid;
133  }
134 
140  void set_proxy(const String &p) {
141  if (p.length() == 0)
142  proxy = 0;
143  else {
144  if (p.length() < 32)
146  else {
147  proxy_buf = new char [ p.length() + 1 ];
148  proxy = proxy_buf;
149  }
150  strcpy((char *)proxy, p.c_str());
151  }
152  }
153 
158  HT_ASSERT(arrival_time.time_since_epoch().count() > 0);
159  return arrival_time + std::chrono::milliseconds(header.timeout_ms);
160  }
161 
166 
169 
171  const char *proxy {};
172 
174  char *proxy_buf {};
175 
178 
181 
185  int error {};
186 
189 
191  const uint8_t *payload {};
192 
194  size_t payload_len {};
195 
205  uint64_t group_id {};
206 
209 
212 
219  String to_str() const;
220 
224  void display() { std::cerr << to_str() << std::endl; }
225  };
226 
228  typedef std::shared_ptr<Event> EventPtr;
230 } // namespace Hypertable
231 
232 #endif // AsyncComm_Event_h
void display()
Displays a one-line string representation of the event to stdout.
Definition: Event.h:224
Network communication event.
Definition: Event.h:54
CommHeader header
Comm layer header for MESSAGE events.
Definition: Event.h:188
void set_proxy(const String &p)
Sets the address proxy name from which this event was generated.
Definition: Event.h:140
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
chrono::time_point< fast_clock > time_point
Definition: fast_clock.h:42
const char * proxy
Address proxy name.
Definition: Event.h:171
char proxy_buf_static[32]
Static proxy name buffer.
Definition: Event.h:177
char * proxy_buf
Pointer to allocated proxy name buffer.
Definition: Event.h:174
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
uint32_t timeout_ms
Request timeout.
Definition: CommHeader.h:144
Event(Type type_, const sockaddr_in &addr_, const String &proxy_, int error_=Error::OK)
Constructor initializing with InetAddr and proxy name.
Definition: Event.h:86
size_t payload_len
Length of the message.
Definition: Event.h:194
Type type
Type of event.
Definition: Event.h:165
Error event
Definition: Event.h:64
Connection established event.
Definition: Event.h:61
uint64_t group_id
Thread group to which this message belongs.
Definition: Event.h:205
const uint8_t * payload
Points to a buffer containing the message payload.
Definition: Event.h:191
#define HT_ASSERT(_e_)
Definition: Logger.h:396
Declaration of ClockT.
uint32_t gid
Group ID (see ApplicationQueue)
Definition: CommHeader.h:142
InetAddr local_addr
Local address to which event was delivered.
Definition: Event.h:180
InetAddr addr
Remote address from which event was generated.
Definition: Event.h:168
Event(Type type_, int error_=Error::OK)
Constructor initializing with empty address.
Definition: Event.h:97
void decode(const uint8_t **bufp, size_t *remainp)
Decode serialized header at *bufp The bufp pointer is advanced to the address immediately following t...
Definition: CommHeader.cc:58
Event(Type type_, const String &proxy_, int error_=0)
Constructor initialized with proxy name.
Definition: Event.h:108
Encapsulate an internet address.
Definition: InetAddr.h:66
ClockT::time_point deadline()
Deadline for request.
Definition: Event.h:157
Logging routines and macros.
Type
Enumeration for event types.
Definition: Event.h:60
Event(Type type_, const InetAddr &addr_, int error_=Error::OK)
Constructor initializing with InetAddr.
Definition: Event.h:74
Connection disconnected event.
Definition: Event.h:62
Time related declarations.
void load_message_header(const uint8_t *buf, size_t len)
Loads header object from serialized message buffer.
Definition: Event.h:130
Hypertable definitions
int error
Error code associated with this event.
Definition: Event.h:185
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
ClockT::time_point arrival_time
time (seconds since epoch) when message arrived
Definition: Event.h:208
Internet address wrapper classes and utility functions.
Request/response message event.
Definition: Event.h:63
Timer event
Definition: Event.h:65
String to_str() const
Generates a one-line string representation of the event.
Definition: Event.cc:48
A String class based on std::string.
Declarations for CommHeader.
~Event()
Destructor.
Definition: Event.h:115
Error codes, Exception handling, error logging.
bool payload_aligned
Flag indicating if payload was allocated with posix_memalign.
Definition: Event.h:211