Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PublishCorrelated Request/Outgoing event to associate a Publish… #291

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions rumqttc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ pub use tokio_rustls::rustls::internal::pemfile::{certs, pkcs8_private_keys, rsa
pub use tokio_rustls::rustls::ClientConfig;

pub type Incoming = Packet;
pub type CorrelationId = usize;

/// Current outgoing activity on the eventloop
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Outgoing {
/// Publish packet with packet identifier. 0 implies QoS 0
Publish(u16),
/// Same as `Publish`, but includes a correlation id. Triggered by `Request::PublishCorrelated`.
PublishCorrelated(u16, CorrelationId),
/// Subscribe packet with packet identifier
Subscribe(u16),
/// Unsubscribe packet with packet identifier
Expand Down Expand Up @@ -152,6 +155,10 @@ pub enum Outgoing {
#[derive(Clone, Debug, PartialEq)]
pub enum Request {
Publish(Publish),
/// `PublishCorrelated` allows to correlate a `Publish` request with an id choosen by the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// `PublishCorrelated` allows to correlate a `Publish` request with an id choosen by the
/// `PublishCorrelated` allows correlating a `Publish` request with an id chosen by the

/// client. This serves the purpose to correlate a published message with the `pkgid`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// client. This serves the purpose to correlate a published message with the `pkgid`
/// client. This serves the purpose to correlate a published message with the `pkid`

/// that it got assigned.
PublishCorrelated(Publish, CorrelationId),
PubAck(PubAck),
PubRec(PubRec),
PubComp(PubComp),
Expand Down
60 changes: 55 additions & 5 deletions rumqttc/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Event, Incoming, Outgoing, Request};
use crate::{CorrelationId, Event, Incoming, Outgoing, Request};

use bytes::BytesMut;
use mqttbytes::v4::*;
Expand Down Expand Up @@ -140,6 +140,9 @@ impl MqttState {
pub fn handle_outgoing_packet(&mut self, request: Request) -> Result<(), StateError> {
match request {
Request::Publish(publish) => self.outgoing_publish(publish)?,
Request::PublishCorrelated(publish, correlation_id) => {
self.outgoing_publish_correlated(publish, correlation_id)?
}
Request::PubRel(pubrel) => self.outgoing_pubrel(pubrel)?,
Request::Subscribe(subscribe) => self.outgoing_subscribe(subscribe)?,
Request::Unsubscribe(unsubscribe) => self.outgoing_unsubscribe(unsubscribe)?,
Expand Down Expand Up @@ -295,9 +298,25 @@ impl MqttState {
Ok(())
}

fn outgoing_publish(&mut self, publish: Publish) -> Result<(), StateError> {
self.outgoing_publish_maybe_correlated(publish, None)
}

fn outgoing_publish_correlated(
&mut self,
publish: Publish,
correlation_id: CorrelationId,
) -> Result<(), StateError> {
self.outgoing_publish_maybe_correlated(publish, Some(correlation_id))
}

/// Adds next packet identifier to QoS 1 and 2 publish packets and returns
/// it buy wrapping publish in packet
fn outgoing_publish(&mut self, mut publish: Publish) -> Result<(), StateError> {
fn outgoing_publish_maybe_correlated(
&mut self,
mut publish: Publish,
maybe_correlation_id: Option<CorrelationId>,
) -> Result<(), StateError> {
if publish.qos != QoS::AtMostOnce {
if publish.pkid == 0 {
publish.pkid = self.next_pkid();
Expand Down Expand Up @@ -331,8 +350,12 @@ impl MqttState {
);

publish.write(&mut self.write)?;
let event = Event::Outgoing(Outgoing::Publish(publish.pkid));
self.events.push_back(event);
let outgoing_event = match maybe_correlation_id {
Some(correlation_id) => Outgoing::PublishCorrelated(publish.pkid, correlation_id),

None => Outgoing::Publish(publish.pkid),
};
self.events.push_back(Event::Outgoing(outgoing_event));
Ok(())
}

Expand Down Expand Up @@ -468,7 +491,7 @@ impl MqttState {
#[cfg(test)]
mod test {
use super::{MqttState, StateError};
use crate::{Incoming, MqttOptions, Request};
use crate::{Event, Incoming, MqttOptions, Outgoing, Request};
use mqttbytes::v4::*;
use mqttbytes::*;

Expand Down Expand Up @@ -583,6 +606,33 @@ mod test {
}
}

#[test]
fn outgoing_publish_correlated_requests_should_emit_outgoing_publish_correlated_events() {
let mut mqtt = build_mqttstate();

// QoS0, 1, 2 Publishes
let publish1 = build_outgoing_publish(QoS::AtMostOnce);
let publish2 = build_outgoing_publish(QoS::AtLeastOnce);
let publish3 = build_outgoing_publish(QoS::ExactlyOnce);

mqtt.handle_outgoing_packet(Request::PublishCorrelated(publish1, 42))
.unwrap();
mqtt.handle_outgoing_packet(Request::Publish(publish2))
.unwrap();
mqtt.handle_outgoing_packet(Request::PublishCorrelated(publish3, 43))
.unwrap();

assert_eq!(
mqtt.events[0],
Event::Outgoing(Outgoing::PublishCorrelated(0u16, 42))
);
assert_eq!(mqtt.events[1], Event::Outgoing(Outgoing::Publish(1u16)));
assert_eq!(
mqtt.events[2],
Event::Outgoing(Outgoing::PublishCorrelated(2u16, 43))
);
}

#[test]
fn incoming_puback_should_remove_correct_publish_from_queue() {
let mut mqtt = build_mqttstate();
Expand Down