Skip to content

Commit

Permalink
Refractor code to protocol mod
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Mar 25, 2022
1 parent 25f881f commit 2fbde65
Show file tree
Hide file tree
Showing 10 changed files with 349 additions and 171 deletions.
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
core::{discovery::Discovery, error::Error, io::*, peer::*, site::*, user::*},
environment::ENV,
io::db::DbManager,
net::protocol::Protocol,
protocol::{api::Request, Protocol},
utils::to_json_value,
};

Expand Down
2 changes: 1 addition & 1 deletion src/io/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
discovery::tracker::IpPort,
environment::ENV,
io::utils::check_file_integrity,
net::protocol::Protocol,
protocol::{api::Request, Protocol},
};

impl Site {
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod discovery;
pub mod environment;
pub mod io;
pub mod net;
pub mod protocol;
pub mod utils;

use crate::{
Expand All @@ -16,6 +17,7 @@ use crate::{
core::{error::Error, site::Site},
environment::*,
io::db::DbManager,
protocol::Protocol,
};

#[tokio::main]
Expand Down
138 changes: 0 additions & 138 deletions src/net/builders.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod builders;
pub mod protocol;
pub mod request;
pub mod response;
53 changes: 24 additions & 29 deletions src/net/protocol.rs → src/net/request.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
use crate::{
core::error::Error,
protocol::{
api::Request,
builders::{request::*, *},
Protocol,
},
};
use serde::Serialize;
use serde_json::json;
use zeronet_protocol::{message::Response, templates::*, ZeroConnection};

use crate::core::error::Error;

use super::builders::*;

pub struct Protocol<'a>(&'a mut ZeroConnection);
use zeronet_protocol::{message::Response, templates::*};

impl<'a> Protocol<'a> {
pub fn new(connection: &'a mut ZeroConnection) -> Self {
Protocol(connection)
}

pub fn handler(&mut self, handler: &mut dyn FnMut(String)) {
handler("Protocol::handler".to_string());
}

async fn jsoned_req<T: Serialize>(&mut self, cmd: &str, req: T) -> Result<Response, Error> {
let res = self.0.request(cmd, json!(req)).await?;
Ok(res)
}

async fn invoke_with_builder<T: Serialize>(
pub async fn invoke_with_builder<T: Serialize>(
&mut self,
builder: (&str, T),
) -> Result<Response, Error> {
Expand All @@ -32,63 +26,64 @@ impl<'a> Protocol<'a> {
}

///https://docs.zeronet.dev/1DeveLopDZL1cHfKi8UXHh2UBEhzH6HhMp/help_zeronet/network_protocol/
impl<'a> Protocol<'a> {
#[async_trait::async_trait]
impl<'a> Request for Protocol<'a> {
///#handshake
pub async fn handshake(&mut self) -> Result<Handshake, Error> {
let builder = build_handshake();
async fn handshake(&mut self) -> Result<Handshake, Error> {
let builder = handshake();
let res = self.invoke_with_builder(builder).await?;
let body: Handshake = res.body()?;
Ok(body)
}

///#ping
pub async fn ping(&mut self) -> Result<bool, Error> {
async fn ping(&mut self) -> Result<bool, Error> {
let res = self.0.request("ping", json!({})).await?;
let res: PingResponse = res.body()?;
Ok(res.body == "Pong!")
}

///#getFile
pub async fn get_file(
async fn get_file(
&mut self,
site: String,
inner_path: String,
) -> Result<GetFileResponse, Error> {
//TODO!: Remove default values from builder, file_size and location
let builder = build_get_file(site, inner_path, 0, 0);
let builder = get_file(site, inner_path, 0, 0);
let res = self.invoke_with_builder(builder).await?;
let body: GetFileResponse = res.body()?;
Ok(body)
}

///#streamFile
pub async fn stream_file(
async fn stream_file(
&mut self,
site: String,
inner_path: String,
) -> Result<StreamFileResponse, Error> {
//TODO!: Remove default values from builder, size
let builder = build_stream_file(site, inner_path, 0);
let builder = stream_file(site, inner_path, 0);
let res = self.invoke_with_builder(builder).await?;
let body: StreamFileResponse = res.body()?;
Ok(body)
}

///#listModified
pub async fn list_modified(
async fn list_modified(
&mut self,
site: String,
since: usize,
) -> Result<ListModifiedResponse, Error> {
let builder = build_list_modified(site, since);
let builder = list_modified(site, since);
let res = self.invoke_with_builder(builder).await?;
let body: ListModifiedResponse = res.body()?;
Ok(body)
}

///#pex
pub async fn pex(&mut self, site: String) -> Result<PexResponse, Error> {
let builder = build_pex(site, 10);
async fn pex(&mut self, site: String) -> Result<PexResponse, Error> {
let builder = pex(site, 10);
let res = self.invoke_with_builder(builder).await?;
let body: PexResponse = res.body()?;
Ok(body)
Expand All @@ -99,7 +94,7 @@ impl<'a> Protocol<'a> {
mod tests {
use zeronet_protocol::PeerAddr;

use crate::core::peer::Peer;
use crate::{core::peer::Peer, protocol::api::Request};

use super::Protocol;

Expand Down
59 changes: 59 additions & 0 deletions src/net/response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::collections::HashMap;

use serde_bytes::ByteBuf;
use serde_json::json;

use crate::{
core::error::Error,
protocol::{
api::Response as ZeroNetResponse,
builders::{response::*, *},
Protocol,
},
};

#[async_trait::async_trait]
impl<'a> ZeroNetResponse for Protocol<'a> {
async fn handshake(&mut self, id: usize) -> Result<bool, Error> {
let builder = handshake();
self.0.respond(id, json!(builder.1)).await?;
Ok(true)
}

async fn ping(&mut self, id: usize) -> Result<bool, Error> {
self.0.respond(id, json!({"body":"Pong!"})).await?;
Ok(true)
}

async fn get_file(&mut self, id: usize, body: ByteBuf) -> Result<bool, Error> {
let builder = get_file(body, 0, 0);
self.0.respond(id, json!(builder.1)).await?;
Ok(true)
}

async fn stream_file(&mut self, id: usize, stream_bytes: usize) -> Result<bool, Error> {
let builder = stream_file(stream_bytes);
self.0.respond(id, json!(builder.1)).await?;
Ok(true)
}
async fn list_modified(
&mut self,
id: usize,
modified_files: HashMap<String, usize>,
) -> Result<bool, Error> {
let builder = list_modified(modified_files);
self.0.respond(id, json!(builder.1)).await?;
Ok(true)
}

async fn pex(
&mut self,
id: usize,
peers: Vec<ByteBuf>,
peers_onion: Vec<ByteBuf>,
) -> Result<bool, Error> {
let builder = pex(peers, peers_onion);
self.0.respond(id, json!(builder.1)).await?;
Ok(true)
}
}
Loading

0 comments on commit 2fbde65

Please sign in to comment.