Commit 0b80948b authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

refactor: create message in controller, make it simple to use

parent 282cba84
use std::{collections::HashMap, io::Write, net::TcpStream}; use std::{collections::HashMap, io::Write, net::TcpStream};
use super::{ use super::{
events::{FeaturesReq, HelloEvent, PacketInEvent}, events::PacketInEvent, message::OfpMsgEvent, trait_marshal::MessageMarshal, OfpHeader,
trait_marshal::MessageMarshal,
OfpHeader,
}; };
pub struct Controller { pub struct Controller<OME: OfpMsgEvent> {
version: u8, ofp: OME,
mac_to_port: HashMap<u64, u16>, mac_to_port: HashMap<u64, u16>,
} }
impl Controller { impl<OME: OfpMsgEvent> Controller<OME> {
pub const OFP_1_0: u8 = 1; pub const OFP_1_0: u8 = 1;
pub fn new(version: u8) -> Self { pub fn new(ofp: OME) -> Self {
Self { Self {
version, ofp,
mac_to_port: HashMap::new(), mac_to_port: HashMap::new(),
} }
} }
...@@ -25,7 +23,7 @@ impl Controller { ...@@ -25,7 +23,7 @@ impl Controller {
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 = OfpHeader::new(
self.version, self.ofp.version() as u8,
msg.msg_code() as u8, msg.msg_code() as u8,
body_bytes.len() as u16, body_bytes.len() as u16,
xid, xid,
...@@ -39,12 +37,12 @@ impl Controller { ...@@ -39,12 +37,12 @@ impl Controller {
* example of sending message * example of sending message
*/ */
pub fn hello(&self, stream: &mut TcpStream) { pub fn hello(&self, stream: &mut TcpStream) {
let hello_msg = HelloEvent::new(); let hello_msg = self.ofp.hello_event();
self.send_msg(hello_msg, 0, stream); self.send_msg(hello_msg, 0, stream);
} }
pub fn fetures_req(&self, xid: u32, stream: &mut TcpStream) { pub fn fetures_req(&self, xid: u32, stream: &mut TcpStream) {
let fetreq_msg = FeaturesReq::new(); let fetreq_msg = self.ofp.fetures_req();
self.send_msg(fetreq_msg, xid, stream); self.send_msg(fetreq_msg, xid, stream);
} }
......
use super::events::{FeaturesReq, HelloEvent};
pub trait OfpMsgEvent {
fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReq;
fn version(&self) -> usize;
}
pub enum OfpMsg { pub enum OfpMsg {
Hello = 0, Hello = 0,
FeaturesReq = 5, FeaturesReq = 5,
...@@ -6,6 +14,20 @@ pub enum OfpMsg { ...@@ -6,6 +14,20 @@ pub enum OfpMsg {
NotFound = -1, NotFound = -1,
} }
impl OfpMsgEvent for OfpMsg {
fn hello_event(&self) -> HelloEvent {
HelloEvent::new()
}
fn fetures_req(&self) -> FeaturesReq {
FeaturesReq::new()
}
fn version(&self) -> usize {
1
}
}
impl OfpMsg { impl OfpMsg {
pub fn parse(message_code: u8) -> Self { pub fn parse(message_code: u8) -> Self {
match message_code { match message_code {
......
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