Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Oct 8, 2018
1 parent 328f692 commit 0ccae93
Showing 1 changed file with 38 additions and 42 deletions.
80 changes: 38 additions & 42 deletions src/prosafe_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,20 @@ impl QueryRequest {
}

// ---------------------------------------------------------------------------------------------------------------------
// PortStats
// QueryResponse
// ---------------------------------------------------------------------------------------------------------------------

#[derive(Debug)]
pub struct PortStats {
pub stats: Vec<PortStat>,
}
struct QueryResponse;

#[derive(Debug)]
pub struct PortStat {
pub port_no: u8,
pub recv_bytes: u64,
pub send_bytes: u64,
pub error_pkts: u64,
}

impl PortStats {
fn decode(dat: &[u8]) -> Result<Self, Error> {
impl QueryResponse {
fn decode(dat: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
let (_, rest) = bytes(&[0x01, 0x02])
.and(be_u16())
.and(be_u16())
.and(skip_count(26, any()))
.parse(dat)
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
let mut stats = Vec::new();
let mut ret = Vec::new();
let mut buf = rest;
while buf.len() != 0 {
let ((cmd, len), rest) = be_u16()
Expand All @@ -99,9 +88,38 @@ impl PortStats {
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
buf = rest;

ret.push(dat);
}

Ok(ret)
}
}

// ---------------------------------------------------------------------------------------------------------------------
// PortStats
// ---------------------------------------------------------------------------------------------------------------------

#[derive(Debug)]
pub struct PortStats {
pub stats: Vec<PortStat>,
}

#[derive(Debug)]
pub struct PortStat {
pub port_no: u8,
pub recv_bytes: u64,
pub send_bytes: u64,
pub error_pkts: u64,
}

impl PortStats {
fn decode(dat: &[u8]) -> Result<Self, Error> {
let dat = QueryResponse::decode(dat)?;
let mut stats = Vec::new();
for d in dat {
let ((port_no, metrics), _rest) = any()
.and(count::<Vec<_>, _>(6, be_u64()))
.parse(&dat as &[u8])
.parse(&d as &[u8])
.map_err(|x| format_err!("failed to parse: {:?}", x))?;

let stat = PortStat {
Expand Down Expand Up @@ -144,33 +162,12 @@ pub enum Link {

impl SpeedStats {
fn decode(dat: &[u8]) -> Result<Self, Error> {
let (_, rest) = bytes(&[0x01, 0x02])
.and(be_u16())
.and(be_u16())
.and(skip_count(26, any()))
.parse(dat)
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
let dat = QueryResponse::decode(dat)?;
let mut stats = Vec::new();
let mut buf = rest;
while buf.len() != 0 {
let ((cmd, len), rest) = be_u16()
.and(be_u16())
.parse(buf)
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
buf = rest;

if cmd == 0xffff {
break;
}

let (dat, rest) = count::<Vec<_>, _>(len as usize, any())
.parse(buf)
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
buf = rest;

for d in dat {
let ((port_no, metrics), _rest) = any()
.and(count::<Vec<_>, _>(2, any()))
.parse(&dat as &[u8])
.parse(&d as &[u8])
.map_err(|x| format_err!("failed to parse: {:?}", x))?;

let link = match metrics[0] {
Expand All @@ -188,7 +185,6 @@ impl SpeedStats {
port_no: port_no,
link: link,
};

stats.push(stat);
}

Expand Down

0 comments on commit 0ccae93

Please sign in to comment.