diff --git a/examples/http_client/Cargo.toml b/examples/http_client/Cargo.toml index 537512fc2..ee4cdaaca 100644 --- a/examples/http_client/Cargo.toml +++ b/examples/http_client/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] rust-ini = "0.20.0" -swarm_nl = { path = "../swarm_nl", features = ["tokio-runtime"] } +swarm_nl = { git = "https://github.com/algorealmInc/SwarmNL.git", features = ["tokio-runtime"] } tokio = { version = "1.37.0", features = ["full"] } # async-std = { version = "1.12", features = ["attributes"] } reqwest = { version = "0.12.9", features = ["json"] } diff --git a/examples/http_client/Dockerfile b/examples/http_client/Dockerfile new file mode 100644 index 000000000..d831e619b --- /dev/null +++ b/examples/http_client/Dockerfile @@ -0,0 +1,23 @@ +# Use a Rust base image +FROM rust:latest + +# Install tmux and other utilities +RUN apt-get update && apt-get install -y tmux bash && apt-get clean + +# Set the working directory +WORKDIR /usr/src/swarmnl-http-client-demo + +# Copy the Rust project into the container +COPY . . + +# Prebuild dependencies to speed up the runtime builds +RUN cargo build --release --features=first-node && \ + cargo build --release --features=second-node && \ + cargo build --release --features=third-node + +# Copy the tmux startup script +COPY run_nodes.sh /run_nodes.sh +RUN chmod +x /run_nodes.sh + +# Start tmux with the script +CMD ["/run_nodes.sh"] diff --git a/examples/http_client/README.md b/examples/http_client/README.md new file mode 100644 index 000000000..e5f1b4206 --- /dev/null +++ b/examples/http_client/README.md @@ -0,0 +1,52 @@ +# HTTP client example + +SwarmNL supports seamless integration with HTTP clients working in the application layer by providing interfaces to handle network events as they occur. This example demonstrates how to send an HTTP POST request to a remote server in response to incoming replication data events. + +To run this example, cd into the root of this directory and in separate terminals launch the following commands: + +```bash +cargo run --features=first-node +``` + +Then the second node: + +```bash +cargo run --features=second-node +``` + +And the third node: + +```bash +cargo run --features=first-node +``` + +Replicate data across the network: + +```bash +# in the first terminal +repl wonderful + +# in the second terminal +repl great + +# in the third terminal +repl amazing +``` + +You should see the message `Successfully sent POST request.` printed to the terminal. + +## Run with Docker + +Build: + +```bash +docker build -t http-client . +``` + +Run: + +```bash +docker run -it http-client +``` + +The Docker image will open three terminals and the script will submit commands to each one to demonstrate the example. To quit, use the `exit` command in each terminal. \ No newline at end of file diff --git a/examples/http_client/run_nodes.sh b/examples/http_client/run_nodes.sh new file mode 100644 index 000000000..58fcb0a98 --- /dev/null +++ b/examples/http_client/run_nodes.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Start tmux session +tmux new-session -d -s rust-nodes "cargo run --features=first-node" +tmux split-window -h "cargo run --features=second-node" +tmux split-window -v "cargo run --features=third-node" + +# Arrange panes for better layout +tmux select-layout tiled + +# Give the nodes some time to start +sleep 10 + +# Send commands to each pane +# Pane 0 (first node) +tmux send-keys -t rust-nodes:0.0 "repl wonderful" C-m +sleep 3 + +# Pane 1 (second node) +tmux send-keys -t rust-nodes:0.1 "repl great" C-m +sleep 3 + +# Pane 2 (third node) +tmux send-keys -t rust-nodes:0.2 "repl amazing" C-m +sleep 3 + +# Attach to the session so you can observe the output +tmux attach-session -t rust-nodes + +# Exit the sessions +tmux send-keys -t rust-nodes:0.0 "exit" C-m +tmux send-keys -t rust-nodes:0.1 "exit" C-m +tmux send-keys -t rust-nodes:0.2 "exit" C-m \ No newline at end of file diff --git a/examples/http_client/src/main.rs b/examples/http_client/src/main.rs index f2950e6cf..7f96a08af 100644 --- a/examples/http_client/src/main.rs +++ b/examples/http_client/src/main.rs @@ -6,6 +6,8 @@ //! providing interfaces to handle network events as they occur. This example demonstrates how to //! send an HTTP POST request to a remote server in response to incoming replication data events. +#![allow(unused_variables, dead_code, unused_imports)] + use std::{ collections::HashMap, io::{self, Write},