Commit e17a2926 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

change ofp_header to one file

parent 960dd6a2
...@@ -4,7 +4,7 @@ use std::{ ...@@ -4,7 +4,7 @@ use std::{
net::TcpStream, net::TcpStream,
}; };
use super::{tcp_listener_handler, MessageMarshal, OfpMsgEvent}; use super::{tcp_listener_handler, MessageMarshal, OfpMsgEvent, OpenflowHeader};
pub trait ControllerFrame10 { pub trait ControllerFrame10 {
fn get_ofp(&self) -> &impl OfpMsgEvent; fn get_ofp(&self) -> &impl OfpMsgEvent;
......
...@@ -3,19 +3,19 @@ use std::{io::Cursor, mem::size_of}; ...@@ -3,19 +3,19 @@ use std::{io::Cursor, mem::size_of};
use crate::openflow::ofp10::OpenflowHeader; use crate::openflow::ofp10::OpenflowHeader;
pub struct OfpHeader10 { pub struct OfpHeader {
pub version: u8, pub version: u8,
pub message: u8, pub message: u8,
pub length: u16, pub length: u16,
pub xid: u32, pub xid: u32,
} }
impl OpenflowHeader for OfpHeader10 { impl OpenflowHeader for OfpHeader {
fn new(message: u8, length: usize, xid: usize) -> Self { fn new(message: u8, length: usize, xid: usize) -> Self {
Self { Self {
version: 1, version: 1,
message, message,
length: (size_of::<OfpHeader10>() + length) as u16, length: (size_of::<OfpHeader>() + length) as u16,
xid: xid as u32, xid: xid as u32,
} }
} }
...@@ -32,10 +32,10 @@ impl OpenflowHeader for OfpHeader10 { ...@@ -32,10 +32,10 @@ impl OpenflowHeader for OfpHeader10 {
self.xid self.xid
} }
fn header_size(&self) -> usize { fn header_size(&self) -> usize {
size_of::<OfpHeader10>() size_of::<Self>()
} }
fn pkt_size(&self) -> usize { fn pkt_size(&self) -> usize {
return self.length as usize - size_of::<OfpHeader10>(); return self.length as usize - size_of::<Self>();
} }
fn parse(buf: &Vec<u8>) -> Self { fn parse(buf: &Vec<u8>) -> Self {
......
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod ofp_header_v1_0;
pub use ofp_header_v1_0::OfpHeader10;
use crate::openflow::ofp10::OpenflowHeader;
pub struct OfpHeader<OFH: OpenflowHeader> {
ofp_header: OFH,
}
impl<OFH: OpenflowHeader> OfpHeader<OFH> {
pub fn new(ofp_header: OFH) -> Self {
Self { ofp_header }
}
pub fn version(&self) -> usize {
self.ofp_header.version()
}
pub fn message(&self) -> u8 {
self.ofp_header.message()
}
pub fn length(&self) -> usize {
self.ofp_header.length()
}
pub fn xid(&self) -> u32 {
self.ofp_header.xid()
}
pub fn header_size(&self) -> usize {
self.ofp_header.header_size()
}
pub fn pkt_size(&self) -> usize {
self.ofp_header.pkt_size()
}
pub fn marshal(&self, bytes: &mut Vec<u8>) {
self.ofp_header.marshal(bytes);
}
}
use super::{ use super::{
events::{Action, FeaturesReqEvent, Payload}, events::{Action, FeaturesReqEvent, Payload},
ofp_header::{OfpHeader, OfpHeader10}, ofp_header::OfpHeader,
HelloEvent, Msg, OfpMsgEvent, OpenflowHeader, PacketOutEvent, HelloEvent, Msg, OfpMsgEvent, OpenflowHeader, PacketOutEvent,
}; };
...@@ -13,8 +13,8 @@ impl Openflow10 { ...@@ -13,8 +13,8 @@ impl Openflow10 {
} }
impl OfpMsgEvent for Openflow10 { impl OfpMsgEvent for Openflow10 {
fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader> { fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader {
OfpHeader::new(OfpHeader10::parse(bytes)) OfpHeader::parse(bytes)
} }
fn header_size(&self) -> usize { fn header_size(&self) -> usize {
8 8
...@@ -41,8 +41,8 @@ impl OfpMsgEvent for Openflow10 { ...@@ -41,8 +41,8 @@ impl OfpMsgEvent for Openflow10 {
1 1
} }
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader> { fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader {
OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize)) OfpHeader::new(message, length as usize, xid as usize)
} }
fn msg_parse(&self, msg: u8) -> Msg { fn msg_parse(&self, msg: u8) -> Msg {
......
...@@ -4,8 +4,6 @@ use crate::openflow::ofp10::{ ...@@ -4,8 +4,6 @@ use crate::openflow::ofp10::{
Msg, Msg,
}; };
use super::header_trait::OpenflowHeader;
/** /**
* the trait for parse value to bytes. * the trait for parse value to bytes.
* for use with Controller's send_msg. * for use with Controller's send_msg.
...@@ -21,8 +19,8 @@ pub trait MessageMarshal { ...@@ -21,8 +19,8 @@ pub trait MessageMarshal {
* for works with controller to create OfpMsgEvent * for works with controller to create OfpMsgEvent
*/ */
pub trait OfpMsgEvent { pub trait OfpMsgEvent {
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader<impl OpenflowHeader>; fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader;
fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader<impl OpenflowHeader>; fn header_parse(&self, bytes: &Vec<u8>) -> OfpHeader;
fn version(&self) -> usize; fn version(&self) -> usize;
fn ofp_version() -> usize; fn ofp_version() -> usize;
fn header_size(&self) -> usize; fn header_size(&self) -> usize;
......
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tenjin::{ use tenjin::{
openflow::ofp10::{ControllerFrame10, Msg, OfpMsgEvent}, openflow::ofp10::{ControllerFrame10, Msg, OfpMsgEvent, OpenflowHeader},
Controller, Controller,
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment