Skip to content

Commit 5ca8cd5

Browse files
committed
Resolves PR comments
1 parent 553f4a7 commit 5ca8cd5

File tree

6 files changed

+25
-160
lines changed

6 files changed

+25
-160
lines changed

example_apps/ripple/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,4 @@ tokio = { workspace = true, features = ["rt-multi-thread", "macros", "sync", "io
2020
tokio-util = { workspace = true }
2121
rand = { workspace = true }
2222
parking_lot = { workspace = true, features = ["send_guard"] }
23-
chrono = { workspace = true }
24-
swimos_recon = { workspace = true }
23+
chrono = { workspace = true }

example_apps/ripple/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ $ cargo run --bin ripple
1515
```
1616

1717
Internally, this will spawn two servers: the Swim server which is running at localhost:9001 and a UI server running
18-
at http://localhost:9002/index.html. Using your browser, navigate to http://localhost:9002/index.html and randomly click
18+
at http://localhost:9002. Using your browser, navigate to http://localhost:9002/index.html and randomly click
1919
on the screen to generate 'ripples'. These ripples are propagated to all users which are connected to the Swim server.
2020
You may also hold your mouse's left button down to generate a 'charge' which is denoted by a larger circle displayed on
2121
the screen.

example_apps/ripple/src/agent.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::collections::HashMap;
16+
use std::fmt::{Display, Formatter};
1617
use std::sync::Arc;
1718
use std::time::Duration;
1819

@@ -110,7 +111,7 @@ pub struct Charge {
110111
/// The charge's current Y position.
111112
y: f64,
112113
/// The color of the charge.
113-
color: Color,
114+
color: String,
114115
/// The radius of the charge.
115116
#[form(name = "r")]
116117
radius: i32,
@@ -134,7 +135,7 @@ pub enum ChargeAction {
134135
/// The charge's current Y position.
135136
y: f64,
136137
/// The color of the charge.
137-
color: Color,
138+
color: String,
138139
/// The radius of the charge.
139140
#[form(name = "r")]
140141
radius: i32,
@@ -149,7 +150,7 @@ pub enum ChargeAction {
149150
/// The charge's current Y position.
150151
y: f64,
151152
/// The color of the charge.
152-
color: Color,
153+
color: String,
153154
/// The radius of the charge.
154155
#[form(name = "r")]
155156
radius: i32,
@@ -303,7 +304,7 @@ pub struct Ripple {
303304
/// Phases are the subsequent ripples that are generated by a ripple.
304305
phases: Vec<f64>,
305306
/// The color of the charge.
306-
color: Color,
307+
color: String,
307308
}
308309

309310
impl Default for Ripple {
@@ -332,7 +333,7 @@ impl Ripple {
332333
x: rng.gen::<f64>(),
333334
y: rng.gen::<f64>(),
334335
phases,
335-
color: Color::select_random(rng),
336+
color: Color::select_random(rng).to_string(),
336337
}
337338
}
338339
}
@@ -348,6 +349,16 @@ pub enum Color {
348349
Cyan,
349350
}
350351

352+
impl Display for Color {
353+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
354+
match self {
355+
Color::Green => write!(f, "#80dc1a"),
356+
Color::Magenta => write!(f, "#c200fa"),
357+
Color::Cyan => write!(f, "#56dbb6"),
358+
}
359+
}
360+
}
361+
351362
impl Color {
352363
/// Returns a random color generated using `rng`.
353364
fn select_random(rng: &mut StdRng) -> Color {

example_apps/ripple/src/main.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,27 @@
2525
//! http://127.0.0.1:9002/index.html
2626
//! ```
2727
28+
use std::convert::Infallible;
29+
use std::{error::Error, net::SocketAddr, time::Duration};
30+
2831
use axum::body::Body;
2932
use axum::http::{header, HeaderValue};
3033
use axum::response::IntoResponse;
3134
use axum::routing::get;
3235
use axum::Router;
33-
use futures::future::{select, Either};
3436
use rand::rngs::StdRng;
3537
use rand::SeedableRng;
36-
use std::convert::Infallible;
37-
use std::future::IntoFuture;
38-
use std::{error::Error, net::SocketAddr, pin::pin, sync::Arc, time::Duration};
3938
use tokio::net::TcpListener;
40-
use tokio::sync::Notify;
4139
use tracing_subscriber::filter::LevelFilter;
4240

4341
use swimos::agent::agent_model::AgentModel;
4442
use swimos::server::{Server, ServerBuilder};
4543
use swimos_utilities::routing::RoutePattern;
46-
use swimos_utilities::trigger::trigger;
4744

4845
use crate::agent::{MirrorAgent, MirrorLifecycle};
4946

5047
mod agent;
51-
mod util;
5248

53-
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
5449
static HTML: HeaderValue = HeaderValue::from_static("text/html; charset=utf-8");
5550
const INDEX: &[u8] = include_bytes!("../ui/index.html");
5651
const RIPPLE: &[u8] = include_bytes!("../ui/swim-ripple.js");
@@ -60,45 +55,21 @@ const RIPPLE_MAP: &[u8] = include_bytes!("../ui/swim-ripple.js.map");
6055
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
6156
example_logging()?;
6257

63-
let shutdown_tx = Arc::new(Notify::new());
64-
let shutdown_rx = shutdown_tx.clone();
65-
6658
let server_task = tokio::spawn(run_swim_server());
67-
let ui_task = tokio::spawn(ui_server(shutdown_rx, SHUTDOWN_TIMEOUT));
59+
let ui_task = tokio::spawn(ui_server());
6860
ui_task.await??;
6961
server_task.await??;
7062

7163
Ok(())
7264
}
7365

7466
/// Spawns the UI server on 0.0.0.0:9002.
75-
///
76-
/// # Arguments:
77-
/// * `shutdown_signal` - Future which completes when the server should terminate.
78-
/// * `shutdown_timeout` - How long to wait for the UI server to terminate before returning.
79-
async fn ui_server(
80-
shutdown_signal: Arc<Notify>,
81-
shutdown_timeout: Duration,
82-
) -> Result<(), Box<dyn Error + Send + Sync>> {
67+
async fn ui_server() -> Result<(), Box<dyn Error + Send + Sync>> {
8368
let app = ui_server_router();
8469
let bind_to: SocketAddr = "0.0.0.0:9002".parse()?;
85-
let (stop_tx, stop_rx) = trigger();
8670
let listener = TcpListener::bind(bind_to).await?;
8771

88-
let server =
89-
axum::serve(listener, app.into_make_service()).with_graceful_shutdown(async move {
90-
let _ = stop_rx.await;
91-
});
92-
93-
let server_task = pin!(server.into_future());
94-
let shutdown_notified = pin!(shutdown_signal.notified());
95-
match select(server_task, shutdown_notified).await {
96-
Either::Left((result, _)) => result?,
97-
Either::Right((_, server)) => {
98-
assert!(stop_tx.trigger());
99-
tokio::time::timeout(shutdown_timeout, server).await??;
100-
}
101-
}
72+
axum::serve(listener, app.into_make_service()).await?;
10273

10374
Ok(())
10475
}

example_apps/ripple/src/util.rs

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

example_apps/ripple/ui/swim-ripple.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)