Commit f8cf0109 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

refactor and plan for others message

parent 2a1ef305
use std::io::Read; use std::io::Read;
use std::net::TcpListener; use std::net::TcpListener;
use tenjin::openflow::events::packet_in::PacketInEvent; use tenjin::openflow::events::packet_in::PacketInEvent;
use tenjin::openflow::message::Openflow10; use tenjin::openflow::messages::{OfpMsg, Openflow10};
use tenjin::openflow::{Controller, OfpHeader, OfpMsg}; use tenjin::openflow::traiter::OfpMsgEvent;
use tenjin::openflow::{Controller, OfpHeader};
extern crate byteorder; extern crate byteorder;
...@@ -32,7 +33,7 @@ fn main() -> Result<(), std::io::Error> { ...@@ -32,7 +33,7 @@ fn main() -> Result<(), std::io::Error> {
let length_payload = packet.size(); let length_payload = packet.size();
let mut payload = vec![0u8; length_payload]; let mut payload = vec![0u8; length_payload];
stream.read(&mut payload)?; stream.read(&mut payload)?;
let message = OfpMsg::parse(packet.message); let message = controller.ofp.msg_parse(packet.message as u16);
match message { match message {
// 0 is Hello message // 0 is Hello message
......
use std::{collections::HashMap, io::Write, net::TcpStream}; use std::{collections::HashMap, io::Write, net::TcpStream};
use super::{ use super::{
events::PacketInEvent, message::OfpMsgEvent, trait_marshal::MessageMarshal, OfpHeader, events::PacketInEvent,
messages::traiter::{MessageMarshal, OfpMsgEvent},
}; };
pub struct Controller<OME: OfpMsgEvent> { pub struct Controller<OME: OfpMsgEvent> {
ofp: OME, /*
* pub is temporary, remove soon;
* for test in main func
*/
pub ofp: OME,
mac_to_port: HashMap<u64, u16>, mac_to_port: HashMap<u64, u16>,
} }
...@@ -22,12 +27,9 @@ impl<OME: OfpMsgEvent> Controller<OME> { ...@@ -22,12 +27,9 @@ impl<OME: OfpMsgEvent> Controller<OME> {
let mut header_bytes: Vec<u8> = Vec::new(); let mut header_bytes: Vec<u8> = Vec::new();
let mut body_bytes: Vec<u8> = Vec::new(); let mut body_bytes: Vec<u8> = Vec::new();
msg.marshal(&mut body_bytes); msg.marshal(&mut body_bytes);
let ofpheader = OfpHeader::new( let ofpheader =
self.ofp.version() as u8, self.ofp
msg.msg_code() as u8, .header(msg.msg_usize(&self.ofp) as u8, body_bytes.len() as u16, xid);
body_bytes.len() as u16,
xid,
);
ofpheader.marshal(&mut header_bytes); ofpheader.marshal(&mut header_bytes);
header_bytes.append(&mut body_bytes); header_bytes.append(&mut body_bytes);
let _ = stream.write_all(&header_bytes); let _ = stream.write_all(&header_bytes);
......
use crate::openflow::trait_marshal::MessageMarshal; use crate::openflow::messages::{MessageMarshal, OfpMsg, OfpMsgEvent};
pub struct FeaturesReq {} pub struct FeaturesReq {}
...@@ -11,11 +11,15 @@ impl FeaturesReq { ...@@ -11,11 +11,15 @@ impl FeaturesReq {
impl MessageMarshal for FeaturesReq { impl MessageMarshal for FeaturesReq {
fn marshal(&self, _: &mut Vec<u8>) {} fn marshal(&self, _: &mut Vec<u8>) {}
fn msg_code(&self) -> crate::openflow::OfpMsg { fn msg_code(&self) -> OfpMsg {
crate::openflow::OfpMsg::FeaturesReq OfpMsg::FeaturesReq
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
0 0
} }
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FeaturesReq)
}
} }
...@@ -2,7 +2,10 @@ use std::io::Cursor; ...@@ -2,7 +2,10 @@ use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crate::openflow::{trait_marshal::MessageMarshal, OfpMsg, OfpPort, PseudoPort}; use crate::openflow::{
messages::{MessageMarshal, OfpMsg, OfpMsgEvent},
OfpPort, PseudoPort,
};
use super::{FlowAction, FlowModCommand, MatchFields}; use super::{FlowAction, FlowModCommand, MatchFields};
...@@ -89,6 +92,9 @@ impl FlowModEvent { ...@@ -89,6 +92,9 @@ impl FlowModEvent {
} }
impl MessageMarshal for FlowModEvent { impl MessageMarshal for FlowModEvent {
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::FlowMod)
}
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
24 24
} }
......
use crate::openflow::{trait_marshal::MessageMarshal, OfpMsg}; use crate::openflow::messages::{MessageMarshal, OfpMsg};
use crate::openflow::traiter::OfpMsgEvent;
pub struct HelloEvent {} pub struct HelloEvent {}
...@@ -11,11 +12,15 @@ impl HelloEvent { ...@@ -11,11 +12,15 @@ impl HelloEvent {
impl MessageMarshal for HelloEvent { impl MessageMarshal for HelloEvent {
fn marshal(&self, _: &mut Vec<u8>) {} fn marshal(&self, _: &mut Vec<u8>) {}
fn msg_code(&self) -> crate::openflow::OfpMsg { fn msg_code(&self) -> OfpMsg {
OfpMsg::Hello OfpMsg::Hello
} }
fn size_of(&self) -> usize { fn size_of(&self) -> usize {
0 0
} }
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp.msg_usize(OfpMsg::Hello)
}
} }
pub mod ofp_message;
pub use ofp_message::OfpMsg;
pub mod traiter;
pub use traiter::MessageMarshal;
pub use traiter::OfpMsgEvent;
pub mod ofp_v1_0;
pub use ofp_v1_0::Openflow10;
pub enum OfpMsg {
Hello = 0,
FeaturesReq = 5,
PacketIn = 8,
FlowMod = 14,
NotFound = -1,
}
use super::events::{FeaturesReq, HelloEvent}; use crate::openflow::{
events::{FeaturesReq, HelloEvent},
OfpHeader,
};
pub trait OfpMsgEvent { use super::traiter::OfpMsgEvent;
fn hello_event(&self) -> HelloEvent; use super::OfpMsg;
fn fetures_req(&self) -> FeaturesReq;
fn version(&self) -> usize;
}
pub enum OfpMsg {
Hello = 0,
FeaturesReq = 5,
PacketIn = 8,
FlowMod = 14,
NotFound = -1,
}
pub struct Openflow10 {} pub struct Openflow10 {}
...@@ -34,14 +26,33 @@ impl OfpMsgEvent for Openflow10 { ...@@ -34,14 +26,33 @@ impl OfpMsgEvent for Openflow10 {
fn version(&self) -> usize { fn version(&self) -> usize {
1 1
} }
}
impl OfpMsg { fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader {
pub fn parse(message_code: u8) -> Self { OfpHeader {
match message_code { version: 1,
message,
length,
xid,
}
}
fn msg_parse(&self, msg: u16) -> OfpMsg {
match msg {
0 => OfpMsg::Hello, 0 => OfpMsg::Hello,
5 => OfpMsg::FeaturesReq,
8 => OfpMsg::PacketIn, 8 => OfpMsg::PacketIn,
14 => OfpMsg::FlowMod,
_ => OfpMsg::NotFound, _ => OfpMsg::NotFound,
} }
} }
fn msg_usize(&self, msg: OfpMsg) -> usize {
match msg {
OfpMsg::Hello => 0,
OfpMsg::FeaturesReq => 5,
OfpMsg::PacketIn => 8,
OfpMsg::FlowMod => 14,
_ => 1024,
}
}
} }
use crate::openflow::{
events::{FeaturesReq, HelloEvent},
OfpHeader,
};
use super::OfpMsg; use super::OfpMsg;
/** /**
...@@ -7,5 +12,18 @@ use super::OfpMsg; ...@@ -7,5 +12,18 @@ use super::OfpMsg;
pub trait MessageMarshal { pub trait MessageMarshal {
fn marshal(&self, bytes: &mut Vec<u8>); fn marshal(&self, bytes: &mut Vec<u8>);
fn msg_code(&self) -> OfpMsg; fn msg_code(&self) -> OfpMsg;
fn msg_usize<OFP: OfpMsgEvent>(&self, ofp: &OFP) -> usize;
fn size_of(&self) -> usize; fn size_of(&self) -> usize;
} }
/**
* for works with controller to create OfpMsgEvent
*/
pub trait OfpMsgEvent {
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader;
fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReq;
fn version(&self) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg;
fn msg_usize(&self, msg: OfpMsg) -> usize;
}
pub mod header; pub mod header;
pub use header::OfpHeader; pub use header::OfpHeader;
pub mod message;
pub use message::OfpMsg;
pub mod controller; pub mod controller;
pub use controller::Controller; pub use controller::Controller;
...@@ -12,4 +9,5 @@ pub mod events; ...@@ -12,4 +9,5 @@ pub mod events;
pub mod ofp_port; pub mod ofp_port;
pub use ofp_port::{OfpPort, PseudoPort}; pub use ofp_port::{OfpPort, PseudoPort};
pub mod trait_marshal; pub mod messages;
\ No newline at end of file pub use messages::{ofp_v1_0, traiter};
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