Commit 466cd82f authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

improve tcp_listener of ofp10

parent 239a28c0
...@@ -16,6 +16,7 @@ use crate::{ ...@@ -16,6 +16,7 @@ use crate::{
* In production please remove allow unused. * In production please remove allow unused.
*/ */
#[derive(Clone)]
pub struct Controller { pub struct Controller {
mac_to_port: HashMap<u64, u16>, mac_to_port: HashMap<u64, u16>,
} }
......
...@@ -3,6 +3,7 @@ use tenjin::{openflow::ofp10::ControllerFrame10, Controller}; ...@@ -3,6 +3,7 @@ use tenjin::{openflow::ofp10::ControllerFrame10, Controller};
extern crate byteorder; extern crate byteorder;
fn main() -> Result<(), std::io::Error> { fn main() -> Result<(), std::io::Error> {
Controller::listener("127.0.0.1:6653"); let controller = Controller::new();
controller.listener("127.0.0.1:6633");
Ok(()) Ok(())
} }
...@@ -9,16 +9,24 @@ use super::{ ...@@ -9,16 +9,24 @@ use super::{
tcp_listener_handler, MessageMarshal, OfpMsgEvent, Openflow10, OpenflowHeader, tcp_listener_handler, MessageMarshal, OfpMsgEvent, Openflow10, OpenflowHeader,
}; };
pub trait ControllerFrame10 { pub trait ControllerFrame10
where
Self: 'static,
{
fn ofp(&self) -> ofp10::Openflow10 { fn ofp(&self) -> ofp10::Openflow10 {
Openflow10::new() Openflow10::new()
} }
fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream); fn packet_in_handler(&mut self, xid: u32, packetin: PacketInEvent, stream: &mut TcpStream);
fn new() -> Self; fn new() -> Self;
fn listener(address: &str) { fn listener(&self, address: &str)
where
Self: Sized,
Self: Send,
Self: Clone,
{
println!("server run at {}", address); println!("server run at {}", address);
let _ = tcp_listener_handler(address); let _ = tcp_listener_handler(address, self.clone());
} }
fn handle_header(&mut self, buf: &mut Vec<u8>) -> Option<(u8, usize, u32)> { fn handle_header(&mut self, buf: &mut Vec<u8>) -> Option<(u8, usize, u32)> {
......
...@@ -5,7 +5,10 @@ use crate::openflow::ofp10::HelloEvent; ...@@ -5,7 +5,10 @@ use crate::openflow::ofp10::HelloEvent;
use super::{ControllerFrame10, OfpMsgEvent}; use super::{ControllerFrame10, OfpMsgEvent};
pub fn tcp_listener_handler(address: &str) -> Result<(), std::io::Error> { pub fn tcp_listener_handler(
address: &str,
controller: impl ControllerFrame10 + Send + 'static + Clone,
) -> Result<(), std::io::Error> {
let listener = TcpListener::bind(address)?; let listener = TcpListener::bind(address)?;
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
...@@ -14,8 +17,8 @@ pub fn tcp_listener_handler(address: &str) -> Result<(), std::io::Error> { ...@@ -14,8 +17,8 @@ pub fn tcp_listener_handler(address: &str) -> Result<(), std::io::Error> {
println!("server has connection from {}", addr); println!("server has connection from {}", addr);
} }
let mut ctrl = controller.clone();
thread::spawn(move || { thread::spawn(move || {
let mut ctrl = Controller::new();
ctrl.send_msg(HelloEvent::new(), 0, &mut stream); ctrl.send_msg(HelloEvent::new(), 0, &mut stream);
let ofp_size = ctrl.ofp().header_size(); let ofp_size = ctrl.ofp().header_size();
let mut buffer = vec![0u8; ofp_size]; let mut buffer = vec![0u8; ofp_size];
......
...@@ -9,7 +9,6 @@ pub fn tcp_listener_handler( ...@@ -9,7 +9,6 @@ pub fn tcp_listener_handler(
controller: impl ControllerFrame13 + Send + 'static + Clone, controller: impl ControllerFrame13 + Send + 'static + Clone,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
let listener = TcpListener::bind(address)?; let listener = TcpListener::bind(address)?;
// let controller = Arc::new(Mutex::new(controller));
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(mut stream) => { Ok(mut stream) => {
......
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