use crate::transport::HostTransport;
use actr_framework::Bytes;
use actr_protocol::{ActorResult, ActrError, ActrId, PayloadType, RpcEnvelope};
use std::sync::Arc;
pub struct HostGate {
transport: Arc<HostTransport>,
}
impl HostGate {
pub fn new(transport: Arc<HostTransport>) -> Self {
Self { transport }
}
pub async fn send_request_with_type(
&self,
_target: &ActrId,
payload_type: PayloadType,
identifier: Option<String>,
envelope: RpcEnvelope,
) -> ActorResult<Bytes> {
tracing::debug!(
"HostGate::send_request_with_type to {:?} (type={:?}, id={:?})",
_target,
payload_type,
identifier
);
self.transport
.send_request(payload_type, identifier, envelope)
.await
.map_err(|e| ActrError::Unavailable(e.to_string()))
}
pub async fn send_message_with_type(
&self,
_target: &ActrId,
payload_type: PayloadType,
identifier: Option<String>,
envelope: RpcEnvelope,
) -> ActorResult<()> {
tracing::debug!(
"HostGate::send_message_with_type to {:?} (type={:?}, id={:?})",
_target,
payload_type,
identifier
);
self.transport
.send_message(payload_type, identifier, envelope)
.await
.map_err(|e| ActrError::Unavailable(e.to_string()))
}
#[cfg(feature = "test-utils")]
pub async fn send_request(&self, target: &ActrId, envelope: RpcEnvelope) -> ActorResult<Bytes> {
tracing::info!(
"HostGate::send_request to {:?}, request_id={}",
target,
envelope.request_id
);
let result = self
.transport
.send_request(PayloadType::RpcReliable, None, envelope)
.await
.map_err(|e| ActrError::Unavailable(e.to_string()));
match &result {
Ok(_) => tracing::info!("HostGate::send_request completed successfully"),
Err(e) => tracing::error!("HostGate::send_request failed: {:?}", e),
}
result
}
#[cfg(feature = "test-utils")]
pub async fn send_message(&self, target: &ActrId, envelope: RpcEnvelope) -> ActorResult<()> {
tracing::debug!("HostGate::send_message to {}", target);
self.transport
.send_message(PayloadType::RpcReliable, None, envelope)
.await
.map_err(|e| ActrError::Unavailable(e.to_string()))
}
pub async fn send_data_stream(
&self,
_target: &ActrId,
payload_type: PayloadType,
stream_id: &str,
data: Bytes,
) -> ActorResult<()> {
tracing::debug!(
"HostGate::send_data_stream stream_id={}, size={} bytes",
stream_id,
data.len()
);
#[cfg_attr(not(feature = "opentelemetry"), allow(unused_mut))]
let mut envelope = RpcEnvelope {
route_key: "fast_path.data_stream".to_string(),
payload: Some(data),
error: None,
traceparent: None,
tracestate: None,
request_id: uuid::Uuid::new_v4().to_string(),
metadata: vec![],
timeout_ms: 0,
};
#[cfg(feature = "opentelemetry")]
{
use crate::wire::webrtc::trace::inject_span_context_to_rpc;
inject_span_context_to_rpc(&tracing::Span::current(), &mut envelope);
}
self.transport
.send_message(payload_type, Some(stream_id.to_string()), envelope)
.await
.map_err(|e| ActrError::Unavailable(e.to_string()))
}
}