/
event_manager.cc
80 lines (69 loc) · 2.07 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
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
#include "Thrift.h"
#include <string>
#include "io/event_manager.h"
#include "base/logging.h"
#include "io/io_log.h"
using boost::asio::io_service;
SandeshTraceBufferPtr IOTraceBuf(SandeshTraceBufferCreate(IO_TRACE_BUF, 1000));
EventManager::EventManager() {
shutdown_ = false;
}
void EventManager::Shutdown() {
shutdown_ = true;
io_service_.stop();
}
void EventManager::Run() {
using apache::thrift::TException;
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);
assert(false);
} catch (...) {
EVENT_MANAGER_LOG_ERROR("Exception caught in io_service run : "
"bailing out");
assert(false);
}
} 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;
}