]>
Commit | Line | Data |
---|---|---|
20effc67 TL |
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) 2021 ScyllaDB Ltd. | |
20 | */ | |
21 | ||
22 | #include <iostream> | |
23 | #include <seastar/websocket/server.hh> | |
24 | #include <seastar/core/app-template.hh> | |
25 | #include <seastar/core/fstream.hh> | |
26 | #include <seastar/core/sleep.hh> | |
27 | #include <seastar/core/seastar.hh> | |
28 | #include <seastar/core/loop.hh> | |
29 | #include <seastar/core/thread.hh> | |
30 | #include <seastar/util/defer.hh> | |
31 | ||
32 | using namespace seastar; | |
33 | using namespace seastar::experimental; | |
34 | ||
35 | int main(int argc, char** argv) { | |
36 | seastar::app_template app; | |
37 | app.run(argc, argv, [] () -> seastar::future<> { | |
38 | return async([] { | |
1e59de90 TL |
39 | static websocket::server ws; |
40 | ws.register_handler("echo", [] (input_stream<char>& in, | |
41 | output_stream<char>& out) { | |
42 | return repeat([&in, &out]() { | |
43 | return in.read().then([&out](temporary_buffer<char> f) { | |
44 | std::cerr << "f.size(): " << f.size() << "\n"; | |
45 | if (f.empty()) { | |
46 | return make_ready_future<stop_iteration>(stop_iteration::yes); | |
47 | } else { | |
48 | return out.write(std::move(f)).then([&out]() { | |
49 | return out.flush().then([] { | |
50 | return make_ready_future<stop_iteration>(stop_iteration::no); | |
51 | }); | |
52 | }); | |
53 | } | |
54 | }); | |
55 | }); | |
56 | }); | |
57 | auto d = defer([] () noexcept { | |
20effc67 TL |
58 | ws.stop().get(); |
59 | }); | |
60 | ws.listen(socket_address(ipv4_addr("127.0.0.1", 8123))); | |
61 | std::cout << "Listening on 127.0.0.1:8123 for 1 hour (interruptible, hit Ctrl-C to stop)..." << std::endl; | |
62 | seastar::sleep_abortable(std::chrono::hours(1)).get(); | |
63 | std::cout << "Stopping the server, deepest thanks to all clients, hope we meet again" << std::endl; | |
64 | }); | |
65 | }); | |
66 | } |