Commit 943ae31f authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

use multi thread in tcp connect

parent fbf4d695
......@@ -4,7 +4,6 @@ use tenjin::openflow::Controller;
extern crate byteorder;
fn main() -> Result<(), std::io::Error> {
let mut controller = Controller::new(Openflow10::new());
controller.listener("127.0.0.1:6633");
Controller::listener("127.0.0.1:6633", Openflow10::new());
Ok(())
}
......@@ -33,8 +33,8 @@ impl<OME: OfpMsgEvent> Controller<OME> {
}
}
pub fn listener(&mut self, address: &str) {
tcp_listener_handler(self, address);
pub fn listener(address: &str, ofp: OME) {
tcp_listener_handler(ofp.version() as u8, address);
}
pub fn request_handler(&mut self, buf: &mut Vec<u8>, stream: &mut TcpStream) {
......
/**
* this macro use for create Controller with version inside thread.
* Tt is used at 'tcp_listener/tcp_listener_handler.rs'
*/
#[macro_export]
macro_rules! ofp_from_version {
($ofp_version: expr) => {
match $ofp_version {
1 => Openflow10::new(),
_ => panic!("This version is not support")
}
};
}
......@@ -7,3 +7,5 @@ pub use traiter::OfpMsgEvent;
pub mod ofp_v1_0;
pub use ofp_v1_0::Openflow10;
pub mod macro_selector;
......@@ -15,6 +15,9 @@ impl Openflow10 {
}
impl OfpMsgEvent for Openflow10 {
fn header_size(&self) -> usize {
8
}
fn hello_event(&self) -> HelloEvent {
HelloEvent::new()
}
......
......@@ -21,9 +21,11 @@ pub trait MessageMarshal {
*/
pub trait OfpMsgEvent {
fn header(&self, message: u8, length: u16, xid: u32) -> OfpHeader;
fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReq;
fn version(&self) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg;
fn header_size(&self) -> usize;
fn msg_usize(&self, msg: OfpMsg) -> usize;
fn msg_parse(&self, msg: u16) -> OfpMsg;
fn hello_event(&self) -> HelloEvent;
fn fetures_req(&self) -> FeaturesReq;
}
use std::{io::Read, net::TcpListener};
use crate::ofp_from_version;
use crate::openflow::{messages::Openflow10, traiter::OfpMsgEvent, Controller};
use std::{io::Read, net::TcpListener, thread};
use crate::openflow::{traiter::OfpMsgEvent, Controller};
pub fn tcp_listener_handler<OME: OfpMsgEvent>(controller: &mut Controller<OME>, address: &str) {
pub fn tcp_listener_handler(ofp_version: u8, address: &str) {
let listener = TcpListener::bind(address).unwrap();
/*
* buffer with 8 length that only support openflow 1.0
* I will make it support others version soon.
*/
let mut buffer = vec![0u8; 8];
for stream in listener.incoming() {
match stream {
Ok(mut stream) => {
controller.hello(&mut stream);
loop {
match stream.read(&mut buffer) {
Ok(v) if v > 0 => {
controller.request_handler(&mut buffer, &mut stream);
thread::spawn(move || {
/*
* when spawn new thread.
* The Controller will be create.
*/
let mut controller = Controller::new(ofp_from_version!(ofp_version));
controller.hello(&mut stream);
let mut buffer = vec![0u8; controller.ofp.header_size()];
loop {
match stream.read(&mut buffer) {
Ok(v) if v > 0 => {
controller.request_handler(&mut buffer, &mut stream);
}
Ok(_) | Err(_) => break,
}
Ok(_) | Err(_) => panic!("Connection failed!"),
}
}
});
}
Err(_) => todo!(),
Err(_) => panic!("Connection failed!"),
}
}
}
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