Networking for the Aritice Network

Purpose

the point of this crate is to provide networking support between peers on MAC, Linux, and Windows it will also act as the artifice server

Version details

this version adds support for both async client, and async server implementations

Example usage

it would be better to use a management crate but these example would technically work

Async

Dependencies

toml tokio = {version = "0.2.21", features = ["full"]}

Async Client

```rust use networking::{asyncronous::AsyncHost, ArtificeConfig, ArtificePeer}; use std::fs::File; use std::io::Read;

[tokio::main]

async fn main() -> Result<(), Box> { let mut configfile = File::open("host.json").unwrap(); let mut confvec = String::new(); configfile.readtostring(&mut confvec).unwrap(); let config: ArtificeConfig = serdejson::fromstr(&confvec).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = Vec::new(); file.readtoend(&mut invec).unwrap(); let string = String::fromutf8(invec).unwrap(); // println!("invec: {}", invec); let peer: ArtificePeer = serdejson::fromstr(&string).unwrap(); let host = AsyncHost::clientonly(&config).await.unwrap(); let mut stream = host.connect(peer).await.unwrap(); let mut buffer = Vec::new(); println!("about to read from sream"); println!( "got {} bytes from server", stream.recv(&mut buffer).await.unwrap() ); println!("read from stream"); let string = String::fromutf8(buffer).unwrap(); println!("got message: {} from server", string); Ok(()) }

```

Async Server

```rust use networking::{asyncronous::AsyncHost, ArtificeConfig, ArtificePeer}; use std::fs::File; use std::io::Read;

[tokio::main]

async fn main() -> Result<(), Box> { // currently not functioning let mut configfile = File::open("host.json").unwrap(); let mut confvec = String::new(); configfile.readtostring(&mut confvec).unwrap(); let config: ArtificeConfig = serdejson::fromstr(&confvec).unwrap(); let mut host = AsyncHost::fromhostconfig(&config).await.unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = String::new(); file.readtostring(&mut invec).unwrap(); let peer: ArtificePeer = serdejson::fromstr(&invec).unwrap(); while let Some(Ok(mut stream)) = host.incoming()?.await { // make sure you got a connection from the correct peer asserteq!(peer, *stream.peer()); println!("sending message hello world"); stream.send(b"hello world").await.unwrap(); } Ok(()) }

```

Sync

Sync Client

```rust use networking::{ArtificeConfig, ArtificeHost, ArtificePeer}; use std::fs::File; use std::io::Read; fn main() { let mut configfile = File::open("host.json").unwrap(); let mut confvec = String::new(); configfile.readtostring(&mut confvec).unwrap(); let config: ArtificeConfig = serdejson::fromstr(&confvec).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = Vec::new(); file.readtoend(&mut invec).unwrap(); let string = String::fromutf8(invec).unwrap(); // println!("invec: {}", invec); let peer: ArtificePeer = serdejson::fromstr(&string).unwrap(); let host = ArtificeHost::clientonly(&config); let mut stream = host.connect(peer).unwrap(); let mut buffer = Vec::new(); println!("about to read from sream"); println!( "got {} bytes from server", stream.recv(&mut buffer).unwrap() ); println!("read from stream"); let string = String::fromutf8(buffer).unwrap(); println!("got message: {} from server", string); //stream.write(&buffer).unwrap(); }

```

Sync Server

```rust use networking::{ArtificeConfig, ArtificeHost, ArtificePeer}; use std::fs::File; use std::io::{Read}; fn main() { let mut configfile = File::open("host.json").unwrap(); let mut confvec = String::new(); configfile.readtostring(&mut confvec).unwrap(); let config: ArtificeConfig = serdejson::fromstr(&confvec).unwrap(); let host = ArtificeHost::fromhostdata(&config).unwrap(); let mut file = File::open("peer.json").unwrap(); let mut invec = String::new(); file.readtostring(&mut invec).unwrap(); let peer: ArtificePeer = serdejson::fromstr(&invec).unwrap(); for netstream in host { let mut stream = netstream.unwrap(); println!("about to write to stream"); stream .send(&"hello world".tostring().into_bytes()) .unwrap(); // do something with the stream example: if *stream.peer() == peer { // correct peer } } }

```