Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Self-contained distributed software platform for building stateful, massively real-time streaming applications in Rust.

License

NotificationsYou must be signed in to change notification settings

graphform/swim-rust

Repository files navigation





The Swim Rust SDK contains software framework for building stateful applications that can be interactedwith via multiplexed streaming APIs. It is built on top of theTokio asynchronous runtimeand a Tokio runtime is required for any Swim application.

Each application consists of some number of stateful agents, each of which runs as a separate Tokio taskand can be individually addressed by a URI. An agent may have both public and private state which can eitherbe held solely in memory or, optionally, in persistent storage. The public state of the agent consists of anumber of lanes, analogous to a field in a record. There are multiple kinds of lanes that, for example, lanescontaining single values and those containing a map of key-value pairs.

The state of any lane can be observed by establishing a link to it (either from another agent instance or adedicated client). A established link will push all updates to the state of that lane to the subscriber andwill also allow the subscriber to request changes to the state (for lane kinds that support this). Linksoperate over a web-socket connection and are multiplexed, meaning that links to multiple lanes on the samehost can share a single web-socket connection.

SwimOS Crates.io VersionSwimOS Client Crates.io VersionSwimOS Form Crates.io Version

Website |Developer Guide |Server API Docs |Client API Docs

Usage Guides

Implementing Swim Agents in Rust

Building a Swim Server Application

Reference Documentation

Examples

The following example application runs a SwimOS server that hosts a single agent route where each agent instancehas single lane, calledlane. Each time a changes is made to the lane, it will be printed on the console by theserver.

[dependencies]swimos = {version ="0.1.1",features = ["server","agent"] }
use swimos::{    agent::{        agent_lifecycle::HandlerContext,        agent_model::AgentModel,        event_handler::{EventHandler,HandlerActionExt},        lanes::ValueLane,        lifecycle,AgentLaneModel,},    route::RoutePattern,    server::{until_termination,Server,ServerBuilder},};#[tokio::main]pubasyncfnmain() ->Result<(),Box<dyn std::error::Error>>{// An agent route consists of the agent definition and a lifecycle.let model =AgentModel::new(ExampleAgent::default,ExampleLifecycle.into_lifecycle());let server =ServerBuilder::with_plane_name("Example Plane").set_bind_addr("127.0.0.1:8080".parse()?)// Bind the server to this address..add_route(RoutePattern::parse_str("/examples/{id}")?, model)// Register the agent we have defined..build().await?;// Run the server until we terminate it with Ctrl-C.let(task, handle) = server.run();let(ctrl_c_result, server_result) = tokio::join!(until_termination(handle,None), task);    ctrl_c_result?;    server_result?;Ok(())}// Deriving the `AgentLaneModel` trait makes this type into an agent.#[derive(AgentLaneModel)]structExampleAgent{lane:ValueLane<i32>,}// Any agent type can have any number of lifecycles defined for it. A lifecycle describes// how the agent will react to events that occur as it executes.#[derive(Default,Clone,Copy)]structExampleLifecycle;// The `lifecycle` macro creates an method called `into_lifecycle` for the type, using the// annotated event handlers methods in the block.#[lifecycle(ExampleAgent)]implExampleLifecycle{#[on_event(lane)]fnlane_event(&self,context:HandlerContext<ExampleAgent>,value:&i32,) ->implEventHandler<ExampleAgent>{let n =*value;        context.get_agent_uri().and_then(move |uri|{            context.effect(move ||{println!("Received value: {} for 'lane' on agent at URI: {}.", n, uri);})})}}

For example, if a Swim client sends an update, with the value5, to the agent at the URI/examples/name for thelanelane, an instance ofExampleAgent, usingExampleLifecycle, will be started by the server. The value of thelane will then be set to5 and the following will be printed on the console:

Received value: 5 for 'lane' on agent at URI: /examples/name.

A number of example applications are available in theexample_apps directory which demonstrateindividual features as well as more comprehensive applications.

Development

See thedevelopment guide.

License

This project is licensed under theApache 2.0 License.

Packages

No packages published

Contributors5

Languages


[8]ページ先頭

©2009-2025 Movatter.jp