This repository has been archived by the owner on Jun 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start adding the networking (abstracted from the transport)
- Loading branch information
1 parent
b54fdf3
commit 9a9ce6e
Showing
31 changed files
with
2,993 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,4 @@ pub mod utils; | |
pub mod models; | ||
pub mod store; | ||
pub mod node; | ||
//pub mod network; | ||
pub mod network; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2018 Yobicash Ltd. | ||
// | ||
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> | ||
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>. | ||
// This file may not be copied, modified, or distributed except according to those | ||
// terms. | ||
|
||
//! The `handlers` module provides the Yobicash network handlers types and methods. | ||
|
||
// pub mod ping; | ||
// pub mod list; | ||
// pub mod sample; | ||
// pub mod get; | ||
// pub mod lookup; | ||
// pub mod put; | ||
|
||
// pub use self::ping::*; | ||
// pub use self::list::*; | ||
// pub use self::sample::*; | ||
// pub use self::get::*; | ||
// pub use self::lookup::*; | ||
// pub use self::put::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018 Yobicash Ltd. | ||
// | ||
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> | ||
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>. | ||
// This file may not be copied, modified, or distributed except according to those | ||
// terms. | ||
|
||
//! The `get` module provides the Yobicash network get message types and methods. | ||
|
||
pub mod request; | ||
pub mod response; | ||
|
||
use self::request::*; | ||
use self::response::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// Copyright 2018 Yobicash Ltd. | ||
// | ||
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> | ||
// and the Apache 2.0 license <LICENSE-APACHE or https://opensource.org/licenses/Apache-2.0>. | ||
// This file may not be copied, modified, or distributed except according to those | ||
// terms. | ||
|
||
//! The `request` module provides the Yobicash network get request message type and methods. | ||
|
||
use serde_json as json; | ||
use rmp_serde as messagepack; | ||
use hex; | ||
|
||
use error::ErrorKind; | ||
use result::Result; | ||
use traits::{Validate, HexSerialize, Serialize}; | ||
use utils::{Version, NetworkType}; | ||
use crypto::Digest; | ||
use crypto::HexSerialize as CryptoHexSerialize; | ||
use network::session::Session; | ||
use network::resource_type::ResourceType; | ||
|
||
/// The request for getting a resource. | ||
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)] | ||
pub struct GetRequest { | ||
/// Id of the session. | ||
pub id: u32, | ||
/// The version of the protocol. | ||
pub version: Version, | ||
/// The network type of the protocol. | ||
pub network_type: NetworkType, | ||
/// The maximum size of the `GetRequest` message. | ||
pub max_size: u32, | ||
/// The resource type. | ||
pub resource_type: ResourceType, | ||
/// The resoure id. | ||
pub resource_id: Digest, | ||
} | ||
|
||
impl GetRequest { | ||
/// Creates a new `GetRequest`. | ||
pub fn new(session: &Session, | ||
resource_type: ResourceType, | ||
resource_id: Digest) -> Result<GetRequest> { | ||
session.validate()?; | ||
|
||
if session.max_size.is_none() { | ||
return Err(ErrorKind::InvalidSession.into()); | ||
} | ||
|
||
let max_size = session.max_size.unwrap(); | ||
|
||
let id = session.id; | ||
let version = session.version.clone(); | ||
let network_type = session.network_type; | ||
|
||
let get_request = GetRequest { | ||
id: id, | ||
version: version, | ||
network_type: network_type, | ||
max_size: max_size, | ||
resource_type: resource_type, | ||
resource_id: resource_id, | ||
}; | ||
|
||
if get_request.to_bytes()?.len() as u32 > max_size { | ||
return Err(ErrorKind::InvalidLength.into()); | ||
} | ||
|
||
Ok(get_request) | ||
} | ||
} | ||
|
||
impl Validate for GetRequest { | ||
fn validate(&self) -> Result<()> { | ||
self.version.validate()?; | ||
|
||
if self.to_bytes()?.len() as u32 > self.max_size { | ||
return Err(ErrorKind::InvalidLength.into()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a> Serialize<'a> for GetRequest { | ||
fn to_json(&self) -> Result<String> { | ||
|
||
let obj = json!({ | ||
"id": self.id, | ||
"version": self.version.to_string(), | ||
"network_type": self.network_type.to_hex()?, | ||
"max_size": self.max_size, | ||
"resource_type": self.resource_type.to_hex()?, | ||
"resource_id": self.resource_id.to_hex()?, | ||
}); | ||
|
||
let s = obj.to_string(); | ||
|
||
Ok(s) | ||
} | ||
|
||
fn from_json(s: &str) -> Result<Self> { | ||
let obj: json::Value = json::from_str(s)?; | ||
|
||
let id_value = obj["id"].clone(); | ||
let id: u32 = json::from_value(id_value)?; | ||
|
||
let version_value = obj["version"].clone(); | ||
let version_str: String = json::from_value(version_value)?; | ||
let version = Version::from_string(&version_str)?; | ||
|
||
let network_type_value = obj["network_type"].clone(); | ||
let network_type_hex: String = json::from_value(network_type_value)?; | ||
let network_type = NetworkType::from_hex(&network_type_hex)?; | ||
|
||
let max_size_value = obj["max_size"].clone(); | ||
let max_size: u32 = json::from_value(max_size_value)?; | ||
|
||
let resource_type_value = obj["resource_type"].clone(); | ||
let resource_type_hex: String = json::from_value(resource_type_value)?; | ||
let resource_type = ResourceType::from_hex(&resource_type_hex)?; | ||
|
||
let resource_id_value = obj["resource_id"].clone(); | ||
let resource_id_hex: String = json::from_value(resource_id_value)?; | ||
let resource_id = Digest::from_hex(&resource_id_hex)?; | ||
|
||
let get_request = GetRequest { | ||
id: id, | ||
version: version, | ||
network_type: network_type, | ||
max_size: max_size, | ||
resource_type: resource_type, | ||
resource_id: resource_id, | ||
}; | ||
|
||
Ok(get_request) | ||
} | ||
|
||
fn to_bytes(&self) -> Result<Vec<u8>> { | ||
let buf = messagepack::to_vec(self)?; | ||
|
||
Ok(buf) | ||
} | ||
|
||
fn from_bytes(b: &[u8]) -> Result<Self> { | ||
let get_request = messagepack::from_slice(b)?; | ||
|
||
Ok(get_request) | ||
} | ||
|
||
fn to_hex(&self) -> Result<String> { | ||
Ok(hex::encode(self.to_bytes()?)) | ||
} | ||
|
||
fn from_hex(s: &str) -> Result<Self> { | ||
Self::from_bytes(&hex::decode(s)?) | ||
} | ||
} |
Oops, something went wrong.