-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generics to InternalServiceConnector
- Loading branch information
Showing
50 changed files
with
198 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
citadel-internal-service-connector/src/io_interface/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use async_trait::async_trait; | ||
use citadel_internal_service_types::InternalServicePayload; | ||
use futures::{Sink, Stream}; | ||
|
||
pub mod in_memory; | ||
pub mod tcp; | ||
|
||
#[async_trait] | ||
pub trait IOInterface: Sized + Send + 'static { | ||
type Sink: Sink<InternalServicePayload, Error = std::io::Error> + Unpin + Send + 'static; | ||
type Stream: Stream<Item = std::io::Result<InternalServicePayload>> + Unpin + Send + 'static; | ||
async fn next_connection(&mut self) -> Option<(Self::Sink, Self::Stream)>; | ||
} |
62 changes: 62 additions & 0 deletions
62
citadel-internal-service-connector/src/io_interface/tcp.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::codec::SerializingCodec; | ||
use crate::connector::{wrap_tcp_conn, InternalServiceConnector, WrappedSink, WrappedStream}; | ||
use crate::io_interface::IOInterface; | ||
use async_trait::async_trait; | ||
use citadel_internal_service_types::{InternalServicePayload, InternalServiceResponse}; | ||
use futures::stream::{SplitSink, SplitStream}; | ||
use futures::StreamExt; | ||
use tokio::net::{TcpListener, TcpStream, ToSocketAddrs}; | ||
use tokio_util::codec::Framed; | ||
|
||
pub struct TcpIOInterface { | ||
pub listener: TcpListener, | ||
} | ||
|
||
impl TcpIOInterface { | ||
pub async fn new<T: ToSocketAddrs>(bind_address: T) -> std::io::Result<Self> { | ||
let listener = TcpListener::bind(bind_address).await?; | ||
Ok(Self { listener }) | ||
} | ||
} | ||
|
||
impl InternalServiceConnector<TcpIOInterface> { | ||
pub async fn connect<T: ToSocketAddrs>(addr: T) -> Result<Self, Box<dyn std::error::Error>> { | ||
let conn = TcpStream::connect(addr).await?; | ||
let (sink, mut stream) = wrap_tcp_conn(conn).split(); | ||
let greeter_packet = stream | ||
.next() | ||
.await | ||
.ok_or("Failed to receive greeting packet")??; | ||
if matches!( | ||
greeter_packet, | ||
InternalServicePayload::Response(InternalServiceResponse::ServiceConnectionAccepted(_)) | ||
) { | ||
let stream = WrappedStream { inner: stream }; | ||
let sink = WrappedSink { inner: sink }; | ||
Ok(Self { sink, stream }) | ||
} else { | ||
Err("Failed to receive greeting packet")? | ||
} | ||
} | ||
|
||
pub fn split(self) -> (WrappedSink<TcpIOInterface>, WrappedStream<TcpIOInterface>) { | ||
(self.sink, self.stream) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl IOInterface for TcpIOInterface { | ||
type Sink = SplitSink< | ||
Framed<TcpStream, SerializingCodec<InternalServicePayload>>, | ||
InternalServicePayload, | ||
>; | ||
type Stream = SplitStream<Framed<TcpStream, SerializingCodec<InternalServicePayload>>>; | ||
|
||
async fn next_connection(&mut self) -> Option<(Self::Sink, Self::Stream)> { | ||
self.listener | ||
.accept() | ||
.await | ||
.ok() | ||
.map(|(stream, _)| wrap_tcp_conn(stream).split()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod codec; | ||
pub mod util; | ||
pub mod connector; | ||
|
||
pub mod io_interface; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.