Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register and Connect Util Functions #35

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions citadel-internal-service-connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ edition = "2021"

[dependencies]
citadel-internal-service-types = { workspace = true }
citadel_logging = { workspace = true }
tokio = { workspace = true, features = ["net", "rt", "macros"] }
tokio-util = { workspace = true, features = ["codec"] }
bincode2 = { workspace = true }
serde = { workspace = true }
futures = { workspace = true, features = ["alloc"] }
uuid = { workspace = true }
citadel_logging = { workspace = true }
async-trait = "0.1.79"
uuid = { workspace = true, features = ["v4"]}
async-trait = "0.1.79"
13 changes: 13 additions & 0 deletions citadel-internal-service-connector/src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::connector::ClientError;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::error::Error;
Expand Down Expand Up @@ -37,6 +38,18 @@ impl From<CodecError> for std::io::Error {
}
}

impl From<CodecError> for ClientError {
fn from(value: CodecError) -> Self {
ClientError::CodecError(value.reason)
}
}

impl From<std::io::Error> for ClientError {
fn from(value: std::io::Error) -> Self {
ClientError::SendError(value.to_string())
}
}

impl<T: Serialize> Encoder<T> for SerializingCodec<T> {
type Error = std::io::Error;

Expand Down
43 changes: 43 additions & 0 deletions citadel-internal-service-connector/src/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use citadel_internal_service_types::{
InternalServicePayload, InternalServiceRequest, InternalServiceResponse,
};
use futures::{Sink, Stream, StreamExt};
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::net::TcpStream;
Expand All @@ -22,6 +24,47 @@ pub struct WrappedSink<T: IOInterface> {
pub inner: T::Sink,
}

#[derive(Debug, Clone)]
pub enum ClientError {
ConnectionToInternalServiceFailed(String),
InternalServiceDisconnected,
InternalServiceInvalidResponse(String),
CodecError(String),
SendError(String),
}

impl Display for ClientError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
}
}

impl Error for ClientError {}

#[macro_export]
macro_rules! scan_for_response {
($stream:expr, $required_packet:pat) => {{
loop {
match $stream.next().await {
Some(response) => {
if response.is_error() {
return Err(ClientError::InternalServiceInvalidResponse(format!(
"{response:?}"
)));
}

if matches!(response, $required_packet) {
break response;
}

citadel_logging::trace!("Service Connector - Unrelated response: {response:?}");
}
None => return Err(ClientError::InternalServiceDisconnected)?,
}
}
}};
}

impl<T: IOInterface> Stream for WrappedStream<T> {
type Item = InternalServiceResponse;

Expand Down
Loading
Loading