]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/demos/tutorial_examples.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / seastar / demos / tutorial_examples.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2020 ScyllaDB.
20 */
21
22 #include <iostream>
23
24 #include <seastar/core/seastar.hh>
25 #include <seastar/core/reactor.hh>
26 #include <seastar/core/future-util.hh>
27 #include <seastar/net/api.hh>
28
29 seastar::future<> service_loop() {
30 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234})),
31 [] (auto& listener) {
32 return seastar::keep_doing([&listener] () {
33 return listener.accept().then(
34 [] (seastar::accept_result res) {
35 std::cout << "Accepted connection from " << res.remote_address << "\n";
36 });
37 });
38 });
39 }
40
41 const char* canned_response = "Seastar is the future!\n";
42
43 seastar::future<> service_loop_2() {
44 seastar::listen_options lo;
45 lo.reuse_address = true;
46 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo),
47 [] (auto& listener) {
48 return seastar::keep_doing([&listener] () {
49 return listener.accept().then(
50 [] (seastar::accept_result res) {
51 auto s = std::move(res.connection);
52 auto out = s.output();
53 return seastar::do_with(std::move(s), std::move(out),
54 [] (auto& s, auto& out) {
55 return out.write(canned_response).then([&out] {
56 return out.close();
57 });
58 });
59 });
60 });
61 });
62 }
63
64 seastar::future<> handle_connection_3(seastar::connected_socket s,
65 seastar::socket_address a) {
66 auto out = s.output();
67 auto in = s.input();
68 return do_with(std::move(s), std::move(out), std::move(in),
69 [] (auto& s, auto& out, auto& in) {
70 return seastar::repeat([&out, &in] {
71 return in.read().then([&out] (auto buf) {
72 if (buf) {
73 return out.write(std::move(buf)).then([&out] {
74 return out.flush();
75 }).then([] {
76 return seastar::stop_iteration::no;
77 });
78 } else {
79 return seastar::make_ready_future<seastar::stop_iteration>(
80 seastar::stop_iteration::yes);
81 }
82 });
83 }).then([&out] {
84 return out.close();
85 });
86 });
87 }
88
89 seastar::future<> service_loop_3() {
90 seastar::listen_options lo;
91 lo.reuse_address = true;
92 return seastar::do_with(seastar::listen(seastar::make_ipv4_address({1234}), lo),
93 [] (auto& listener) {
94 return seastar::keep_doing([&listener] () {
95 return listener.accept().then(
96 [] (seastar::accept_result res) {
97 // Note we ignore, not return, the future returned by
98 // handle_connection(), so we do not wait for one
99 // connection to be handled before accepting the next one.
100 (void)handle_connection_3(std::move(res.connection), std::move(res.remote_address)).handle_exception(
101 [] (std::exception_ptr ep) {
102 fmt::print(stderr, "Could not handle connection: {}\n", ep);
103 });
104 });
105 });
106 });
107 }
108
109 #include <seastar/core/app-template.hh>
110
111 int main(int ac, char** av) {
112 seastar::app_template app;
113 return app.run(ac, av, [] {
114 std::cout << "This is the tutorial examples demo. It is not running anything but rather makes sure the tutorial examples compile" << std::endl;
115 return seastar::make_ready_future<>();
116 });
117 }