blob: 5b55658c793953d738607bdd1e27a663e792b158 [file] [log] [blame]
Ed Tanous08bbe112023-04-06 13:10:02 -07001#pragma once
2
3#include "baserule.hpp"
4#include "http_request.hpp"
5#include "http_response.hpp"
6#include "server_sent_event.hpp"
7
8#include <boost/beast/http/verb.hpp>
9
10#include <functional>
11#include <memory>
12#include <string>
13
14namespace crow
15{
16
17class SseSocketRule : public BaseRule
18{
19 using self_t = SseSocketRule;
20
21 public:
22 explicit SseSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) {}
23
24 void validate() override {}
25
26 void handle(const Request& /*req*/,
27 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28 const std::vector<std::string>& /*params*/) override
29 {
30 asyncResp->res.result(boost::beast::http::status::not_found);
31 }
32
Ed Tanous93cf0ac2024-03-28 00:35:13 -070033 void handleUpgrade(const Request& /*req*/,
Ed Tanous08bbe112023-04-06 13:10:02 -070034 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
35 boost::asio::ip::tcp::socket&& adaptor) override
36 {
37 std::shared_ptr<
38 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>
39 myConnection = std::make_shared<
40 crow::sse_socket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070041 std::move(adaptor), openHandler, closeHandler);
Ed Tanous08bbe112023-04-06 13:10:02 -070042 myConnection->start();
43 }
Ed Tanous93cf0ac2024-03-28 00:35:13 -070044 void handleUpgrade(const Request& /*req*/,
Ed Tanous08bbe112023-04-06 13:10:02 -070045 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous003301a2024-04-16 09:59:19 -070046 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>&&
Ed Tanous08bbe112023-04-06 13:10:02 -070047 adaptor) override
48 {
49 std::shared_ptr<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070050 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>
Ed Tanous08bbe112023-04-06 13:10:02 -070051 myConnection = std::make_shared<crow::sse_socket::ConnectionImpl<
Ed Tanous003301a2024-04-16 09:59:19 -070052 boost::asio::ssl::stream<boost::asio::ip::tcp::socket>>>(
Ed Tanous93cf0ac2024-03-28 00:35:13 -070053 std::move(adaptor), openHandler, closeHandler);
Ed Tanous08bbe112023-04-06 13:10:02 -070054 myConnection->start();
55 }
Ed Tanous08bbe112023-04-06 13:10:02 -070056
57 template <typename Func>
58 self_t& onopen(Func f)
59 {
60 openHandler = f;
61 return *this;
62 }
63
64 template <typename Func>
65 self_t& onclose(Func f)
66 {
67 closeHandler = f;
68 return *this;
69 }
70
71 private:
72 std::function<void(crow::sse_socket::Connection&)> openHandler;
73 std::function<void(crow::sse_socket::Connection&)> closeHandler;
74};
75
76} // namespace crow