-
Notifications
You must be signed in to change notification settings - Fork 390
/
event_manager.cc
95 lines (83 loc) · 2.51 KB
/
event_manager.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "Thrift.h"
#include "io/event_manager.h"
#include "base/logging.h"
#include "io/io_log.h"
using namespace boost::asio;
SandeshTraceBufferPtr IOTraceBuf(SandeshTraceBufferCreate(IO_TRACE_BUF, 1000));
EventManager::EventManager() {
shutdown_ = false;
}
void EventManager::Shutdown() {
shutdown_ = true;
// TODO: make sure that are no users of this event manager.
io_service_.stop();
}
void EventManager::Run() {
assert(mutex_.try_lock());
io_service::work work(io_service_);
do {
if (shutdown_) break;
boost::system::error_code ec;
io_service_.run(ec);
if (ec) {
EVENT_MANAGER_LOG_ERROR("io_service run failed: " << ec.message());
continue;
}
} while(0);
mutex_.unlock();
}
// Run method to handle exceptions coming during io_service run
void EventManager::RunWithExceptionHandling() {
using namespace apache::thrift;
assert(mutex_.try_lock());
io_service::work work(io_service_);
do {
if (shutdown_) break;
boost::system::error_code ec;
try {
io_service_.run(ec);
if (ec) {
EVENT_MANAGER_LOG_ERROR("io_service run failed: " << ec.message());
break;
}
} catch(const TException &except) {
// ignore thrift exceptions
EVENT_MANAGER_LOG_ERROR("Thrift exception caught : " <<
except.what() << "; ignoring");
continue;
} catch (std::exception &except) {
static std::string what = except.what();
EVENT_MANAGER_LOG_ERROR("Exception caught in io_service run : "
<< what);
exit(-1);
} catch(...) {
EVENT_MANAGER_LOG_ERROR("Exception caught in io_service run : "
"bailing out");
exit(-1);
}
} while(true);
mutex_.unlock();
}
size_t EventManager::RunOnce() {
assert(mutex_.try_lock());
if (shutdown_) return 0;
boost::system::error_code err;
size_t res = io_service_.run_one(err);
if (res == 0)
io_service_.reset();
mutex_.unlock();
return res;
}
size_t EventManager::Poll() {
assert(mutex_.try_lock());
if (shutdown_) return 0;
boost::system::error_code err;
size_t res = io_service_.poll(err);
if (res == 0)
io_service_.reset();
mutex_.unlock();
return res;
}