Skip to content

Commit 5167ddf

Browse files
Merge pull request #581 from swimos/supply-lane
Supply lanes
2 parents 0b7dcb4 + 4f9a2d1 commit 5167ddf

File tree

14 files changed

+693
-11
lines changed

14 files changed

+693
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ members = [
2222
"example_apps/command_lane",
2323
"example_apps/value_store",
2424
"example_apps/map_store",
25+
"example_apps/supply_lane",
2526
"example_apps/value_lane_persistence",
2627
"example_apps/map_lane_persistence",
2728
"example_apps/value_store_persistence",

docs/define.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The derive macro for `AgentLaneModel` can be applied to any struct type where al
2929
* Join Value Lanes: `swim::agent::lanes::JoinValueLane`.
3030
* Join Map Lanes: `swim::agent::lanes::JoinMapLane`.
3131
* HTTP Lanes: `swim::agent::lanes::HttpLane` (or the shorthand `swim::agent::lanes::SimpleHttpLane`).
32+
* Supply Lanes: `swim::agent::lanes::SupplyLane`.
3233

3334
The supported store types are:
3435

docs/lifecycle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ fn my_demand_keys_handler(&self, context: HandlerContext<ExampleAgent>) -> impl
137137
}
138138
```
139139

140+
Supply lane events
141+
-------------------
142+
143+
Supply lanes generate no events on a lifecycle.
144+
140145
Value lane events
141146
-----------------
142147

example_apps/supply_lane/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "supply-lane"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
swim = { path = "../../swim", features = ["server", "agent"] }
8+
tokio = { workspace = true, features = ["rt-multi-thread", "macros"]}
9+
example-util = { path = "../example_util" }

example_apps/supply_lane/src/agent.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2015-2023 Swim Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use swim::agent::lanes::SupplyLane;
16+
use swim::agent::{
17+
agent_lifecycle::utility::HandlerContext,
18+
event_handler::{EventHandler, HandlerActionExt},
19+
lanes::ValueLane,
20+
lifecycle, projections, AgentLaneModel,
21+
};
22+
23+
#[derive(AgentLaneModel)]
24+
#[projections]
25+
pub struct ExampleAgent {
26+
lane: ValueLane<i32>,
27+
supply: SupplyLane<i32>,
28+
}
29+
30+
#[derive(Clone)]
31+
pub struct ExampleLifecycle;
32+
33+
#[lifecycle(ExampleAgent)]
34+
impl ExampleLifecycle {
35+
#[on_start]
36+
pub fn on_start(
37+
&self,
38+
context: HandlerContext<ExampleAgent>,
39+
) -> impl EventHandler<ExampleAgent> {
40+
context.get_agent_uri().and_then(move |uri| {
41+
context.effect(move || {
42+
println!("Starting agent at: {}", uri);
43+
})
44+
})
45+
}
46+
47+
#[on_stop]
48+
pub fn on_stop(
49+
&self,
50+
context: HandlerContext<ExampleAgent>,
51+
) -> impl EventHandler<ExampleAgent> {
52+
context.get_agent_uri().and_then(move |uri| {
53+
context.effect(move || {
54+
println!("Stopping agent at: {}", uri);
55+
})
56+
})
57+
}
58+
59+
#[on_event(lane)]
60+
pub fn on_event(
61+
&self,
62+
context: HandlerContext<ExampleAgent>,
63+
value: &i32,
64+
) -> impl EventHandler<ExampleAgent> {
65+
let n = *value;
66+
context.supply(ExampleAgent::SUPPLY, n * 2)
67+
}
68+
}

example_apps/supply_lane/src/main.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2015-2023 Swim Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::{error::Error, time::Duration};
16+
17+
use example_util::{example_logging, manage_handle};
18+
use swim::{
19+
agent::agent_model::AgentModel,
20+
route::RoutePattern,
21+
server::{Server, ServerBuilder},
22+
};
23+
24+
use crate::agent::{ExampleAgent, ExampleLifecycle};
25+
26+
mod agent;
27+
28+
#[tokio::main]
29+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
30+
example_logging()?;
31+
let route = RoutePattern::parse_str("/example/:name}")?;
32+
33+
let lifecycle = ExampleLifecycle;
34+
let agent = AgentModel::new(ExampleAgent::default, lifecycle.into_lifecycle());
35+
36+
let server = ServerBuilder::with_plane_name("Example Plane")
37+
.add_route(route, agent)
38+
.update_config(|config| {
39+
config.agent_runtime.inactive_timeout = Duration::from_secs(5 * 60);
40+
})
41+
.build()
42+
.await?;
43+
44+
let (task, handle) = server.run();
45+
46+
let shutdown = manage_handle(handle);
47+
48+
let (_, result) = tokio::join!(shutdown, task);
49+
50+
result?;
51+
println!("Server stopped successfully.");
52+
Ok(())
53+
}

server/swim_agent/src/agent_lifecycle/utility/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use crate::lanes::demand::{Cue, DemandLane};
5050
use crate::lanes::demand_map::CueKey;
5151
use crate::lanes::join_map::JoinMapAddDownlink;
5252
use crate::lanes::join_value::{JoinValueAddDownlink, JoinValueLane};
53+
use crate::lanes::supply::{Supply, SupplyLane};
5354
use crate::lanes::{DemandMapLane, JoinMapLane};
5455

5556
pub use self::downlink_builder::event::{
@@ -366,6 +367,22 @@ impl<Agent: 'static> HandlerContext<Agent> {
366367
CueKey::new(lane, key)
367368
}
368369

370+
/// Create an event handler that will supply an event to a supply lane.
371+
///
372+
/// #Arguments
373+
/// * `lane` - Projection to the supply lane.
374+
/// * `value` - The value to supply.
375+
pub fn supply<V>(
376+
&self,
377+
lane: fn(&Agent) -> &SupplyLane<V>,
378+
value: V,
379+
) -> impl EventHandler<Agent> + Send + 'static
380+
where
381+
V: Send + 'static,
382+
{
383+
Supply::new(lane, value)
384+
}
385+
369386
/// Suspend a future to be executed by the agent task. The future must result in another
370387
/// event handler that will be executed by the agent upon completion.
371388
pub fn suspend<Fut, H>(&self, future: Fut) -> impl EventHandler<Agent> + Send + 'static

server/swim_agent/src/lanes/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub mod http;
1919
mod join;
2020
pub mod map;
2121
mod queues;
22+
23+
pub mod supply;
2224
pub mod value;
2325

2426
pub use join::map as join_map;
@@ -38,6 +40,7 @@ pub use self::{
3840
join_map::JoinMapLane,
3941
join_value::JoinValueLane,
4042
map::MapLane,
43+
supply::SupplyLane,
4144
value::ValueLane,
4245
};
4346

0 commit comments

Comments
 (0)