Commit 32a24045 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

define value to Msg and impl

parent 8a49beb1
use crate::openflow::ofp10::{ use crate::openflow::ofp10::{ErrorEvent, Msg, PacketInEvent};
ErrorEvent, Msg, PacketInEvent,
};
use std::{ use std::{
io::{Read, Write}, io::{Read, Write},
net::TcpStream, net::TcpStream,
}; };
use super::{tcp_listener::tcp_listener_handler, traiter::{MessageMarshal, OfpMsgEvent}}; use super::{
tcp_listener::tcp_listener_handler,
traiter::{MessageMarshal, OfpMsgEvent},
};
pub trait ControllerFrame<OME: OfpMsgEvent> { pub trait ControllerFrame<OME: OfpMsgEvent> {
fn get_ofp(&self) -> &impl OfpMsgEvent; fn get_ofp(&self) -> &impl OfpMsgEvent;
...@@ -30,7 +31,7 @@ pub trait ControllerFrame<OME: OfpMsgEvent> { ...@@ -30,7 +31,7 @@ pub trait ControllerFrame<OME: OfpMsgEvent> {
let (message, pkt_size, xid) = self.handle_header(buf); let (message, pkt_size, xid) = self.handle_header(buf);
let mut payload = vec![0u8; pkt_size]; let mut payload = vec![0u8; pkt_size];
let _ = stream.read(&mut payload); let _ = stream.read(&mut payload);
let message = self.get_ofp().msg_parse(message as u16); let message = self.get_ofp().msg_parse(message as u8);
match message { match message {
Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream), Msg::Hello => self.send_msg(self.get_ofp().fetures_req(), xid, stream),
Msg::Error => { Msg::Error => {
......
#[repr(u8)]
pub enum Msg { pub enum Msg {
Hello, Hello = 0,
Error, Error = 1,
FeaturesReq, FeaturesReq = 5,
PacketIn, PacketIn = 10,
PacketOut, PacketOut = 13,
FlowMod, FlowMod = 14,
NotFound, NotFound = 0xff,
} }
impl Msg {
pub fn to_int(&self) -> u8 {
self.clone() as u8
}
pub fn parse(msg_code: u8) -> Self {
type ConvTyp = u8;
match msg_code {
m if m == (Self::Hello as ConvTyp) => Self::Hello,
m if m == (Self::Error as ConvTyp) => Self::Error,
m if m == (Self::FeaturesReq as ConvTyp) => Self::FeaturesReq,
m if m == (Self::PacketIn as ConvTyp) => Self::PacketIn,
m if m == (Self::PacketOut as ConvTyp) => Self::PacketOut,
m if m == (Self::FlowMod as ConvTyp) => Self::FlowMod,
_ => Self::NotFound,
}
}
}
impl Clone for Msg {
fn clone(&self) -> Self {
match self {
Self::Hello => Self::Hello,
Self::Error => Self::Error,
Self::FeaturesReq => Self::FeaturesReq,
Self::PacketIn => Self::PacketIn,
Self::PacketOut => Self::PacketOut,
Self::FlowMod => Self::FlowMod,
Self::NotFound => Self::NotFound,
}
}
}
...@@ -46,27 +46,11 @@ impl OfpMsgEvent for Openflow10 { ...@@ -46,27 +46,11 @@ impl OfpMsgEvent for Openflow10 {
OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize)) OfpHeader::new(OfpHeader10::new(message, length as usize, xid as usize))
} }
fn msg_parse(&self, msg: u16) -> Msg { fn msg_parse(&self, msg: u8) -> Msg {
match msg { Msg::parse(msg)
0 => Msg::Hello,
1 => Msg::Error,
5 => Msg::FeaturesReq,
10 => Msg::PacketIn,
13 => Msg::PacketOut,
14 => Msg::FlowMod,
_ => Msg::NotFound,
}
} }
fn msg_usize(&self, msg: Msg) -> usize { fn msg_usize(&self, msg: Msg) -> usize {
match msg { msg.to_int() as usize
Msg::Hello => 0,
Msg::Error => 1,
Msg::FeaturesReq => 5,
Msg::PacketIn => 10,
Msg::PacketOut => 13,
Msg::FlowMod => 14,
_ => 1024,
}
} }
} }
...@@ -28,7 +28,7 @@ pub trait OfpMsgEvent { ...@@ -28,7 +28,7 @@ pub trait OfpMsgEvent {
fn header_size(&self) -> usize; fn header_size(&self) -> usize;
fn msg_usize(&self, msg: Msg) -> usize; fn msg_usize(&self, msg: Msg) -> usize;
fn msg_parse(&self, msg: u16) -> Msg; fn msg_parse(&self, msg: u8) -> Msg;
fn hello_event(&self) -> HelloEvent; fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReqEvent; fn fetures_req(&self) -> FeaturesReqEvent;
fn packet_out( fn packet_out(
......
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