Skip to content

Commit

Permalink
Merge pull request #12 from bemanproject/milano-presentation
Browse files Browse the repository at this point in the history
added code and data for the 2024-11-27 Milano presentation
  • Loading branch information
dietmarkuehl authored Nov 30, 2024
2 parents dd1f169 + f90be71 commit 543372f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FetchContent_Declare(
execution26
# for local development, use SOURCE_DIR <path-to>/execution26
GIT_REPOSITORY https://github.com/bemanproject/execution26
GIT_TAG 81c58fe
GIT_TAG c32b4fc
)
FetchContent_MakeAvailable(execution26)

Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(EXAMPLES
client
task
cppcon-2024
milano
)

foreach(EXAMPLE ${EXAMPLES})
Expand Down
10 changes: 10 additions & 0 deletions examples/data/index-milano.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<head>
<title>Ciao Milano!</title>
</head>
<body>
<img src="/logo.png"/>
<h1>Ciao Milano!</h1>
<img src="/itcpp.png"/>
</body>
</html>
Binary file added examples/data/itcpp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 14 additions & 2 deletions examples/demo_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
#ifndef INCLUDED_EXAMPLES_DEMO_ALGORITHM
#define INCLUDED_EXAMPLES_DEMO_ALGORITHM

#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wmissing-braces"
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-warning-option"
# pragma clang diagnostic ignored "-Wmissing-braces"
#endif

#include <beman/net29/net.hpp>
#include <atomic>
#include <optional>
Expand All @@ -16,8 +28,6 @@
# include<expected>
#endif

#include <iostream> //-dk:TODO remove

// ----------------------------------------------------------------------------

namespace demo
Expand Down Expand Up @@ -88,11 +98,13 @@ namespace demo::detail
{
using type = ex::detail::type_list<T...>;
};
#if __clang_major__ < 16
template <typename... T>
struct make_type_list<ex::completion_signatures<T...>>
{
using type = ex::detail::type_list<T...>;
};
#endif
template <typename T>
using make_type_list_t = typename make_type_list<T>::type;
}
Expand Down
97 changes: 97 additions & 0 deletions examples/milano.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// examples/http-server.cpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <beman/net29/net.hpp>
#include <beman/execution26/execution.hpp>
#include "demo_algorithm.hpp"
#include "demo_error.hpp"
#include "demo_scope.hpp"
#include "demo_task.hpp"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <string_view>
#include <unordered_map>

namespace ex = beman::execution26;
namespace net = beman::net29;
using namespace std::chrono_literals;

// ----------------------------------------------------------------------------

std::unordered_map<std::string, std::string> files{
{"/", "examples/data/index-milano.html"},
{"/favicon.ico", "examples/data/favicon.ico"},
{"/logo.png", "examples/data/logo.png"},
{"/itcpp.png", "examples/data/itcpp.png"},
};

auto timeout(auto scheduler, auto dur, ex::sender auto sender) {
return demo::when_any(
net::resume_after(scheduler, dur)
| demo::into_error([]{ return std::error_code(); }),
std::move(sender)
);
}

using on_exit_msg = std::unique_ptr<char const, decltype([](auto msg){ std::cout << msg << "\n"; })>;
demo::task<> run_client(auto client, auto s) {
on_exit_msg exit("client exiting");
char buffer[8194];
std::ostringstream out;
try {
while (true) {
auto n = co_await timeout(s, 1s, net::async_receive(client, net::buffer(buffer)));
if (n == 0u)
co_return;
// std::cout << "received=" << std::string_view(buffer, n) << "\n";
std::istringstream in(std::string(buffer, n));
std::string method, url, version;
if (!(in >> method >> url >> version))
co_return;
auto it = files.find(url);
std::cout << "url=" << url << " found=" << (it == files.end()? "no": "yes") << "\n";
std::string content;
if (it != files.end()) {
std::ifstream fin(it->second);
out.str({});
out << fin.rdbuf();
content = out.str();
}
out.str(std::string());
out << "HTTP/1.1 200 found\r\n"
<< "Content-Length: " << content.size() << "\r\n"
<< "\r\n"
<< content;

content = out.str();
co_await net::async_send(client, net::buffer(content));
//break;
}
} catch (std::exception const& ex) {
std::cout << "received timeout! ex=" << ex.what() << "\n";
} catch (...) {
std::cout << "received timeout!\n";
}
}

auto main() -> int
{
demo::scope scope;
net::io_context context;

scope.spawn([](auto& context, auto& scope)->demo::task<> {
net::ip::tcp::endpoint ep(net::ip::address_v4::any(), 12345);
net::ip::tcp::acceptor acceptor(context, ep);
while (true) {
auto[client, address] = co_await net::async_accept(acceptor);
std::cout << "received a connection from " << address << "\n";
scope.spawn(run_client(std::move(client), context.get_scheduler()));
}
}(context, scope));

context.run();


}
2 changes: 1 addition & 1 deletion src/beman/net29/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ install(
FILE_SET ${TARGET_LIBRARY}_detail_headers
)
target_include_directories(${TARGET_LIBRARY} PUBLIC $<INSTALL_INTERFACE:include>)
target_link_libraries(${TARGET_LIBRARY} PUBLIC beman::execution26)
target_link_libraries(${TARGET_LIBRARY} PUBLIC beman_execution26)

install(EXPORT ${TARGETS_EXPORT_NAME}1
FILE ${TARGET_LIBRARY}-config.cmake
Expand Down

0 comments on commit 543372f

Please sign in to comment.