net command

This commit is contained in:
debris 2016-09-19 18:57:41 +02:00
parent 7b1983013d
commit 0f7f75dee3
7 changed files with 81 additions and 1 deletions

69
net/src/command.rs Normal file
View File

@ -0,0 +1,69 @@
use std::str;
use std::ascii::AsciiExt;
use hash::H96;
use ser::{Serializable, Stream, Deserializable, Reader, Error as ReaderError};
use Error;
#[derive(Debug, PartialEq)]
pub struct Command(H96);
impl str::FromStr for Command {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.is_ascii() || s.len() > 12 {
return Err(Error::InvalidCommand);
}
let mut result = H96::default();
result[..s.len()].copy_from_slice(s.as_ref());
Ok(Command(result))
}
}
impl From<&'static str> for Command {
fn from(s: &'static str) -> Self {
s.parse().unwrap()
}
}
impl Serializable for Command {
fn serialize(&self, stream: &mut Stream) {
stream.append(&self.0);
}
}
impl Deserializable for Command {
fn deserialize(reader: &mut Reader) -> Result<Self, ReaderError> where Self: Sized {
reader.read().map(Command)
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use ser::{serialize, deserialize};
use super::Command;
#[test]
fn test_parse() {
assert_eq!(Command("76657273696f6e0000000000".into()), "version".into());
}
#[test]
fn test_command_serialize() {
let expected = "76657273696f6e0000000000".into();
let command: Command = "version".into();
assert_eq!(serialize(&command), expected);
}
#[test]
fn test_command_deserialize() {
let raw: Bytes = "76657273696f6e0000000000".into();
let expected: Command = "version".into();
assert_eq!(expected, deserialize(&raw).unwrap());
}
}

4
net/src/error.rs Normal file
View File

@ -0,0 +1,4 @@
#[derive(Debug)]
pub enum Error {
InvalidCommand,
}

View File

@ -3,6 +3,8 @@ extern crate primitives;
extern crate serialization as ser;
mod address;
mod command;
mod error;
mod inventory;
mod ip;
mod port;
@ -11,6 +13,8 @@ mod service;
pub use primitives::{hash, bytes};
pub use self::address::NetAddress;
pub use self::command::Command;
pub use self::error::Error;
pub use self::inventory::{InventoryVector, InventoryType};
pub use self::ip::IpAddress;
pub use self::port::Port;

View File

@ -106,6 +106,7 @@ macro_rules! impl_hash {
}
}
impl_hash!(H96, 12);
impl_hash!(H160, 20);
impl_hash!(H256, 32);
impl_hash!(H264, 33);

View File

@ -1,5 +1,5 @@
use bytes::Bytes;
use hash::{H160, H256, H264, H512, H520};
use hash::{H96, H160, H256, H264, H512, H520};
use compact_integer::CompactInteger;
use {Serializable, Stream, Deserializable, Reader, Error};
@ -22,6 +22,7 @@ macro_rules! impl_ser_for_hash {
}
}
impl_ser_for_hash!(H96, 12);
impl_ser_for_hash!(H160, 20);
impl_ser_for_hash!(H256, 32);
impl_ser_for_hash!(H264, 33);

View File

@ -43,6 +43,7 @@ digraph dependencies {
N3 -> N15[label="",style=dashed];
N3 -> N18[label="",style=dashed];
N3 -> N22[label="",style=dashed];
N4 -> N5[label="",style=dashed];
N4 -> N7[label="",style=dashed];
N4 -> N13[label="",style=dashed];
N5 -> N14[label="",style=dashed];

Binary file not shown.

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 99 KiB