Skip to content

Commit 0d2a78a

Browse files
committed
Merge branch 'main' of https://github.com/swimos/swim-rust into dependencies
2 parents 62c972f + e4f1a80 commit 0d2a78a

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ members = [
3737
"example_apps/tutorial_app",
3838
"example_apps/tutorial_app/model",
3939
"example_apps/tutorial_app/generator",
40+
"example_apps/join_map",
4041
"example_apps/join_value",
4142
]
4243

example_apps/join_map/Cargo.toml

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

example_apps/join_map/src/agents.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use swimos::{
2+
agent::agent_lifecycle::utility::HandlerContext,
3+
agent::event_handler::{EventHandler, HandlerActionExt},
4+
agent::lanes::{JoinMapLane, MapLane},
5+
agent::projections,
6+
agent::{lifecycle, AgentLaneModel},
7+
};
8+
9+
#[derive(AgentLaneModel)]
10+
#[projections]
11+
pub struct StreetStatisticsAgent {
12+
state: MapLane<String, u64>,
13+
}
14+
15+
#[derive(Clone)]
16+
pub struct StreetStatisticsLifecycle;
17+
18+
#[lifecycle(StreetStatisticsAgent)]
19+
impl StreetStatisticsLifecycle {}
20+
21+
#[derive(AgentLaneModel)]
22+
#[projections]
23+
pub struct AggregatedStatisticsAgent {
24+
streets: JoinMapLane<String, String, u64>,
25+
}
26+
27+
#[derive(Clone)]
28+
pub struct AggregatedLifecycle;
29+
30+
#[lifecycle(AggregatedStatisticsAgent)]
31+
impl AggregatedLifecycle {
32+
#[on_start]
33+
pub fn on_start(
34+
&self,
35+
context: HandlerContext<AggregatedStatisticsAgent>,
36+
) -> impl EventHandler<AggregatedStatisticsAgent> {
37+
let california_downlink = context.add_map_downlink(
38+
AggregatedStatisticsAgent::STREETS,
39+
"california".to_string(),
40+
None,
41+
"/state/california",
42+
"state",
43+
);
44+
let texas_downlink = context.add_map_downlink(
45+
AggregatedStatisticsAgent::STREETS,
46+
"texas".to_string(),
47+
None,
48+
"/state/texas",
49+
"state",
50+
);
51+
let florida_downlink = context.add_map_downlink(
52+
AggregatedStatisticsAgent::STREETS,
53+
"florida".to_string(),
54+
None,
55+
"/state/florida",
56+
"state",
57+
);
58+
59+
context
60+
.get_agent_uri()
61+
.and_then(move |uri| context.effect(move || println!("Starting agent at: {}", uri)))
62+
.followed_by(california_downlink)
63+
.followed_by(texas_downlink)
64+
.followed_by(florida_downlink)
65+
}
66+
67+
#[on_stop]
68+
pub fn on_stop(
69+
&self,
70+
context: HandlerContext<AggregatedStatisticsAgent>,
71+
) -> impl EventHandler<AggregatedStatisticsAgent> {
72+
context
73+
.get_agent_uri()
74+
.and_then(move |uri| context.effect(move || println!("Stopping agent at: {}", uri)))
75+
}
76+
}

example_apps/join_map/src/main.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
mod agents;
16+
17+
use crate::agents::{
18+
AggregatedLifecycle, AggregatedStatisticsAgent, StreetStatisticsAgent,
19+
StreetStatisticsLifecycle,
20+
};
21+
use example_util::{example_logging, manage_handle};
22+
use std::{error::Error, time::Duration};
23+
use swimos::{
24+
agent::agent_model::AgentModel, route::RoutePattern, server::Server, server::ServerBuilder,
25+
};
26+
27+
#[tokio::main]
28+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
29+
example_logging()?;
30+
31+
let street_route = RoutePattern::parse_str("/state/:name")?;
32+
let street_agent = AgentModel::new(
33+
StreetStatisticsAgent::default,
34+
StreetStatisticsLifecycle.into_lifecycle(),
35+
);
36+
37+
let aggregated_route = RoutePattern::parse_str("/join/state/:name")?;
38+
let aggregated_agent = AgentModel::new(
39+
AggregatedStatisticsAgent::default,
40+
AggregatedLifecycle.into_lifecycle(),
41+
);
42+
43+
let server = ServerBuilder::with_plane_name("Statistics Plane")
44+
.add_route(street_route, street_agent)
45+
.add_route(aggregated_route, aggregated_agent)
46+
.update_config(|config| {
47+
config.agent_runtime.inactive_timeout = Duration::from_secs(5 * 60);
48+
})
49+
.build()
50+
.await?;
51+
52+
let (task, handle) = server.run();
53+
let shutdown = manage_handle(handle);
54+
let (_, result) = tokio::join!(shutdown, task);
55+
56+
result?;
57+
println!("Server stopped successfully.");
58+
Ok(())
59+
}

0 commit comments

Comments
 (0)