0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ApplicationHandler.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 
29 #ifndef AsyncComm_ApplicationHandler_h
30 #define AsyncComm_ApplicationHandler_h
31 
32 #include "Clock.h"
33 #include "Event.h"
34 #include "ReactorRunner.h"
35 
36 namespace Hypertable {
37 
68 
69  public:
76  ApplicationHandler(EventPtr &event) : m_event(event) {
77  if (m_event)
78  m_urgent = (bool)(m_event->header.flags & CommHeader::FLAGS_BIT_URGENT);
79  else
80  m_urgent = false;
81  }
82 
86  ApplicationHandler(bool urgent=false) : m_urgent(urgent) { }
87 
89  virtual ~ApplicationHandler() { }
90 
93  virtual void run() = 0;
94 
101  uint64_t get_group_id() {
102  return (m_event) ? m_event->group_id : 0;
103  }
104 
108  bool is_urgent() { return m_urgent; }
109 
113  bool is_expired() {
114  if (m_event && m_event->type == Event::MESSAGE &&
116  (m_event->header.flags & CommHeader::FLAGS_BIT_REQUEST)) {
117  auto now = ClockT::now();
118  uint32_t wait_ms = (uint32_t)std::chrono::duration_cast<std::chrono::milliseconds>(now - m_event->arrival_time).count();
119  if (wait_ms >= m_event->header.timeout_ms) {
120  if (m_event->header.flags & CommHeader::FLAGS_BIT_REQUEST)
121  HT_WARNF("Request expired, wait time %u > timeout %u",
122  (unsigned)wait_ms, m_event->header.timeout_ms);
123  else
124  HT_WARNF("Response expired, wait time %u > timeout %u", (unsigned)wait_ms,
125  m_event->header.timeout_ms);
126 
127  if (m_event->header.timeout_ms == 0) {
128  HT_INFO("Changing zero timeout request to 120000 ms");
129  m_event->header.timeout_ms = 120000;
130  return false;
131  }
132 
133  return true;
134  }
135  }
136  return false;
137  }
138 
139  protected:
141  bool m_urgent;
142  };
144 } // namespace Hypertable
145 
146 #endif // AsyncComm_ApplicationHandler_h
#define HT_WARNF(msg,...)
Definition: Logger.h:290
Declarations for ReactorRunner.
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
Definition: Event.h:228
#define HT_INFO(msg)
Definition: Logger.h:271
ApplicationHandler(EventPtr &event)
Constructor initializing from an Event object.
Declarations for Event.
uint64_t get_group_id()
Returns the group ID that this handler belongs to.
static bool record_arrival_time
If set to true arrival time is recorded and passed into IOHandler::handle.
Definition: ReactorRunner.h:67
Declaration of ClockT.
static time_point now() noexcept
Definition: fast_clock.cc:37
virtual ~ApplicationHandler()
Destructor.
bool is_expired()
Returns true if request has expired.
ApplicationHandler(bool urgent=false)
Default constructor with m_urgent flag initialization.
Hypertable definitions
virtual void run()=0
Carries out the request.
Request/response message event.
Definition: Event.h:63
Base clase for application handlers.
EventPtr m_event
MESSAGE Event from which handler was initialized.
bool m_urgent
Flag indicating if handler is urgent.
bool is_urgent()
Returns true if request is urgent.