A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
service.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
45#ifndef A11_SERVICE_SERVICE_H_
46#define A11_SERVICE_SERVICE_H_
47
48#include <memory>
49#include <string>
50#include <string_view>
51#include <vector>
52
53#include <absl/status/status.h>
54#include <absl/status/statusor.h>
55#include <absl/time/time.h>
56
59#include "a11/data/types.h"
61#include "a11/net/wire_stream.h"
62#include "a11/service/session.h"
63
64namespace a11::service {
65
79using OnServiceConnection = std::function<a11::Task(
80 std::shared_ptr<Session>, std::shared_ptr<net::WireStream>)>;
81
108
116class Service : public std::enable_shared_from_this<Service> {
117 public:
126 static absl::StatusOr<std::shared_ptr<Service>> Create(
127 std::shared_ptr<actions::ActionRegistry> action_registry = nullptr,
128 OnServiceConnection on_connection = {}, ServiceOptions options = {});
129
130 ~Service();
131
132 Service(const Service&) = delete;
133 Service& operator=(const Service&) = delete;
134
135 // --- what the service can do ------------------------------------------
136
138 [[nodiscard]] std::shared_ptr<actions::ActionRegistry> GetActionRegistry()
139 const;
149 absl::Status SetActionRegistry(
150 const std::shared_ptr<actions::ActionRegistry>& action_registry);
154 absl::Status SetOnConnection(OnServiceConnection on_connection);
155
167 absl::StatusOr<std::string> Describe(std::string_view name,
168 std::string_view query) const;
169
177
178 // --- the sole join point with a transport ------------------------------
179
187 a11::Task Serve(std::shared_ptr<net::WireStream> stream,
195 absl::StatusOr<std::shared_ptr<Session>> StartStreamHandler(
196 const std::shared_ptr<net::WireStream>& stream,
199 absl::Status AddStreamToSession(
200 std::string_view session_id,
201 const std::shared_ptr<net::WireStream>& stream,
203
204 // --- who is connected -------------------------------------------------
205
207 [[nodiscard]] std::vector<std::string> SessionIds() const;
209 absl::StatusOr<std::shared_ptr<Session>> GetSession(
210 std::string_view session_id) const;
212 absl::StatusOr<std::shared_ptr<Session>> GetSessionForStream(
213 std::string_view stream_id) const;
215 [[nodiscard]] size_t SessionCount() const;
216
217 // --- lifecycle --------------------------------------------------------
218
220 [[nodiscard]] bool accepting() const;
226 absl::Status StopAccepting();
233 a11::Task Drain(absl::Duration timeout = absl::InfiniteDuration());
235 absl::Status Abort(const absl::Status& status);
237 [[nodiscard]] a11::Task Done() const;
238
239 private:
240 // Public only so this translation unit's helpers can name it; the definition
241 // lives in the .cc and is not part of the API.
242 public:
243 struct State;
244
245 private:
246 explicit Service(std::shared_ptr<State> state) : state_(std::move(state)) {}
247
248 // A shared_ptr rather than inline pImpl storage: every in-flight connection
249 // task captures the state and unregisters itself from it when its session
250 // finishes, so the state has to be able to outlive this handle.
251 std::shared_ptr<State> state_;
252};
253
254} // namespace a11::service
255
256#endif // A11_SERVICE_SERVICE_H_
std::string stream_id
Definition authorization.cc:328
std::string session_id
Definition authorization.cc:327
A registry of actions, the sessions serving them, and their lifecycle.
Definition service.h:116
absl::Status StopAccepting()
Refuse new connections, leaving live ones alone.
Definition service.cc:343
absl::Status SetOnConnection(OnServiceConnection on_connection)
Replace the per-connection hook.
Definition service.cc:208
absl::StatusOr< std::shared_ptr< Session > > GetSessionForStream(std::string_view stream_id) const
Definition service.cc:317
Service(const Service &)=delete
a11::Task Drain(absl::Duration timeout=absl::InfiniteDuration())
Wait for live sessions to finish.
Definition service.cc:350
absl::Status SetActionRegistry(const std::shared_ptr< actions::ActionRegistry > &action_registry)
Replace the registry new connections are built from.
Definition service.cc:185
absl::StatusOr< std::shared_ptr< Session > > StartStreamHandler(const std::shared_ptr< net::WireStream > &stream, StreamMode mode=StreamMode::kAccept)
Start serving one stream and return its session immediately.
Definition service.cc:266
absl::StatusOr< std::shared_ptr< Session > > GetSession(std::string_view session_id) const
Definition service.cc:306
std::vector< std::string > SessionIds() const
Definition service.cc:296
bool accepting() const
Definition service.cc:338
Service & operator=(const Service &)=delete
std::shared_ptr< actions::ActionRegistry > GetActionRegistry() const
Definition service.cc:180
size_t SessionCount() const
Definition service.cc:333
absl::Status AddStreamToSession(std::string_view session_id, const std::shared_ptr< net::WireStream > &stream, StreamMode mode=StreamMode::kAccept)
Attach another transport to an existing session.
Definition service.cc:283
absl::Status Abort(const absl::Status &status)
Stop accepting and abort every live session with status.
Definition service.cc:369
net::DescribeActionsHandler DescribeHandler()
A net::DescribeActionsHandler bound to this service.
Definition service.cc:237
~Service()
Definition service.cc:174
a11::Task Done() const
Definition service.cc:385
absl::StatusOr< std::string > Describe(std::string_view name, std::string_view query) const
Describes this service's actions, for GET /actions.
Definition service.cc:214
a11::Task Serve(std::shared_ptr< net::WireStream > stream, StreamMode mode=StreamMode::kAccept)
Serve one stream, resolving when its session is finished.
Definition service.cc:249
static absl::StatusOr< std::shared_ptr< Service > > Create(std::shared_ptr< actions::ActionRegistry > action_registry=nullptr, OnServiceConnection on_connection={}, ServiceOptions options={})
Create a service.
Definition service.cc:156
GET /actions, on whichever server happens to hold the port.
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Completion values used by every asynchronous A11 operation.
std::optional< absl::Duration > timeout
Definition main.cc:144
absl::flat_hash_map< std::string, Bytes > ByteMap
String-keyed map of byte values (headers, attributes, etc.).
Definition types.h:58
std::function< absl::StatusOr< std::string >(std::string_view name, std::string_view query)> DescribeActionsHandler
Answers a discovery request without opening a wire stream.
Definition describe_endpoint.h:60
Definition action.h:74
StreamMode
Whether this side starts (kStart) or accepts (kAccept) a stream during its startup handshake.
Definition session.h:112
std::function< a11::Task(std::shared_ptr< Session >, std::shared_ptr< net::WireStream >)> OnServiceConnection
Per-connection preparation, run before the session starts pumping.
Definition service.h:80
std::function< a11::Task(std::shared_ptr< net::WireStream >, std::shared_ptr< Session >)> OnSessionStreamDone
Callback invoked once a session stream has finished; may be a coroutine.
Definition session.h:108
std::function< a11::Task(std::optional< data::WireMessage >, std::shared_ptr< net::WireStream >, std::shared_ptr< Session >)> OnSessionStreamMessage
Callback invoked for each message received on a session stream (nullopt signals end-of-stream); may b...
Definition session.h:105
Future< Unit > Task
Asynchronous operation whose only successful result is completion itself.
Definition future.h:411
The action registry: a catalogue of named schema/handler pairs.
A11's connection-scoped runtime: the Session.
How a service treats the connections it accepts.
Definition service.h:83
bool copy_registry_per_connection
Give each connection its own copy of the registry.
Definition service.h:94
data::ByteMap session_headers
Headers stamped on every session the service creates.
Definition service.h:96
SessionOptions session_options
Limits and timeouts for every session the service creates.
Definition service.h:85
absl::Duration drain_timeout
How long ~Service and Drain() wait for live sessions before abandoning them.
Definition service.h:99
OnSessionStreamMessage on_stream_message
Forwarded to each Session; for a service that wants raw message access.
Definition service.h:101
OnSessionStreamDone on_stream_done
Forwarded to each Session.
Definition service.h:103
absl::Status Validate() const
Definition service.cc:148
Definition service.cc:41
Limits and timeouts governing a Session's buffering, concurrency, and lifetime.
Definition session.h:72
A11's core wire value types: chunks, node fragments and messages.
A11's transport abstraction: the bidirectional WireStream channel and the options/callbacks that driv...