blob: 105d5f29b606d38309336fbfd0cbe5ab82067ae8 (
plain) (
blame)
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
|
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <memory>
#include <mutex>
#include <vector>
class FRT_RPCRequest;
namespace config::sentinel {
class Cmd {
public:
using UP = std::unique_ptr<Cmd>;
enum CmdType { LIST, RESTART, START, STOP };
Cmd(FRT_RPCRequest *req, CmdType cmdType, const char *service = "")
: _req(req), _cmdType(cmdType), _serviceName(service)
{}
CmdType type() const { return _cmdType; }
const char *serviceName() const { return _serviceName; }
void retError(const char *errorString) const;
void retValue(const char *valueString) const;
~Cmd();
private:
FRT_RPCRequest *_req;
CmdType _cmdType;
const char *_serviceName;
};
class CommandQueue
{
private:
std::mutex _lock;
std::vector<Cmd::UP> _queue;
public:
CommandQueue() = default;
~CommandQueue() = default;
void enqueue(Cmd::UP cmd) {
std::lock_guard guard(_lock);
_queue.push_back(std::move(cmd));
}
std::vector<Cmd::UP> drain() {
std::vector<Cmd::UP> r;
std::lock_guard guard(_lock);
r.swap(_queue);
return r;
}
};
} // namespace config::sentinel
|