Skip to content

Commit aad43d0

Browse files
authored
Merge pull request #687 from swimos/devguide
New developer guide structure
2 parents 2ea0fff + e34f2bb commit aad43d0

File tree

10 files changed

+37
-250
lines changed

10 files changed

+37
-250
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ members = [
4040
"example_apps/join_value",
4141
"example_apps/aggregations",
4242
"example_apps/time_series",
43-
"example_apps/devguide/2_2/*",
44-
"example_apps/devguide/2_3/*",
43+
"example_apps/devguide",
4544
]
4645

4746
[workspace.package]
@@ -173,4 +172,4 @@ waker-fn = "1.1.0"
173172
num = "0.4"
174173
smol_str = "0.2.0"
175174
http-body-util = "0.1.2"
176-
hyper-util = "0.1.5"
175+
hyper-util = "0.1.5"

example_apps/devguide/2_2/example_client/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example_apps/devguide/2_2/example_client/src/main.rs

Lines changed: 0 additions & 54 deletions
This file was deleted.

example_apps/devguide/2_2/example_server/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example_apps/devguide/2_2/example_server/src/main.rs

Lines changed: 0 additions & 104 deletions
This file was deleted.

example_apps/devguide/2_3/example_client/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example_apps/devguide/2_3/example_server/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example_apps/devguide/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "devguide2"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
license.workspace = true
7+
homepage.workspace = true
8+
publish = false
9+
10+
[dependencies]
11+
swimos = { workspace = true, features = ["server", "agent"] }
12+
swimos_client = { workspace = true }
13+
swimos_form = { workspace = true }
14+
tokio = { workspace = true, features = ["rt-multi-thread", "macros"] }
15+
16+
[[bin]]
17+
name = "devguide_client"
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use swimos_client::{BasicValueDownlinkLifecycle, DownlinkConfig, RemotePath, SwimClientBuilder};
1+
use std::error::Error;
2+
use swimos_client::{BasicValueDownlinkLifecycle, RemotePath, SwimClientBuilder};
23
use swimos_form::Form;
34

45
#[derive(Debug, Form, Copy, Clone)]
@@ -8,12 +9,12 @@ pub enum Operation {
89
}
910

1011
#[tokio::main]
11-
async fn main() {
12+
async fn main() -> Result<(), Box<dyn Error>> {
1213
// Build a Swim Client using the default configuration.
1314
// The `build` method returns a `SwimClient` instance and its internal
1415
// runtime future that is spawned below.
1516
let (client, task) = SwimClientBuilder::default().build().await;
16-
let _client_task = tokio::spawn(task);
17+
let client_task = tokio::spawn(task);
1718
let handle = client.handle();
1819

1920
// Build a path the downlink.
@@ -28,24 +29,14 @@ async fn main() {
2829
);
2930

3031
let lifecycle = BasicValueDownlinkLifecycle::<usize>::default()
31-
// Register an event handler that is invoked when the downlink connects to the agent.
32-
.on_linked_blocking(|| println!("Downlink linked"))
33-
// Register an event handler that is invoked when the downlink synchronises its state.
34-
// with the agent.
35-
.on_synced_blocking(|value| println!("Downlink synced with: {value:?}"))
3632
// Register an event handler that is invoked when the downlink receives an event.
3733
.on_event_blocking(|value| println!("Downlink event: {value:?}"));
3834

39-
// Build our downlink.
40-
//
41-
// This operation may fail if there is a connection issue.
42-
let _state_downlink = handle
35+
handle
4336
.value_downlink::<usize>(state_path)
4437
.lifecycle(lifecycle)
45-
.downlink_config(DownlinkConfig::default())
4638
.open()
47-
.await
48-
.expect("Failed to open downlink");
39+
.await?;
4940

5041
let exec_path = RemotePath::new(
5142
// The host address
@@ -57,17 +48,11 @@ async fn main() {
5748
"exec",
5849
);
5950

60-
let exec_downlink = handle
61-
.value_downlink::<Operation>(exec_path)
62-
.downlink_config(DownlinkConfig::default())
63-
.open()
64-
.await
65-
.expect("Failed to open exec downlink");
51+
let exec_downlink = handle.value_downlink::<Operation>(exec_path).open().await?;
6652

67-
exec_downlink.set(Operation::Add(1000)).await.unwrap();
68-
exec_downlink.set(Operation::Sub(13)).await.unwrap();
53+
exec_downlink.set(Operation::Add(1000)).await?;
54+
exec_downlink.set(Operation::Sub(13)).await?;
6955

70-
tokio::signal::ctrl_c()
71-
.await
72-
.expect("Failed to listen for ctrl-c.");
56+
client_task.await?;
57+
Ok(())
7358
}

example_apps/devguide/2_3/example_server/src/main.rs renamed to example_apps/devguide/src/main.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
use std::{error::Error, time::Duration};
2+
3+
use swimos::agent::event_handler::HandlerActionExt;
4+
use swimos::agent::lanes::CommandLane;
15
use swimos::{
26
agent::{
37
agent_lifecycle::HandlerContext, agent_model::AgentModel, event_handler::EventHandler,
48
lanes::ValueLane, lifecycle, projections, AgentLaneModel,
59
},
610
route::RoutePattern,
7-
server::{Server, ServerBuilder, ServerHandle},
11+
server::{Server, ServerBuilder},
812
};
9-
10-
use std::{error::Error, time::Duration};
11-
use swimos::agent::event_handler::HandlerActionExt;
12-
use swimos::agent::lanes::CommandLane;
1313
use swimos_form::Form;
1414

1515
// Note how as this is a custom type we need to derive `Form` for it.
@@ -115,24 +115,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
115115

116116
// Run the server. A tuple of the server's runtime
117117
// future and a handle to the runtime is returned.
118-
let (task, handle) = server.run();
119-
// Watch for ctrl+c signals
120-
let shutdown = manage_handle(handle);
121-
122-
// Join on the server and ctrl+c futures.
123-
let (_, result) = tokio::join!(shutdown, task);
118+
let (task, _handle) = server.run();
119+
task.await?;
124120

125-
result?;
126-
println!("Server stopped successfully.");
127121
Ok(())
128122
}
129-
130-
// Utility function for awaiting a stop signal in the terminal.
131-
async fn manage_handle(mut handle: ServerHandle) {
132-
tokio::signal::ctrl_c()
133-
.await
134-
.expect("Failed to register interrupt handler.");
135-
136-
println!("Stopping server.");
137-
handle.stop();
138-
}

0 commit comments

Comments
 (0)