pub struct Agent { /* private fields */ }
Implementations§
source§impl Agent
impl Agent
pub fn address(&self) -> &Address
pub fn id(&self) -> &AgentId
sourcepub fn publish<T: Serialize>(
&mut self,
message: OutgoingMessage<T>
) -> Result<(), Error>
pub fn publish<T: Serialize>( &mut self, message: OutgoingMessage<T> ) -> Result<(), Error>
Publish a message.
This method is a shorthand to dump and publish the message with a single call. If you want to print out the dump before or after publishing or assert it in tests consider using IntoPublishableDump::into_dump and publish_dump.
Arguments
message
– a boxed message of any type implementing Publishable trait.
Example
let props = OutgoingRequestProperties::new(
"system.ping",
Subscription::unicast_responses_from(to).subscription_topic(agent.id(), "v1")?,
"random-string-123",
OutgoingShortTermTimingProperties::new(Utc::now()),
);
let message = OutgoingMessage::new(
json!({ "ping": "hello" }),
props,
Destination::Unicast(agent.id().clone(), "v1"),
);
agent.publish(message)?;
sourcepub fn publish_publishable(
&mut self,
message: Box<dyn IntoPublishableMessage>
) -> Result<(), Error>
pub fn publish_publishable( &mut self, message: Box<dyn IntoPublishableMessage> ) -> Result<(), Error>
Publish a publishable message.
Arguments
message
– message to publish.
Example
let props = OutgoingRequestProperties::new(
"system.ping",
Subscription::unicast_responses_from(to).subscription_topic(agent.id(), "v1")?,
"random-string-123",
OutgoingShortTermTimingProperties::new(Utc::now()),
);
let message = OutgoingMessage::new(
json!({ "ping": "hello" }),
props,
Destination::Unicast(agent.id().clone(), "v1"),
);
let msg = Box::new(message) as Box<dyn IntoPublishableMessage>;
agent.publish_publishable(msg.clone())?;
println!("Message published: {}", msg);
pub fn publish_dump(&mut self, dump: PublishableMessage) -> Result<(), Error>
sourcepub fn subscribe<S>(
&mut self,
subscription: &S,
qos: QoS,
maybe_group: Option<&SharedGroup>
) -> Result<(), Error>where
S: SubscriptionTopic,
pub fn subscribe<S>( &mut self, subscription: &S, qos: QoS, maybe_group: Option<&SharedGroup> ) -> Result<(), Error>where S: SubscriptionTopic,
Subscribe to a topic.
Note that the subscription is actually gets confirmed on receiving
AgentNotification::Suback
notification.
Arguments
subscription
– the Subscription.qos
– quality of service. See QoS for available values.maybe_group
– SharedGroup in case of multicast subscription.
Example
agent.subscribe(
&Subscription::multicast_requests(Some("v1")),
QoS::AtMostOnce,
Some(&group),
)?;
match rx.recv_timeout(Duration::from_secs(5)) {
Ok(AgentNotification::Suback(_)) => (),
Ok(other) => panic!("Expected to receive suback notification, got {:?}", other),
Err(err) => panic!("Failed to receive suback notification: {}", err),
}
sourcepub fn unsubscribe<S>(
&mut self,
subscription: &S,
maybe_group: Option<&SharedGroup>
) -> Result<(), Error>where
S: SubscriptionTopic,
pub fn unsubscribe<S>( &mut self, subscription: &S, maybe_group: Option<&SharedGroup> ) -> Result<(), Error>where S: SubscriptionTopic,
Unsubscribe from a topic.
Note that the unsubscribing is actually gets confirmed on receiving
AgentNotification::Unsuback
notification.
Arguments
subscription
– the Subscription.maybe_group
– SharedGroup in case of multicast subscription.
Example
agent.unsubscribe(
&Subscription::multicast_requests(Some("v1")),
Some(&group),
)?;
match rx.recv_timeout(Duration::from_secs(5)) {
Ok(AgentNotification::Unsuback(_)) => (),
Ok(other) => panic!("Expected to receive unsuback notification, got {:?}", other),
Err(err) => panic!("Failed to receive unsuback notification: {}", err),
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for Agent
impl Send for Agent
impl Sync for Agent
impl Unpin for Agent
impl UnwindSafe for Agent
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
Mutably borrows from an owned value. Read more