Skip to content

Commit

Permalink
chore: rename socket to tcp
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Apr 8, 2024
1 parent 65c0dee commit fbaf3ff
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 62 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ wait-on file /path/to/file
### Wait for a Socket to be available using TCP Protocol

```bash
wait-on socket -i 127.0.0.1 -p 8080
wait-on tcp -i 127.0.0.1 -p 8080
```

## License
Expand Down
2 changes: 1 addition & 1 deletion src/bin/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod file;
pub mod socket;
pub mod tcp;
8 changes: 4 additions & 4 deletions src/bin/command/socket.rs → src/bin/command/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ use std::net::IpAddr;
use anyhow::Result;
use clap::Args;

use wait_on::resource::socket::SocketWaiter;
use wait_on::resource::tcp::TcpWaiter;
use wait_on::{WaitOptions, Waitable};

#[derive(Args, Debug)]
pub struct SocketOpt {
pub struct TcpOpt {
#[clap(short = 'p', long = "port")]
pub port: u16,
#[clap(short = 'i', long = "ip", default_value = "127.0.0.1")]
pub addr: IpAddr,
}

impl SocketOpt {
impl TcpOpt {
pub async fn exec(&self) -> Result<()> {
let waiter = SocketWaiter::new(self.addr, self.port);
let waiter = TcpWaiter::new(self.addr, self.port);
waiter.wait(WaitOptions::default()).await
}
}
8 changes: 4 additions & 4 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::Result;
use clap::Parser;

use self::command::file::FileOpt;
use self::command::socket::SocketOpt;
use self::command::tcp::TcpOpt;

#[derive(Debug, Parser)]
#[command(
Expand All @@ -16,8 +16,8 @@ use self::command::socket::SocketOpt;
pub enum Command {
/// Wait on a file to be available
File(FileOpt),
/// Wait on a socket to be available using the TCP Protocol
Socket(SocketOpt),
/// Wait on a TCP connection to be available
Tcp(TcpOpt),
}

#[derive(Debug, Parser)]
Expand All @@ -32,6 +32,6 @@ async fn main() -> Result<()> {

match args.command {
Command::File(opt) => opt.exec().await,
Command::Socket(opt) => opt.exec().await,
Command::Tcp(opt) => opt.exec().await,
}
}
36 changes: 18 additions & 18 deletions src/resource/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ impl FileWaiter {
}
}

impl Waitable for FileWaiter {
async fn wait(self, _: WaitOptions) -> Result<()> {
let (file_exists_handler, rx) = FileExistsHandler::new();
let mut watcher = notify::recommended_watcher(file_exists_handler).unwrap();
let parent = self.path.parent().unwrap();

watcher
.watch(parent, notify::RecursiveMode::NonRecursive)
.unwrap();

if rx.recv().is_ok() {
watcher.unwatch(parent).unwrap();
}

Ok(())
}
}

struct FileExistsHandler {
tx: Sender<()>,
}
Expand All @@ -37,21 +55,3 @@ impl EventHandler for FileExistsHandler {
}
}
}

impl Waitable for FileWaiter {
async fn wait(self, _: WaitOptions) -> Result<()> {
let (file_exists_handler, rx) = FileExistsHandler::new();
let mut watcher = notify::recommended_watcher(file_exists_handler).unwrap();
let parent = self.path.parent().unwrap();

watcher
.watch(parent, notify::RecursiveMode::NonRecursive)
.unwrap();

if rx.recv().is_ok() {
watcher.unwatch(parent).unwrap();
}

Ok(())
}
}
8 changes: 4 additions & 4 deletions src/resource/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@
//! own configuration based on the protocols used.

pub mod file;
pub mod socket;
pub mod tcp;

use anyhow::Result;

use crate::{WaitOptions, Waitable};

use self::file::FileWaiter;
use self::socket::SocketWaiter;
use self::tcp::TcpWaiter;

pub enum Resource {
File(FileWaiter),
Socket(SocketWaiter),
Tcp(TcpWaiter),
}

impl Waitable for Resource {
async fn wait(self, options: WaitOptions) -> Result<()> {
match self {
Resource::File(file) => file.wait(options).await,
Resource::Socket(socket) => socket.wait(options).await,
Resource::Tcp(tcp) => tcp.wait(options).await,
}
}
}
60 changes: 30 additions & 30 deletions src/resource/socket.rs → src/resource/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use tokio::net::{TcpListener, TcpStream};
use crate::{WaitOptions, Waitable};

/// Listens on a specific IP Address and Port using TCP protocol
pub struct SocketWaiter {
pub struct TcpWaiter {
pub addr: IpAddr,
pub port: u16,
}

impl SocketWaiter {
impl TcpWaiter {
pub fn new(addr: IpAddr, port: u16) -> Self {
Self { addr, port }
}
Expand All @@ -25,6 +25,34 @@ impl SocketWaiter {
}
}

impl Waitable for TcpWaiter {
async fn wait(self, _: WaitOptions) -> Result<()> {
let tcp_listener = TcpListener::bind(self.socket()).await?;
let (socket, _) = tcp_listener.accept().await?;
let mut socket = PacketExtractor::<8>::read(socket).await?;

tokio::spawn(async move {
let mut buf = vec![0; 1024];

loop {
let n = socket
.read(&mut buf)
.await
.expect("failed to read data from socket");

if n == 0 {
// socket closed
return;
}
}
})
.await
.map_err(|err| Error::msg(err.to_string()))?;

Ok(())
}
}

#[pin_project]
pub struct PacketExtractor<const B: usize> {
pub header: [u8; B],
Expand Down Expand Up @@ -97,31 +125,3 @@ impl<const B: usize> AsyncWrite for PacketExtractor<B> {
extractor.socket.poll_shutdown(cx)
}
}

impl Waitable for SocketWaiter {
async fn wait(self, _: WaitOptions) -> Result<()> {
let tcp_listener = TcpListener::bind(self.socket()).await?;
let (socket, _) = tcp_listener.accept().await?;
let mut socket = PacketExtractor::<8>::read(socket).await?;

tokio::spawn(async move {
let mut buf = vec![0; 1024];

loop {
let n = socket
.read(&mut buf)
.await
.expect("failed to read data from socket");

if n == 0 {
// socket closed
return;
}
}
})
.await
.map_err(|err| Error::msg(err.to_string()))?;

Ok(())
}
}

0 comments on commit fbaf3ff

Please sign in to comment.