Commit 1320b926 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

use .ofp() instead of .get_ofp()

when write the controller frame only for openflow 1.0, it don't need to return others version.
parent ba707656
......@@ -17,17 +17,12 @@ use crate::{
*/
pub struct Controller {
ofp: Openflow10,
mac_to_port: HashMap<u64, u16>,
}
impl ControllerFrame10 for Controller {
fn get_ofp(&self) -> &impl OfpMsgEvent {
&self.ofp
}
fn new() -> Self {
Self {
ofp: Openflow10::new(),
mac_to_port: HashMap::new(),
}
}
......@@ -68,7 +63,7 @@ impl ControllerFrame10 for Controller {
}
}
let packet_out = self
.ofp
.ofp()
.packet_out(Some(packetin.in_port), packetin.payload, actions);
self.send_msg(packet_out, xid, stream);
}
......
use crate::openflow::ofp10::{ErrorEvent, Msg, PacketInEvent};
use crate::openflow::ofp10::{self, ErrorEvent, Msg, PacketInEvent};
use std::{
io::{Read, Write},
net::TcpStream,
};
use super::{tcp_listener_handler, MessageMarshal, OfpMsgEvent, OpenflowHeader};
use super::{tcp_listener_handler, MessageMarshal, OfpMsgEvent, Openflow10, OpenflowHeader};
pub trait ControllerFrame10 {
fn get_ofp(&self) -> &impl OfpMsgEvent;
fn ofp(&self) -> ofp10::Openflow10 {
Openflow10::new()
}
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream);
fn new() -> Self;
......@@ -16,7 +18,7 @@ pub trait ControllerFrame10 {
}
fn handle_header(&mut self, buf: &mut Vec<u8>) -> (u8, usize, u32) {
let ofp_header = self.get_ofp().header_parse(&buf);
let ofp_header = self.ofp().header_parse(&buf);
(
ofp_header.message(),
ofp_header.pkt_size(),
......@@ -25,12 +27,13 @@ pub trait ControllerFrame10 {
}
fn request_handler(&mut self, buf: &mut Vec<u8>, stream: &mut TcpStream) {
let ofp = self.ofp();
let (message, pkt_size, xid) = self.handle_header(buf);
let mut payload = vec![0u8; pkt_size];
let _ = stream.read(&mut payload);
let message = self.get_ofp().msg_parse(message as u8);
let message = ofp.msg_parse(message as u8);
match message {
Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
Msg::Hello => self.send_msg(ofp.fetures_req(), xid, stream),
Msg::Error => {
let error = ErrorEvent::parse(&payload);
println!("Error {:?}", error.error_type);
......@@ -47,12 +50,12 @@ pub trait ControllerFrame10 {
}
fn send_msg<MSM: MessageMarshal>(&self, msg: MSM, xid: u32, stream: &mut TcpStream) {
let ofp = self.get_ofp();
let ofp = self.ofp();
let mut header_bytes: Vec<u8> = Vec::new();
let mut body_bytes: Vec<u8> = Vec::new();
msg.marshal(&mut body_bytes);
let ofp_header = ofp.header(msg.msg_usize(ofp) as u8, body_bytes.len() as u16, xid);
let ofp_header = ofp.header(msg.msg_usize(&ofp) as u8, body_bytes.len() as u16, xid);
ofp_header.marshal(&mut header_bytes);
header_bytes.append(&mut body_bytes);
let _ = stream.write_all(&header_bytes);
......
......@@ -8,7 +8,10 @@ pub mod events;
pub use events::{ErrorEvent, FlowModEvent, HelloEvent, PacketInEvent, PacketOutEvent};
pub mod ofp_header;
pub use ofp_header::OfpHeader;
pub mod ofp_manager;
pub use ofp_manager::Openflow10;
pub mod controller_frame;
pub use controller_frame::ControllerFrame10;
......
......@@ -17,7 +17,7 @@ pub fn tcp_listener_handler(address: &str) {
thread::spawn(move || {
let mut ctrl = Controller::new();
ctrl.send_msg(HelloEvent::new(), 0, &mut stream);
let ofp_size = ctrl.get_ofp().header_size();
let ofp_size = ctrl.ofp().header_size();
// let ofp = controller.lock().unwrap().get_ofp();
let mut buffer = vec![0u8; ofp_size];
loop {
......
......@@ -10,7 +10,7 @@ mod tests {
let ofp_header_bytes: Vec<u8> = vec![1, 0, 0, 8, 0, 0, 0, 1];
let controller = Controller::new();
let ofp = controller.get_ofp();
let ofp = controller.ofp();
let header = ofp.header_parse(&ofp_header_bytes);
assert_eq!(header.version(), 1);
......@@ -23,7 +23,7 @@ mod tests {
let ofp_header_bytes: Vec<u8> = vec![1, 0, 0, 8, 0, 0, 0, 0];
let controller = Controller::new();
let ofp = controller.get_ofp();
let ofp = controller.ofp();
let ofp_header = ofp.header(ofp.msg_usize(Msg::Hello) as u8, 0, 0);
let mut bytes: Vec<u8> = Vec::new();
ofp_header.marshal(&mut bytes);
......
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