mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-14 19:55:03 +01:00
inject dependency on haesli_messaging into haesli_transport to simplify dependency graph
This commit is contained in:
parent
92e3ac486b
commit
b4ecb6b5d4
8 changed files with 47 additions and 16 deletions
|
|
@ -27,7 +27,7 @@ use tracing::{debug, error, info, trace, warn};
|
|||
use crate::{
|
||||
error::{ConException, ProtocolError, Result, TransError},
|
||||
frame::{self, parse_content_header, Frame, FrameType, MaxFrameSize},
|
||||
methods, sasl,
|
||||
methods, sasl, Handlers,
|
||||
};
|
||||
|
||||
fn ensure_conn(condition: bool) -> Result<()> {
|
||||
|
|
@ -67,6 +67,8 @@ pub struct TransportConnection {
|
|||
event_sender: ConEventSender,
|
||||
/// To receive events from other futures
|
||||
event_receiver: ConEventReceiver,
|
||||
|
||||
handlers: Handlers,
|
||||
}
|
||||
|
||||
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
|
||||
|
|
@ -91,6 +93,7 @@ impl TransportConnection {
|
|||
global_data: GlobalData,
|
||||
method_queue_send: ConEventSender,
|
||||
method_queue_recv: ConEventReceiver,
|
||||
handlers: Handlers,
|
||||
) -> Self {
|
||||
Self {
|
||||
id,
|
||||
|
|
@ -104,6 +107,7 @@ impl TransportConnection {
|
|||
global_data,
|
||||
event_sender: method_queue_send,
|
||||
event_receiver: method_queue_recv,
|
||||
handlers,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -416,8 +420,10 @@ impl TransportConnection {
|
|||
// call into haesli_messaging to handle the method
|
||||
// it returns the response method that we are supposed to send
|
||||
// maybe this might become an `Option` in the future
|
||||
let return_method =
|
||||
haesli_messaging::methods::handle_method(channel_handle, method).await?;
|
||||
let return_method = (self.handlers.handle_method)(channel_handle, method)?;
|
||||
|
||||
//let return_method =
|
||||
// haesli_messaging::methods::handle_method(channel_handle, method).await?;
|
||||
self.send_method(frame.channel, &return_method).await?;
|
||||
}
|
||||
}
|
||||
|
|
@ -513,7 +519,8 @@ impl TransportConnection {
|
|||
|
||||
let channel = self.channels.get(&channel).ok_or(ConException::Todo)?;
|
||||
|
||||
haesli_messaging::methods::publish(channel.global_chan.clone(), message)?;
|
||||
(self.handlers.handle_basic_publish)(channel.global_chan.clone(), message)?;
|
||||
//haesli_messaging::methods::publish(channel.global_chan.clone(), message)?;
|
||||
Ok(())
|
||||
} else {
|
||||
Err(ConException::Todo.into())
|
||||
|
|
|
|||
|
|
@ -13,18 +13,32 @@ mod tests;
|
|||
use std::{future::Future, net::SocketAddr};
|
||||
|
||||
use anyhow::Context;
|
||||
use haesli_core::{connection::ConnectionEvent, queue::QueueEvent, GlobalData};
|
||||
use haesli_core::{
|
||||
connection::{Channel, ConnectionEvent},
|
||||
error::ProtocolError,
|
||||
message::Message,
|
||||
methods::Method,
|
||||
queue::QueueEvent,
|
||||
GlobalData,
|
||||
};
|
||||
use tokio::{net, net::TcpStream, select};
|
||||
use tracing::{info, info_span, Instrument};
|
||||
|
||||
use crate::connection::TransportConnection;
|
||||
|
||||
pub async fn do_thing_i_guess(
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct Handlers {
|
||||
pub handle_method: fn(Channel, Method) -> Result<Method, ProtocolError>,
|
||||
pub handle_basic_publish: fn(Channel, Message) -> Result<(), ProtocolError>,
|
||||
}
|
||||
|
||||
pub async fn connection_loop(
|
||||
global_data: GlobalData,
|
||||
terminate: impl Future + Send,
|
||||
handlers: Handlers,
|
||||
) -> anyhow::Result<()> {
|
||||
select! {
|
||||
res = accept_cons(global_data.clone()) => {
|
||||
res = accept_cons(global_data.clone(), handlers) => {
|
||||
res
|
||||
}
|
||||
_ = terminate => {
|
||||
|
|
@ -33,18 +47,18 @@ pub async fn do_thing_i_guess(
|
|||
}
|
||||
}
|
||||
|
||||
async fn accept_cons(global_data: GlobalData) -> anyhow::Result<()> {
|
||||
async fn accept_cons(global_data: GlobalData, handlers: Handlers) -> anyhow::Result<()> {
|
||||
info!("Binding TCP listener...");
|
||||
let listener = net::TcpListener::bind(("127.0.0.1", 5672)).await?;
|
||||
info!(addr = ?listener.local_addr()?, "Successfully bound TCP listener");
|
||||
|
||||
loop {
|
||||
let connection = listener.accept().await?;
|
||||
handle_con(global_data.clone(), connection);
|
||||
handle_con(global_data.clone(), connection, handlers);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_con(global_data: GlobalData, connection: (TcpStream, SocketAddr)) {
|
||||
fn handle_con(global_data: GlobalData, connection: (TcpStream, SocketAddr), handlers: Handlers) {
|
||||
let (stream, peer_addr) = connection;
|
||||
let id = rand::random();
|
||||
|
||||
|
|
@ -72,6 +86,7 @@ fn handle_con(global_data: GlobalData, connection: (TcpStream, SocketAddr)) {
|
|||
global_data.clone(),
|
||||
method_send,
|
||||
method_recv,
|
||||
handlers,
|
||||
);
|
||||
|
||||
tokio::spawn(connection.start_connection_processing().instrument(span));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue