Pass the stream to sender
This commit is contained in:
parent
f55a6bd581
commit
2a5f6063b0
3 changed files with 19 additions and 10 deletions
13
src/ear.rs
13
src/ear.rs
|
@ -14,20 +14,23 @@ impl Ear {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn listen(&self) -> Vec<String> {
|
pub fn listen(&self) -> (TcpStream, Vec<String>) {
|
||||||
let listener = TcpListener::bind(format!("{}:{}", self.ip_addr, self.port)).unwrap();
|
let listener = TcpListener::bind(format!("{}:{}", self.ip_addr, self.port)).unwrap();
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
let stream = stream.unwrap();
|
let stream = stream.unwrap();
|
||||||
|
|
||||||
let req = self.receive_request(stream);
|
let req = self.receive_request(&stream);
|
||||||
let req = parse_url(req);
|
let req = parse_url(req);
|
||||||
return req;
|
return (stream, req);
|
||||||
}
|
}
|
||||||
return vec![String::new()];
|
|
||||||
|
// TODO: remove this, I'm just making it compile
|
||||||
|
let stream = TcpStream::connect(&self.ip_addr).unwrap();
|
||||||
|
return (stream, vec![String::new()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_request(&self, mut stream: TcpStream) -> Vec<String> {
|
fn receive_request(&self, mut stream: &TcpStream) -> Vec<String> {
|
||||||
let buf_reader = BufReader::new(&mut stream);
|
let buf_reader = BufReader::new(&mut stream);
|
||||||
let http_request: Vec<String> = buf_reader
|
let http_request: Vec<String> = buf_reader
|
||||||
.lines()
|
.lines()
|
||||||
|
|
|
@ -10,9 +10,9 @@ use serde_json::Value;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let listener = Ear::new();
|
let listener = Ear::new();
|
||||||
let sender = Mouth::new();
|
|
||||||
|
|
||||||
let req = listener.listen();
|
let (stream, req) = listener.listen();
|
||||||
|
let sender = Mouth::new(stream);
|
||||||
// let data: String = get_data(&req); // TODO: update type
|
// let data: String = get_data(&req); // TODO: update type
|
||||||
// let json_data = transform_to_json(&data);
|
// let json_data = transform_to_json(&data);
|
||||||
let json_data = serde_json::json!({"test": 200, "foo": "bar"});
|
let json_data = serde_json::json!({"test": 200, "foo": "bar"});
|
||||||
|
|
12
src/mouth.rs
12
src/mouth.rs
|
@ -1,10 +1,16 @@
|
||||||
|
use std::net::TcpStream;
|
||||||
|
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
pub struct Mouth {}
|
pub struct Mouth {
|
||||||
|
stream: TcpStream,
|
||||||
|
}
|
||||||
|
|
||||||
impl Mouth {
|
impl Mouth {
|
||||||
pub fn new() -> Self {
|
pub fn new(stream: TcpStream) -> Self {
|
||||||
Self {}
|
Self {
|
||||||
|
stream: stream,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_data(&self, data: Value) {
|
pub fn send_data(&self, data: Value) {
|
||||||
|
|
Loading…
Reference in a new issue