Skip to content

Commit 2ea0fff

Browse files
authored
Merge pull request #686 from swimos/example_apps_examples
Client examples for server applications
2 parents 0b90398 + 6bc7f72 commit 2ea0fff

File tree

43 files changed

+551
-217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+551
-217
lines changed

example_apps/aggregations/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ publish = false
66

77
[dependencies]
88
swimos = { workspace = true, features = ["server", "agent"] }
9+
swimos_client = { workspace = true }
910
swimos_form = { workspace = true }
1011
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
1112
example-util = { path = "../example_util" }
1213
rand = { workspace = true }
13-
futures-util = { workspace = true }
14+
futures-util = { workspace = true }
15+
16+
[[bin]]
17+
name = "aggregations_client"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::error::Error;
2+
use swimos_client::{BasicValueDownlinkLifecycle, RemotePath, SwimClientBuilder};
3+
4+
#[tokio::main]
5+
async fn main() -> Result<(), Box<dyn Error>> {
6+
let host = "ws://127.0.0.1:8080";
7+
8+
let (client, task) = SwimClientBuilder::default().build().await;
9+
let task_handle = tokio::spawn(task);
10+
11+
let client_handle = client.handle();
12+
13+
let car_lifecycle = BasicValueDownlinkLifecycle::default()
14+
.on_event_blocking(|speed| println!("/cars/1 speed: {speed}"));
15+
client_handle
16+
.value_downlink::<u64>(RemotePath::new(host, "/cars/1", "speed"))
17+
.lifecycle(car_lifecycle)
18+
.open()
19+
.await?;
20+
21+
let area_lifecycle = BasicValueDownlinkLifecycle::default()
22+
.on_event_blocking(|speed| println!("/area/arbury average speed: {speed}"));
23+
client_handle
24+
.value_downlink::<f64>(RemotePath::new(host, "/area/arbury", "average_speed"))
25+
.lifecycle(area_lifecycle)
26+
.open()
27+
.await?;
28+
29+
let city_lifecycle = BasicValueDownlinkLifecycle::default()
30+
.on_event_blocking(|speed| println!("/city average speed: {speed}"));
31+
client_handle
32+
.value_downlink::<f64>(RemotePath::new(host, "/city", "average_speed"))
33+
.lifecycle(city_lifecycle)
34+
.open()
35+
.await?;
36+
37+
task_handle.await?;
38+
39+
Ok(())
40+
}

example_apps/aggregations/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4949
let aggregate_agent = AgentModel::new(CityAgent::default, CityLifecycle.into_lifecycle());
5050

5151
let mut builder = ServerBuilder::with_plane_name("Example Plane")
52+
.set_bind_addr("127.0.0.1:8080".parse()?)
5253
.add_route(RoutePattern::parse_str("/cars/:car_id")?, car_agent)
5354
.add_route(RoutePattern::parse_str("/city")?, aggregate_agent)
5455
.update_config(|config| {

example_apps/command_lane/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ publish = false
66

77
[dependencies]
88
swimos = { workspace = true, features = ["server", "agent"] }
9+
swimos_client = { workspace = true }
910
swimos_form = { workspace = true }
1011
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
1112
example-util = { path = "../example_util" }
13+
14+
[[bin]]
15+
name = "command_client"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use command_lane::Instruction;
2+
use std::error::Error;
3+
use swimos_client::{BasicValueDownlinkLifecycle, RemotePath, SwimClientBuilder};
4+
5+
#[tokio::main]
6+
async fn main() -> Result<(), Box<dyn Error>> {
7+
let host = "ws://127.0.0.1:8080";
8+
9+
let (client, task) = SwimClientBuilder::default().build().await;
10+
let task_handle = tokio::spawn(task);
11+
12+
let client_handle = client.handle();
13+
14+
let state_lifecycle =
15+
BasicValueDownlinkLifecycle::default().on_event_blocking(|state| println!("{state}"));
16+
client_handle
17+
.value_downlink::<i32>(RemotePath::new(host, "/example/1", "lane"))
18+
.lifecycle(state_lifecycle)
19+
.open()
20+
.await?;
21+
22+
let command_handle = client_handle
23+
.value_downlink::<Instruction>(RemotePath::new(host, "/example/1", "command"))
24+
.open()
25+
.await?;
26+
27+
for i in 0..10 {
28+
command_handle.set(Instruction::Add(i)).await?;
29+
}
30+
31+
command_handle.set(Instruction::Zero).await?;
32+
33+
task_handle.await?;
34+
35+
Ok(())
36+
}

example_apps/command_lane/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod model;
2+
3+
pub use model::Instruction;

example_apps/command_lane/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
3535
let agent = AgentModel::new(ExampleAgent::default, lifecycle.into_lifecycle());
3636

3737
let server = ServerBuilder::with_plane_name("Example Plane")
38+
.set_bind_addr("127.0.0.1:8080".parse()?)
3839
.add_route(route, agent)
3940
.update_config(|config| {
4041
config.agent_runtime.inactive_timeout = Duration::from_secs(5 * 60);

example_apps/demand_lane/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,9 @@ publish = false
66

77
[dependencies]
88
swimos = { workspace = true, features = ["server", "agent"] }
9+
swimos_client = { workspace = true }
910
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
1011
example-util = { path = "../example_util" }
12+
13+
[[bin]]
14+
name = "demand_client"

example_apps/demand_lane/src/agent.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub struct ExampleAgent {
3131
#[derive(Clone)]
3232
pub struct ExampleLifecycle;
3333

34-
const CUE_PERIOD: Duration = Duration::from_secs(5);
34+
const CUE_PERIOD: Duration = Duration::from_secs(1);
3535

3636
#[lifecycle(ExampleAgent)]
3737
impl ExampleLifecycle {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::error::Error;
2+
use swimos_client::{BasicValueDownlinkLifecycle, RemotePath, SwimClientBuilder};
3+
4+
#[tokio::main]
5+
async fn main() -> Result<(), Box<dyn Error>> {
6+
let host = "ws://127.0.0.1:8080";
7+
8+
let (client, task) = SwimClientBuilder::default().build().await;
9+
let task_handle = tokio::spawn(task);
10+
11+
let client_handle = client.handle();
12+
13+
let state_lifecycle =
14+
BasicValueDownlinkLifecycle::default().on_event_blocking(|state| println!("{state}"));
15+
let state_view = client_handle
16+
.value_downlink::<i32>(RemotePath::new(host, "/example/1", "lane"))
17+
.lifecycle(state_lifecycle)
18+
.open()
19+
.await?;
20+
21+
let demand_lifecycle =
22+
BasicValueDownlinkLifecycle::default().on_event_blocking(|state| println!("{state}"));
23+
client_handle
24+
.value_downlink::<i32>(RemotePath::new(host, "/example/1", "demand"))
25+
.lifecycle(demand_lifecycle)
26+
.open()
27+
.await?;
28+
29+
state_view.set(13).await?;
30+
31+
task_handle.await?;
32+
33+
Ok(())
34+
}

0 commit comments

Comments
 (0)