Commit f2d84128 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

inprove controller frame: message handle; add echo events

parent ecaef6a6
......@@ -4,7 +4,10 @@ use std::{
net::TcpStream,
};
use super::{tcp_listener_handler, MessageMarshal, OfpMsgEvent, Openflow10, OpenflowHeader};
use super::{
events::{echo_reply::EchoReplyEvent, EchoRequestEvent},
tcp_listener_handler, MessageMarshal, OfpMsgEvent, Openflow10, OpenflowHeader,
};
pub trait ControllerFrame10 {
fn ofp(&self) -> ofp10::Openflow10 {
......@@ -33,18 +36,15 @@ pub trait ControllerFrame10 {
let _ = stream.read(&mut payload);
let message = ofp.msg_parse(message as u8);
match message {
Msg::Hello => self.send_msg(ofp.fetures_req(), xid, stream),
Msg::Error => {
let error = ErrorEvent::parse(&payload);
println!("Error {:?}", error.error_type);
()
Msg::Hello => self.hello_handler(xid, stream),
Msg::Error => self.error_handler(ErrorEvent::parse(&payload)),
Msg::EchoRequest => {
self.echo_request_handler(xid, EchoRequestEvent::new(payload), stream)
}
Msg::FeaturesRequest => (),
Msg::PacketIn => {
self.packet_in_handler(xid, PacketInEvent::parse(&payload), stream);
}
Msg::PacketOut => (),
Msg::FlowMod => (),
_ => (),
}
}
......@@ -60,4 +60,17 @@ pub trait ControllerFrame10 {
header_bytes.append(&mut body_bytes);
let _ = stream.write_all(&header_bytes);
}
/**
* for handle message
*/
fn hello_handler(&self, xid: u32, stream: &mut TcpStream) {
self.send_msg(self.ofp().fetures_req(), xid, stream);
}
fn error_handler(&self, error: ErrorEvent) {
println!("Error {:?}", error.error_type);
}
fn echo_request_handler(&self, xid: u32, echo: EchoRequestEvent, stream: &mut TcpStream) {
self.send_msg(EchoReplyEvent::new(echo.payload), xid, stream);
}
}
use std::io::Write;
use crate::openflow::ofp10::{self, MessageMarshal};
pub struct EchoReplyEvent {
pub payload: Vec<u8>,
}
impl EchoReplyEvent {
pub fn new(payload: Vec<u8>) -> Self {
Self { payload }
}
}
impl MessageMarshal for EchoReplyEvent {
fn marshal(&self, bytes: &mut Vec<u8>) {
let _ = bytes.write_all(&self.payload);
}
fn msg_code(&self) -> ofp10::Msg {
ofp10::Msg::EchoReply
}
fn msg_usize<OFP: ofp10::OfpMsgEvent>(&self, ofp: &OFP) -> usize {
ofp10::Msg::EchoReply as usize
}
fn size_of(&self) -> usize {
self.payload.len()
}
}
use std::io::Write;
use crate::openflow::ofp10::{self, MessageMarshal, Msg};
pub struct EchoRequestEvent {
pub payload: Vec<u8>,
}
impl EchoRequestEvent {
pub fn new(payload: Vec<u8>) -> Self {
Self { payload }
}
}
impl MessageMarshal for EchoRequestEvent {
fn marshal(&self, bytes: &mut Vec<u8>) {
let _ = bytes.write_all(&self.payload);
}
fn msg_code(&self) -> ofp10::Msg {
Msg::EchoRequest
}
fn msg_usize<OFP: ofp10::OfpMsgEvent>(&self, _: &OFP) -> usize {
Msg::EchoRequest as usize
}
fn size_of(&self) -> usize {
self.payload.len()
}
}
......@@ -21,3 +21,8 @@ pub use features_req::FeaturesReqEvent;
pub mod payload;
pub use payload::Payload;
pub mod echo_request;
pub use echo_request::EchoRequestEvent;
pub mod echo_reply;
\ No newline at end of file
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