Commit ed177dfa authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

tcp_listener: move controller variable into thread -> for multi switch

parent 962c16ea
use crate::Controller; use crate::Controller;
use std::sync::{Arc, Mutex};
use std::{io::Read, net::TcpListener, thread}; use std::{io::Read, net::TcpListener, thread};
use super::controller_frame::ControllerFrame; use super::controller_frame::ControllerFrame;
use crate::openflow::ofp10::{traiter::OfpMsgEvent, HelloEvent}; use crate::openflow::ofp10::{traiter::OfpMsgEvent, HelloEvent};
pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str) { pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str) {
let controller = Arc::new(Mutex::from(Controller::new()));
let listener = TcpListener::bind(address).unwrap(); let listener = TcpListener::bind(address).unwrap();
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(mut stream) => { Ok(mut stream) => {
let controller_clone = controller.clone(); if let Ok(addr) = stream.local_addr() {
println!("server has connection from {}", addr);
}
thread::spawn(move || { thread::spawn(move || {
let mut ctrl = match controller_clone.lock() { let mut ctrl = Controller::new();
Ok(guard) => guard,
Err(poinsoned) => {
let guard = poinsoned.into_inner();
guard
}
};
ctrl.send_msg(HelloEvent::new(), 0, &mut stream); ctrl.send_msg(HelloEvent::new(), 0, &mut stream);
let ofp_size = ctrl.get_ofp().header_size(); let ofp_size = ctrl.get_ofp().header_size();
// let ofp = controller.lock().unwrap().get_ofp(); // let ofp = controller.lock().unwrap().get_ofp();
...@@ -29,7 +24,10 @@ pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str) { ...@@ -29,7 +24,10 @@ pub fn tcp_listener_handler<OME: OfpMsgEvent>(address: &str) {
Ok(v) if v > 0 => { Ok(v) if v > 0 => {
ctrl.request_handler(&mut buffer, &mut stream); ctrl.request_handler(&mut buffer, &mut stream);
} }
Ok(_) | Err(_) => break, Ok(_) | Err(_) => {
println!("cannot read packet");
break;
}
} }
} }
}); });
......
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