Skip to content

Commit

Permalink
Merge pull request #24 from jabibamman/feat/server-client-connection
Browse files Browse the repository at this point in the history
Feat(Server) - client/server connexion + write message
  • Loading branch information
jabibamman authored Nov 15, 2023
2 parents 71df860 + c5d4772 commit e1832e4
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 3 deletions.
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ edition = "2021"
image = { version = "0.24.7", features = [] }
complex = { path = "../complex" }
shared = { path = "../shared" }
server = { path = "../server" }
31 changes: 31 additions & 0 deletions client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
use server::services::{connect::connect, write::write};

/// Connect to the server and send a message
///
/// # Arguments
///
/// * `host` - Hostname
/// * `port` - Port
///
/// # Return
///
/// * `Result<TcpStream, std::io::Error>` - TcpStream
///
/// # Example
///
/// ```
/// use client::client::connect_server;
///
/// let stream = connect_server("localhost", "8080");
/// ```
///
/// # Panic
///
/// * `std::io::Error` - If the connection failed
///
pub fn connect_server(host: &str, port: &str) -> std::io::Result<std::net::TcpStream> {
let stream = connect(format!("{}:{}", host, port))?;

write(stream, "Hello World !")?;

Ok(())
}
3 changes: 3 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ edition = "2021"
[dependencies]
complex = { path = "../complex" }
shared = { path = "../shared" }

[lib]
path = "src/lib.rs"
1 change: 1 addition & 0 deletions server/src/mod.rs → server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod handler;
pub mod image;
pub mod services;
3 changes: 0 additions & 3 deletions server/src/main.rs

This file was deleted.

17 changes: 17 additions & 0 deletions server/src/services/connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use std::io::Result;
use std::net::TcpStream;

/// Connect to a server
///
/// # Arguments
///
/// * `address` - Address
///
/// # Return
///
/// * `Result<TcpStream, std::io::Error>` - TcpStream
pub fn connect(address: &str) -> Result<TcpStream> {
let stream = TcpStream::connect(address)?;

Ok(stream)
}
2 changes: 2 additions & 0 deletions server/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod connect;
pub mod write;
19 changes: 19 additions & 0 deletions server/src/services/write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::io::Result;
use std::io::Write;
use std::net::TcpStream;

/// Write a string to a stream
///
/// # Arguments
///
/// * `stream` - TcpStream
/// * `message` - String
///
/// # Return
///
/// * `Result<TcpStream, std::io::Error>` - TcpStream
pub fn write(mut stream: TcpStream, message: &str) -> Result<TcpStream> {
stream.write(format!("${}", message).as_bytes())?;

Ok(stream)
}

0 comments on commit e1832e4

Please sign in to comment.