Pass the stream to sender

This commit is contained in:
flyingscorpio@clevo 2022-12-02 22:50:15 +01:00
parent f55a6bd581
commit 2a5f6063b0
3 changed files with 19 additions and 10 deletions

View file

@ -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();
for stream in listener.incoming() {
let stream = stream.unwrap();
let req = self.receive_request(stream);
let req = self.receive_request(&stream);
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 http_request: Vec<String> = buf_reader
.lines()

View file

@ -10,9 +10,9 @@ use serde_json::Value;
fn main() {
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 json_data = transform_to_json(&data);
let json_data = serde_json::json!({"test": 200, "foo": "bar"});

View file

@ -1,10 +1,16 @@
use std::net::TcpStream;
use serde_json::Value;
pub struct Mouth {}
pub struct Mouth {
stream: TcpStream,
}
impl Mouth {
pub fn new() -> Self {
Self {}
pub fn new(stream: TcpStream) -> Self {
Self {
stream: stream,
}
}
pub fn send_data(&self, data: Value) {