use self::{
mapper::{SubscriptionMapper, WebSocketSubMapper},
validator::SubscriptionValidator,
};
use crate::{
Identifier,
exchange::Connector,
instrument::InstrumentData,
subscription::{Map, Subscription, SubscriptionKind, SubscriptionMeta},
};
use futures::SinkExt;
use rustrade_integration::{
error::SocketError,
protocol::websocket::{WebSocket, WsMessage, connect},
};
use serde::{Deserialize, Serialize};
use std::{fmt::Debug, future::Future};
use tracing::debug;
pub mod mapper;
pub mod validator;
pub trait Subscriber: Clone + Send + Sync {
type SubMapper: SubscriptionMapper;
fn subscribe<Exchange, Instrument, Kind>(
&self,
subscriptions: &[Subscription<Exchange, Instrument, Kind>],
) -> impl Future<Output = Result<Subscribed<Instrument::Key>, SocketError>> + Send
where
Exchange: Connector + Send + Sync,
Kind: SubscriptionKind + Send + Sync,
Instrument: InstrumentData,
Subscription<Exchange, Instrument, Kind>:
Identifier<Exchange::Channel> + Identifier<Exchange::Market>;
}
#[derive(Debug)]
pub struct Subscribed<InstrumentKey> {
pub websocket: WebSocket,
pub map: Map<InstrumentKey>,
pub buffered_websocket_events: Vec<WsMessage>,
}
#[derive(
Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default, Deserialize, Serialize,
)]
pub struct WebSocketSubscriber;
impl Subscriber for WebSocketSubscriber {
type SubMapper = WebSocketSubMapper;
async fn subscribe<Exchange, Instrument, Kind>(
&self,
subscriptions: &[Subscription<Exchange, Instrument, Kind>],
) -> Result<Subscribed<Instrument::Key>, SocketError>
where
Exchange: Connector + Send + Sync,
Kind: SubscriptionKind + Send + Sync,
Instrument: InstrumentData,
Subscription<Exchange, Instrument, Kind>:
Identifier<Exchange::Channel> + Identifier<Exchange::Market>,
{
let exchange = Exchange::ID;
let url = Exchange::url()?;
debug!(%exchange, %url, ?subscriptions, "subscribing to WebSocket");
let mut websocket = connect(url).await?;
debug!(%exchange, ?subscriptions, "connected to WebSocket");
let SubscriptionMeta {
instrument_map,
ws_subscriptions,
} = Self::SubMapper::map::<Exchange, Instrument, Kind>(subscriptions);
for subscription in ws_subscriptions {
debug!(%exchange, payload = ?subscription, "sending exchange subscription");
websocket
.send(subscription)
.await
.map_err(|error| SocketError::WebSocket(Box::new(error)))?;
}
let (map, buffered_websocket_events) = Exchange::SubValidator::validate::<
Exchange,
Instrument::Key,
Kind,
>(instrument_map, &mut websocket)
.await?;
debug!(%exchange, "successfully initialised WebSocket stream with confirmed Subscriptions");
Ok(Subscribed {
websocket,
map,
buffered_websocket_events,
})
}
}