-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8c323a
commit 6527b86
Showing
12 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "pegboard" | ||
version = "0.0.1" | ||
edition = "2018" | ||
authors = ["Rivet Gaming, LLC <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
||
[dependencies] | ||
chirp-workflow = { path = "../../../lib/chirp-workflow/core" } | ||
tokio-tungstenite = "0.23.1" | ||
serde = { version = "1.0.198", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[service] | ||
name = "pegboard" | ||
|
||
[runtime] | ||
kind = "rust" | ||
|
||
[package] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use chirp_workflow::prelude::*; | ||
|
||
pub mod ops; | ||
pub mod utils; | ||
pub mod workflows; | ||
|
||
pub fn registry() -> WorkflowResult<Registry> { | ||
use workflows::*; | ||
|
||
let mut registry = Registry::new(); | ||
registry.register_workflow::<ws::Workflow>()?; | ||
|
||
Ok(registry) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod ws; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use std::{ | ||
collections::HashMap, | ||
net::SocketAddr, | ||
sync::{Arc, RwLock}, | ||
}; | ||
|
||
use chirp_workflow::prelude::*; | ||
use futures_util::FutureExt; | ||
// use tokio::net::{TcpListener, TcpStream}; | ||
// use tokio_tungstenite::tungstenite::protocol::Message; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct Input {} | ||
|
||
#[workflow] | ||
pub async fn pegboard_ws(ctx: &mut WorkflowCtx, input: &Input) -> GlobalResult<()> { | ||
// let addr = "127.0.0.1:8080"; | ||
// let listener = TcpListener::bind(&addr).await?; | ||
// println!("Listening on: {}", addr); | ||
|
||
let conns = Arc::new(RwLock::new(HashMap::<(), ()>::new())); | ||
|
||
ctx.try_join(( | ||
closure(|ctx| socket_thread(ctx, conns.clone()).boxed()), | ||
closure(|ctx| signal_thread(ctx, conns.clone()).boxed()), | ||
)) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn socket_thread( | ||
ctx: &mut WorkflowCtx, | ||
conns: Arc<RwLock<HashMap<(), ()>>>, | ||
) -> GlobalResult<()> { | ||
ctx.repeat(|ctx| { | ||
async move { | ||
if let Ok((stream, addr)) = listener.accept().await { | ||
handle_connection(stream, addr).await; | ||
} else { | ||
tracing::error!("failed to connect websocket"); | ||
} | ||
|
||
Ok(Loop::Continue) | ||
}.boxed() | ||
) | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn signal_thread( | ||
ctx: &mut WorkflowCtx, | ||
conns: Arc<RwLock<HashMap<(), ()>>>, | ||
) -> GlobalResult<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn handle_connection(ctx, raw_stream: TcpStream, addr: SocketAddr) { | ||
ctx.spawn(|ctx| async move { | ||
let ws_stream = tokio_tungstenite::accept_async(raw_stream).await?; | ||
let (mut write, mut read) = ws_stream.split(); | ||
|
||
println!("New WebSocket connection: {}", addr); | ||
|
||
while let Some(Ok(msg)) = read.next().await { | ||
if msg.is_text() || msg.is_binary() { | ||
write.send(msg).await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
}.boxed()).await | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Hash)] | ||
struct FooInput {} | ||
|
||
#[activity(Foo)] | ||
async fn foo(ctx: &ActivityCtx, input: &FooInput) -> GlobalResult<()> { | ||
Ok(()) | ||
} |