1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
#[cfg(test)]
mod test;
use crate::bus_listener::BusListenerEvent;
use crate::core::{
BusEvent, BusListenerCookie, BusListenerFilter, BusListenerScope, Deserialize,
DeserializeError, Deserializer, ObjectId, ObjectUuid, Serialize, SerializeError, Serializer,
};
use crate::error::Error;
use crate::handle::Handle;
use crate::object::Object;
use futures_channel::mpsc::UnboundedReceiver;
use futures_core::future::FusedFuture;
use futures_core::stream::Stream;
use std::future::{self, Future};
use std::pin::Pin;
use std::task::{Context, Poll};
/// A scope that notifies other clients when ends.
///
/// Aldrin services are inherently stateless with respect to the clients that are using
/// them. Sometimes it becomes necessary to know when a client is no longer interested in using a
/// service.
///
/// Scopes and [`Lifetime`s](Lifetime) solve this problem in a robust way. One client can create a
/// `LifetimeScope` while another binds a `Lifetime` to it. The `Lifetime` will then notify when the
/// scope ends. This can be triggered explicitly ([`end`](Self::end)) or implicitly by dropping the
/// scope. A scope also ends when the client that owns it disconnects from be bus for some reason.
///
/// # Examples
///
/// ```
/// # use aldrin_test::tokio::TestBroker;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let broker = TestBroker::new();
/// # let client1 = broker.add_client().await;
/// # let client2 = broker.add_client().await;
/// // Assume client 1 is using a service from client 2. In turn, client 2 needs to know when
/// // client 1 is done.
///
/// // Client 1 creates a scope and passes its id to client 2.
/// let scope = client1.create_lifetime_scope().await?;
/// let id = scope.id();
///
/// // Client 2 creates a lifetime from the id.
/// let lifetime = client2.create_lifetime(id).await?;
///
/// tokio::spawn(async move {
/// // Move in the scope and do some work. The scope is dropped at the end.
/// let _scope = scope;
/// });
///
/// // Resolves when the associated scope ends.
/// lifetime.await;
///
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct LifetimeScope {
object: Object,
}
impl LifetimeScope {
pub(crate) fn new(object: Object) -> Self {
LifetimeScope { object }
}
/// Creates a new scope.
pub async fn create(handle: &Handle) -> Result<Self, Error> {
handle.create_lifetime_scope().await
}
/// Return the scope's id.
pub fn id(&self) -> LifetimeId {
LifetimeId(self.object.id())
}
/// Returns a handle to the client that was used to create the scope.
pub fn handle(&self) -> &Handle {
self.object.handle()
}
/// Ends the scope.
///
/// If the scope has already ended, [`Error::InvalidLifetime`] is returned.
pub async fn end(&self) -> Result<(), Error> {
self.object.destroy().await.map_err(|e| {
if e == Error::InvalidObject {
Error::InvalidLifetime
} else {
e
}
})
}
}
impl Serialize for LifetimeScope {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
self.id().serialize(serializer)
}
}
/// Id of a scope's lifetime.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
#[repr(transparent)]
pub struct LifetimeId(pub ObjectId);
impl LifetimeId {
/// Nil `LifetimeId` (all zeros).
pub const NIL: Self = Self(ObjectId::NIL);
/// Bind the id to a client and create a `Lifetime`.
pub async fn bind(self, handle: &Handle) -> Result<Lifetime, Error> {
Lifetime::create(handle, self).await
}
/// Checks if the id is nil (all zeros).
pub const fn is_nil(&self) -> bool {
self.0.is_nil()
}
}
impl Serialize for LifetimeId {
fn serialize(&self, serializer: Serializer) -> Result<(), SerializeError> {
self.0.serialize(serializer)
}
}
impl Deserialize for LifetimeId {
fn deserialize(deserializer: Deserializer) -> Result<Self, DeserializeError> {
ObjectId::deserialize(deserializer).map(Self)
}
}
impl From<ObjectId> for LifetimeId {
fn from(id: ObjectId) -> Self {
Self(id)
}
}
impl From<LifetimeId> for ObjectId {
fn from(id: LifetimeId) -> Self {
id.0
}
}
/// Notifies when a [`LifetimeScope`] ends.
///
/// See [`LifetimeScope`] for information and usages examples.
#[derive(Debug)]
pub struct Lifetime {
listener: Option<LifetimeListener>,
id: LifetimeId,
found: bool,
}
impl Lifetime {
/// Create a `Lifetime` from an id.
pub async fn create(handle: &Handle, id: LifetimeId) -> Result<Self, Error> {
let listener = handle.create_lifetime_listener().await?;
listener.start(id.0.uuid).await?;
Ok(Self {
listener: Some(listener),
id,
found: false,
})
}
/// Poll whether the associated scope has ended.
pub fn poll_ended(&mut self, cx: &mut Context) -> Poll<()> {
let Some(ref mut listener) = self.listener else {
return Poll::Ready(());
};
loop {
let event = match listener.poll_next_event(cx) {
Poll::Ready(Some(event)) => event,
Poll::Ready(None) => break,
Poll::Pending => return Poll::Pending,
};
match event {
BusListenerEvent::Started(_) => {}
BusListenerEvent::Event(BusEvent::ObjectCreated(id)) => {
debug_assert_eq!(id.uuid, self.id.0.uuid);
if id.cookie == self.id.0.cookie {
self.found = true;
} else {
break;
}
}
BusListenerEvent::Event(BusEvent::ObjectDestroyed(id)) => {
debug_assert_eq!(id.uuid, self.id.0.uuid);
break;
}
BusListenerEvent::CurrentFinished => {
if !self.found {
break;
}
}
BusListenerEvent::Stopped
| BusListenerEvent::Event(BusEvent::ServiceCreated(_))
| BusListenerEvent::Event(BusEvent::ServiceDestroyed(_)) => unreachable!(),
}
}
self.listener = None;
Poll::Ready(())
}
/// Await the associated scope to end.
pub async fn ended(&mut self) {
future::poll_fn(|cx| self.poll_ended(cx)).await
}
/// Check if the associated scope has ended.
///
/// This will only return true after [`poll_ended`](Self::poll_ended) has returned
/// `Poll::Ready(())` or [`ended`](Self::ended) has been awaited.
pub fn has_ended(&self) -> bool {
self.listener.is_none()
}
}
impl Future for Lifetime {
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<()> {
self.poll_ended(cx)
}
}
impl FusedFuture for Lifetime {
fn is_terminated(&self) -> bool {
self.has_ended()
}
}
#[derive(Debug)]
pub(crate) struct LifetimeListener {
cookie: BusListenerCookie,
client: Handle,
events: UnboundedReceiver<BusListenerEvent>,
}
impl LifetimeListener {
pub(crate) fn new(
cookie: BusListenerCookie,
client: Handle,
events: UnboundedReceiver<BusListenerEvent>,
) -> Self {
Self {
cookie,
client,
events,
}
}
async fn start(&self, uuid: ObjectUuid) -> Result<(), Error> {
self.client
.add_bus_listener_filter(self.cookie, BusListenerFilter::object(uuid))?;
self.client
.start_bus_listener(self.cookie, BusListenerScope::All)
.await?;
Ok(())
}
fn poll_next_event(&mut self, cx: &mut Context) -> Poll<Option<BusListenerEvent>> {
Pin::new(&mut self.events).poll_next(cx)
}
}
impl Drop for LifetimeListener {
fn drop(&mut self) {
self.client.destroy_bus_listener_now(self.cookie);
}
}