forked from redpanda-data/seastar-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
63 lines (53 loc) · 1.82 KB
/
main.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
#include <seastar/core/app-template.hh>
#include <seastar/core/sharded.hh>
#include <seastar/core/sstring.hh>
#include <chrono>
#include <iostream>
// the speak service runs on every core (see `seastar::sharded<speak_service>`
// below). when the `speak` method is invoked, it returns a message tagged with
// the core on which the method was invoked.
class speak_service final {
public:
speak_service(const seastar::sstring& msg)
: _msg(msg) {
}
seastar::sstring speak() {
std::stringstream ss;
ss << "msg: \"" << _msg << "\" from core "
<< seastar::engine().cpu_id();
return ss.str();
}
seastar::future<> stop() {
return seastar::make_ready_future<>();
}
private:
seastar::sstring _msg;
};
int main(int argc, char** argv) {
seastar::sharded<speak_service> speak;
seastar::app_template app;
{
namespace po = boost::program_options;
app.add_options()(
"msg",
po::value<seastar::sstring>()->default_value("default-msg"),
"msg");
}
return app.run(argc, argv, [&] {
seastar::engine().at_exit([&speak] { return speak.stop(); });
auto& opts = app.configuration();
auto msg = opts["msg"].as<seastar::sstring>();
return speak.start(msg).then([&speak] {
// sharded<>::map will run the provided lambda on each core. in this
// case, the speak method of the service is invoked and the messaes
// from each core are printed to stdout.
return speak.map([](auto s) { return s.speak(); })
.then([](auto msgs) {
for (auto msg : msgs) {
std::cout << msg << std::endl;
}
return seastar::make_ready_future<int>(0);
});
});
});
}