Skip to content

Commit bf5fe77

Browse files
authored
Merge branch 'main' into box_local
2 parents 56bba18 + 4dafa30 commit bf5fe77

File tree

7 files changed

+61
-8
lines changed

7 files changed

+61
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ serde-xml-rs = "0.6"
124124
axum = "0.6.20"
125125
hyper-staticfile = "0.9"
126126
httparse = "1.8"
127-
sha1 = "0.10"
127+
sha-1 = "0.10.1"
128128
waker-fn = "1.1.0"
129129
num = "0.4"
130130
smol_str = "0.2.0"

runtime/swimos_http/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ hyper = { workspace = true }
1111
http = { workspace = true }
1212
httparse = { workspace = true }
1313
bytes = { workspace = true }
14-
sha1 = { workspace = true }
14+
sha-1 = { workspace = true }
1515
base64 = { workspace = true }
1616
thiserror = { workspace = true }
1717
tokio = { workspace = true }

server/swimos_agent_derive/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//! Derivation macros for the structure and lifecycle of agents.
16+
1517
use agent_lifecycle::ImplAgentLifecycle;
1618
use lane_model_derive::{combine_agent_attrs, make_agent_attr_consumer, DeriveAgentLaneModel};
1719
use lane_projections::ProjectionsImpl;
@@ -32,6 +34,7 @@ fn default_root() -> syn::Path {
3234

3335
const AGENT_TAG: &str = "agent";
3436

37+
/// Derives an agent implementation of an agent from a struct that lists its lanes and stores as fields.
3538
#[proc_macro_derive(AgentLaneModel, attributes(agent, item))]
3639
pub fn derive_agent_lane_model(input: TokenStream) -> TokenStream {
3740
let input = parse_macro_input!(input as DeriveInput);
@@ -49,6 +52,7 @@ pub fn derive_agent_lane_model(input: TokenStream) -> TokenStream {
4952
.into()
5053
}
5154

55+
/// Derives projection functions from a struct to its fields. This is to help make agent lifecycles less verbose.
5256
#[proc_macro_attribute]
5357
pub fn projections(attr: TokenStream, item: TokenStream) -> TokenStream {
5458
let attr_params = if attr.is_empty() {
@@ -70,6 +74,8 @@ pub fn projections(attr: TokenStream, item: TokenStream) -> TokenStream {
7074
.into()
7175
}
7276

77+
/// Derives the agent lifecycle trait for type. This is applied to an `impl` block for the type and uses
78+
/// annotated event handlers in the block to generate the lifecycle.
7379
#[proc_macro_attribute]
7480
pub fn lifecycle(attr: TokenStream, item: TokenStream) -> TokenStream {
7581
let meta = parse_macro_input!(attr as AttributeArgs);

server/swimos_server_app/src/error.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,27 @@ impl Display for AmbiguousRoutes {
7676

7777
impl Error for AmbiguousRoutes {}
7878

79+
/// Error type returned from the server task if it encounters a non-recoverable error.
7980
#[derive(Debug, Error)]
8081
pub enum ServerError {
82+
/// A network connection failed and could not be re-established.
8183
#[error("The server network connection failed.")]
8284
Networking(#[from] ConnectionError),
85+
/// The storage layer encountered a fatal error restoring or persisting the state of the agents.
8386
#[error("Opening the store for a plane failed.")]
8487
Persistence(#[from] StoreError),
8588
}
8689

90+
/// Error type that is returned if a server cannot be started.
8791
#[derive(Debug, Error)]
8892
pub enum ServerBuilderError {
93+
/// The specified agent routes are ambiguous (contain overlaps).
8994
#[error("The specified agent routes are invalid: {0}")]
9095
BadRoutes(#[from] AmbiguousRoutes),
96+
/// The persistence store specified for the server could not be opened.
9197
#[error("Opening the store failed: {0}")]
9298
Persistence(#[from] StoreError),
99+
/// The server TLS configuration is invalid.
93100
#[error("Invalid TLS configuration/certificate: {0}")]
94101
Tls(#[from] TlsError),
95102
}

server/swimos_server_app/src/lib.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,48 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
//! # SwimOS Server Application
16+
//!
17+
//! This module contains the main runtime loop for the SwimOS server. A SwimOS server contains a web server
18+
//! that can accept incoming websocket connections, using the Warp protocol, to communicate with agents
19+
//! that run within the server. It will additionally accept plain HTTP connections that are targetted at
20+
//! HTTP lanes exposed by the agents.
21+
//!
22+
//! A server instance is created by using the [`ServerBuilder`] to configure how the server should behave. By
23+
//! default, a server will not host any agent routes and these must be registered by calling
24+
//! [`ServerBuilder::add_route`]. This crate is not sufficient, in itself, to produce a complete SwimOS application
25+
//! as it does not supply any implementation of the [agent interface](swimos_api::agent::Agent).
26+
//!
27+
//! A Rust implementation of agents is provided by the `swimos_agent` crate.
28+
//!
29+
//! # Examples
30+
//!
31+
//! ```no_run
32+
//! use std::error::Error;
33+
//! use swimos_server_app::{Server, ServerBuilder};
34+
//!
35+
//! # async fn example(ctrl_c: impl std::future::Future<Output = ()>) -> Result<(), Box<dyn Error>> {
36+
//! let server = ServerBuilder::with_plane_name("Example Server")
37+
//! .set_bind_addr("127.0.0.1:8080".parse()?)
38+
//! .enable_introspection()
39+
//! .with_in_memory_store()
40+
//! // Register agent routes.
41+
//! .build()
42+
//! .await?;
43+
//!
44+
//! let (task, mut handle) = server.run();
45+
//! let stop = async move {
46+
//! ctrl_c.await; // Provide a signal to cause the server to stop. E.g. Ctrl-C.
47+
//! handle.stop();
48+
//! };
49+
//! tokio::join!(stop, task).1?;
50+
//! # Ok(())
51+
//! # }
52+
//! ```
53+
//!
54+
1555
mod config;
16-
pub mod error;
56+
mod error;
1757
mod in_memory_store;
1858
mod plane;
1959
mod server;
@@ -25,11 +65,9 @@ pub use self::{
2565
util::AgentExt,
2666
};
2767

28-
pub mod introspection {
29-
pub use swimos_introspection::IntrospectionConfig;
30-
}
31-
68+
pub use error::{AmbiguousRoutes, ServerBuilderError, ServerError};
3269
pub use ratchet::deflate::DeflateConfig;
70+
pub use swimos_introspection::IntrospectionConfig;
3371
use swimos_utilities::byte_channel::{ByteReader, ByteWriter};
3472

3573
type Io = (ByteWriter, ByteReader);

server/swimos_server_app/src/server/builder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ use swimos_utilities::routing::RoutePattern;
3737
use crate::{
3838
config::SwimServerConfig,
3939
error::ServerBuilderError,
40-
introspection::IntrospectionConfig,
4140
plane::{PlaneBuilder, PlaneModel},
41+
IntrospectionConfig,
4242
};
4343

4444
use super::{

server/swimos_server_app/src/server/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use crate::error::ServerError;
3131

3232
use self::{error::UnresolvableRoute, runtime::StartAgentRequest};
3333

34+
/// A handle used to interact with a running Swim server instance. This can be used to find the interface
35+
/// on which the server is listening, instruct the server to stop and explicitly start agents.
3436
pub struct ServerHandle {
3537
stop_trigger: Option<trigger::Sender>,
3638
addr: Option<SocketAddr>,

0 commit comments

Comments
 (0)