use super::*;
use crate::{push, wire};
use std::time::Instant;
pub struct AccountPushConnection {
client: NativeClient,
connection: push::Connection,
credential_generation: u64,
}
impl NativeClient {
pub fn open_push_connection_blocking(&self) -> Result<AccountPushConnection> {
let (exchange, state) = self
.owned_account()?
.begin()
.map_err(|error| error.context(NativeRequestNotSubmitted))?;
let credential_generation = exchange.credential_generation;
let options = self.options.clone();
let connection = Self::execute_account_request(
options.clone(),
exchange,
state,
Arc::new(AtomicBool::new(false)),
|_, state, check| {
check()?;
let connection =
push::Connection::open(state, options.timeout, options.receive_budget)?;
check()?;
Ok(connection)
},
)?;
Ok(AccountPushConnection {
client: self.clone(),
connection,
credential_generation,
})
}
}
impl AccountPushConnection {
fn request<T>(
&mut self,
operation: impl FnOnce(
&mut push::Connection,
&mut AccountState,
&dyn Fn() -> Result<()>,
) -> Result<T>,
) -> Result<T> {
let (exchange, state) = begin_push_request(&self.client, self.credential_generation)?;
NativeClient::execute_account_request(
self.client.options.clone(),
exchange,
state,
Arc::new(AtomicBool::new(false)),
|_, state, check| operation(&mut self.connection, state, check),
)
}
pub fn heartbeat(&mut self, cursor: &[u8]) -> Result<wire::Heartbeat> {
self.request(|connection, state, check| connection.heartbeat_guarded(state, cursor, check))
}
pub fn sync(&mut self, cursor: &[u8], scene: u32) -> Result<wire::SyncBatch> {
self.request(|connection, state, check| {
connection.sync_guarded(state, cursor, scene, check)
})
}
pub fn poll(&mut self, timeout: Duration) -> Result<Option<push::Notification>> {
ensure!(
!timeout.is_zero() && timeout <= Duration::from_secs(300),
"Invalid account notification timeout"
);
self.request(|connection, _, check| {
let deadline = Instant::now() + timeout;
loop {
check()?;
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Ok(None);
}
let notification = connection.poll(remaining.min(Duration::from_secs(1)))?;
check().map_err(|_| anyhow::anyhow!("Account notification session superseded"))?;
if notification.is_some() {
return Ok(notification);
}
}
})
}
}
fn begin_push_request(client: &NativeClient, connected: u64) -> Result<(Exchange, AccountState)> {
let (exchange, state) = client
.owned_account()?
.begin()
.map_err(|error| error.context(NativeRequestNotSubmitted))?;
ensure!(
exchange.credential_generation == connected,
"Account notification credentials superseded"
);
Ok((exchange, state))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn renewal_with_identical_keys_invalidates_an_existing_notification_transport() {
let client = super::super::tests::client();
let (first, _) = begin_push_request(&client, 0).unwrap();
drop(first);
let (renewal, same_credentials) = client.owned_account().unwrap().begin().unwrap();
renewal.accept_credentials(same_credentials).unwrap();
drop(renewal);
assert!(begin_push_request(&client, 0).is_err());
let (current, _) = begin_push_request(&client, 1).unwrap();
drop(current);
client.stop().unwrap();
let error = begin_push_request(&client, 1).err().unwrap();
assert!(error.is::<NativeRequestNotSubmitted>());
assert_eq!(client.account_status().unwrap().in_flight, 0);
}
}