pub struct Service { /* private fields */ }
Expand description
Owned service on the bus.
Service
s are associated with an Object
and created with
Object::create_service
. Service
s can be
destroyed again by calling destroy
, by dropping them, or implicitly when
the Object
is destroyed.
Service
exposes an asynchronous stream of incoming FunctionCall
s via its
implementation of the Stream
trait.
Events can be emitted directly with Handle::emit_event
. This is available on Handle
,
because usually Service
is borrowed mutably to wait for function calls. The ServiceId
required for Handle::emit_event
can be obtained with e.g. Service::id
.
§Examples
Creating and destroying Service
s:
use aldrin::Error;
use aldrin::core::{ObjectUuid, ServiceUuid};
use std::mem;
use uuid::uuid;
const SERVICE_UUID: ServiceUuid = ServiceUuid(uuid!("f88f1706-9609-42a4-8796-4e7bb8c3ef24"));
let object = handle.create_object(ObjectUuid::new_v4()).await?;
// Create a service and destroy it again explicitly:
let service = object.create_service(SERVICE_UUID, 1).await?;
service.destroy().await?;
// Destroy a service implicitly by dropping it:
let service = object.create_service(SERVICE_UUID, 1).await?;
mem::drop(service);
// Destroy a service implicitly by dropping the object:
let service = object.create_service(SERVICE_UUID, 1).await?;
let service_id = service.id();
mem::drop(object);
assert_eq!(service.destroy().await, Err(Error::InvalidService));
The following is a small chat server example, which shows how to handle function call on a service and how to emit events.
use aldrin::core::{ObjectUuid, ServiceUuid};
use std::collections::HashSet;
use uuid::uuid;
const CHAT_UUID: ServiceUuid = ServiceUuid(uuid!("91334d42-7045-4292-99dc-9fd89c5f104f"));
// Functions
const SHUTDOWN: u32 = 1;
const JOIN_CHAT: u32 = 2;
const LEAVE_CHAT: u32 = 3;
const LIST_USERS: u32 = 4;
const SEND_MESSAGE: u32 = 5;
// Events
const JOINED_CHAT: u32 = 1;
const LEFT_CHAT: u32 = 2;
const MESSAGE_SENT: u32 = 3;
// Create object and our chat service:
let object = handle.create_object(ObjectUuid::new_v4()).await?;
let mut service = object.create_service(CHAT_UUID, 1).await?;
let service_id = service.id();
// HashSet to keep track of users:
let mut users = HashSet::new();
// Iterate (asynchronously) over incoming function calls. `func` is of type `FunctionCall`,
// which contains the function's id, the arguments, and a reply object.
while let Some(func) = service.next_function_call().await {
match func.id {
SHUTDOWN => break,
JOIN_CHAT => {
let name: String = func.args.deserialize()?;
if users.insert(name.clone()) {
// Emit an event that a new user with the given name joined:
handle.emit_event(service_id, JOINED_CHAT, &name)?;
func.reply.ok(&())?;
} else {
// Signal that the name is already taken.
func.reply.err(&())?;
}
}
LEAVE_CHAT => {
let name: String = func.args.deserialize()?;
if users.remove(&name) {
// Emit an event that a user with the given name left:
handle.emit_event(service_id, LEFT_CHAT, &name)?;
func.reply.ok(&())?;
} else {
// Signal that the name is not known.
func.reply.err(&())?;
}
}
LIST_USERS => func.reply.ok(&users)?,
SEND_MESSAGE => {
// Broadcast the message:
let message = func.args.deserialize()?;
handle.emit_event(service_id, MESSAGE_SENT, &message)?;
func.reply.ok(&())?;
}
_ => {}
}
}
Implementations§
source§impl Service
impl Service
sourcepub fn handle(&self) -> &Handle
pub fn handle(&self) -> &Handle
Returns a handle to the client that was used to create the service.
sourcepub async fn destroy(&self) -> Result<(), Error>
pub async fn destroy(&self) -> Result<(), Error>
Destroys the service.
If the Service
has already been destroyed, then Error::InvalidService
is returned.
sourcepub fn poll_next_function_call(
&mut self,
cx: &mut Context<'_>
) -> Poll<Option<FunctionCall>>
pub fn poll_next_function_call( &mut self, cx: &mut Context<'_> ) -> Poll<Option<FunctionCall>>
Polls for the next function call.
sourcepub async fn next_function_call(&mut self) -> Option<FunctionCall>
pub async fn next_function_call(&mut self) -> Option<FunctionCall>
Returns the next function call.
Trait Implementations§
source§impl FusedStream for Service
impl FusedStream for Service
source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.source§impl Stream for Service
impl Stream for Service
§type Item = FunctionCall
type Item = FunctionCall
Auto Trait Implementations§
impl Freeze for Service
impl !RefUnwindSafe for Service
impl Send for Service
impl Sync for Service
impl Unpin for Service
impl !UnwindSafe for Service
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> StreamExt for T
impl<T> StreamExt for T
source§fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
fn next(&mut self) -> Next<'_, Self>where
Self: Unpin,
source§fn into_future(self) -> StreamFuture<Self>
fn into_future(self) -> StreamFuture<Self>
source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
source§fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
source§fn collect<C>(self) -> Collect<Self, C>
fn collect<C>(self) -> Collect<Self, C>
source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB>
source§fn concat(self) -> Concat<Self>
fn concat(self) -> Concat<Self>
source§fn count(self) -> Count<Self>where
Self: Sized,
fn count(self) -> Count<Self>where
Self: Sized,
source§fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F>
source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>
true
if any element in stream satisfied a predicate. Read moresource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>
true
if all element in stream satisfied a predicate. Read moresource§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
source§fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
StreamExt::fold
that holds internal state
and produces a new stream. Read moresource§fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
true
. Read moresource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
true
. Read moresource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F>
source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
items of the underlying stream. Read moresource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
items of the underlying stream. Read moresource§fn zip<St>(self, other: St) -> Zip<Self, St>
fn zip<St>(self, other: St) -> Zip<Self, St>
source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek
method. Read moresource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
source§fn left_stream<B>(self) -> Either<Self, B>
fn left_stream<B>(self) -> Either<Self, B>
source§fn right_stream<B>(self) -> Either<B, Self>
fn right_stream<B>(self) -> Either<B, Self>
source§fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
Stream::poll_next
on Unpin
stream types.