xo-umbrella2/xo-webutil/include/xo/webutil/StreamEndpointDescr.hpp
Roland Conybeare d1b5b75076
Some checks failed
cmake-docker / cmake-build (clang, clang++) (push) Failing after 22m28s
cmake-docker / cmake-build (gcc, g++) (push) Failing after 21m6s
CI / smoke-test (push) Failing after 29m42s
xo-object: use xo-ppsink !xo-indentlog [REFACTOR]
2026-08-07 10:03:12 -04:00

82 lines
2.8 KiB
C++

/* file StreamEndpointDescr.hpp
*
* author: Roland Conybeare, Sep 2022
*/
#pragma once
#include "Alist.hpp"
#include <xo/refcnt/Refcounted.hpp>
#include <xo/callback/CallbackSet.hpp>
#include <xo/ppsink/PpSink.hpp>
#include <xo/ppsink/Prettifier.hpp>
#include <functional>
namespace xo {
namespace reactor { class AbstractSink; }
namespace web {
/* a function that creates an event subscription */
using StreamSubscribeFn = std::function<fn::CallbackId (rp<reactor::AbstractSink> const & ws_sink)>;
using StreamUnsubscribeFn = std::function<void (fn::CallbackId id)>;
/* describes a stream endpoint
* this comprises
* - a uri pattern (matches stream name)
* - a function that establishes subscription
* (by attaching supplied WebsocketSink to an event source)
*/
class StreamEndpointDescr {
public:
using PpSink = xo::pp::PpSink;
public:
StreamEndpointDescr(std::string uri_pattern,
StreamSubscribeFn subscribe_fn,
StreamUnsubscribeFn unsubscribe_fn);
std::string const & uri_pattern() const { return uri_pattern_; }
StreamSubscribeFn const & subscribe_fn() const { return subscribe_fn_; }
StreamUnsubscribeFn const & unsubscribe_fn() const { return unsubscribe_fn_; }
/** structured pretty-printing: render this descriptor into @p sink.
*
* See webutil_ostream.hpp for @c os << StreamEndpointDescr.
**/
void pretty(PpSink & sink) const;
std::string display_string() const;
private:
/* unique pattern in URI-space for this endpoint
* for example
* .uri_pattern = /stem/${foo}/${bar}
* means this endpoint generates contents for uri's
* /stem/apple/banana
* /stem/aphid/green
* but not for
* /stem/apple/banana/carrot
*/
std::string uri_pattern_;
/* a function that subscribes to an event stream
* (by attaching a websocket sink)
*/
StreamSubscribeFn subscribe_fn_;
/* reverses effect of a particular call to .subscribe_fn */
StreamUnsubscribeFn unsubscribe_fn_;
}; /*StreamEndpointDescr*/
} /*namespace web*/
} /*namespace xo*/
namespace xo::pp {
/** pretty-print a StreamEndpointDescr into a PpSink. **/
template <>
struct Prettifier<xo::web::StreamEndpointDescr> {
static void print(PpSink & sink, const xo::web::StreamEndpointDescr & x) {
x.pretty(sink);
}
};
} /*namespace xo::pp*/
/* end StreamEndpointDescr.hpp */