Skip to content

Conversation

@aggarw13
Copy link

@aggarw13 aggarw13 commented Aug 5, 2020

MQTT demo for using subscription manager when sharing MQTT connection among multiple subscription topics

Description of changes:

  • Add demo to showcase use of subscription manager for registering subscription callbacks in a single threaded demo
  • Relocate subscription manager within the demo based on new decision about providing subscription manager specific references to demos (instead of a generic component utility that applies for all demos)
  • Remove all topic matching functions from subscription manager, and instead update it to use the MQTT_MatchTopic function (that is added in [PR from CLI tool] Add utility for MQTT topic filter and topic matching to library #1119)

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

@aggarw13 aggarw13 changed the title Test PR from CLI tool [PR from CLI tool] Add MQTT demo for connection sharing with subscription manager Aug 5, 2020
@codecov-commenter
Copy link

codecov-commenter commented Aug 5, 2020

Codecov Report

Merging #1098 into development will increase coverage by 2.21%.
The diff coverage is 100.00%.

Impacted file tree graph

@@               Coverage Diff               @@
##           development    #1098      +/-   ##
===============================================
+ Coverage        96.54%   98.76%   +2.21%     
===============================================
  Files                9        4       -5     
  Lines             5643     1371    -4272     
  Branches           641      418     -223     
===============================================
- Hits              5448     1354    -4094     
+ Misses               9        0       -9     
+ Partials           186       17     -169     
Impacted Files Coverage Δ
libraries/standard/mqtt/src/mqtt.c 99.10% <ø> (+4.69%) ⬆️
libraries/standard/mqtt/src/mqtt_lightweight.c 97.42% <ø> (+2.90%) ⬆️
libraries/standard/mqtt/src/mqtt_state.c 98.46% <ø> (+2.52%) ⬆️
libraries/standard/http/src/http_client.c 100.00% <100.00%> (+12.51%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1aed50a...6be56c9. Read the comment docs.

Archit Aggarwal and others added 18 commits August 5, 2020 22:02
Co-authored-by: Muneeb Ahmed <54290492+muneebahmed10@users.noreply.github.com>
…t-device-sdk-embedded-C into add-subscription-manager
Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com>
Co-authored-by: Gaurav-Aggarwal-AWS <33462878+aggarg@users.noreply.github.com>

/*-----------------------------------------------------------*/

static int publishToTopic( MQTTContext_t * pMqttContext,
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this function now not only publishes to a topic, but receives publishes from the broker, its name should be updated

Copy link
Author

Choose a reason for hiding this comment

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

This function is only used for sending publishes to broker, and not for receiving incoming PUBLISHes

Copy link
Contributor

Choose a reason for hiding this comment

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

On line 1186, you use this function for receiving incoming publishes with the call to MQTT_ProcessLoop(). Otherwise, the check on line 1283 for receiving the temperature data would fail.

Copy link
Author

Choose a reason for hiding this comment

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

Ahh, okay good point! Have renamed the function to publishToTopicAndProcessIncomingMessage


/* Process Incoming UNSUBACK packet from the broker. */
mqttStatus = MQTT_ProcessLoop( pMqttContext, MQTT_PROCESS_LOOP_TIMEOUT_MS );

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be useful to have a global receivedUnsuback flag? If we do, we don't have to wait the entire timeout and can just wait until we receive the unsuback. If not, I don't really see why this function needs to call the process loop. Since we have three consecutive unsubscribeFromTopic calls, It would save 2 * MQTT_PROCESS_LOOP_TIMEOUT_MS milliseconds If we only called the process loop once after all three calls.

Copy link
Author

Choose a reason for hiding this comment

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

I can instead unsubscribe from all topic filters in the same UNSUBSCRIBE request

Copy link
Author

Choose a reason for hiding this comment

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

Have updated the private function to take in a list of topic filters to unsubscribe with a single packet.

@aggarw13 aggarw13 requested a review from muneebahmed10 August 18, 2020 22:16
* publish to.
*
* The topic name starts with the client identifier to ensure that each demo
* interacts with a unique topic name.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a little confused by this, so maybe this comment could have more clarification. Why does each demo need a unique topic name, and how does the client identifier make it unique? If it's for the case of a customer choosing to use a public broker like test.mosquitto.org instead of a local broker, we could mention that's the reason.

Copy link
Author

Choose a reason for hiding this comment

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

It's a copy-pasta from other demos. Now, that we have moved away from using the public broker, I agree that we can avoid this comment

Copy link
Author

@aggarw13 aggarw13 Aug 20, 2020

Choose a reason for hiding this comment

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

I prefer that we don't mention about the test.mosquitto.org due to its reliability concerns.

}

/* Copy the available index into the output param. */
*pIndex = index;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: would it make more sense to only set this on success?

Copy link
Author

@aggarw13 aggarw13 Aug 20, 2020

Choose a reason for hiding this comment

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

Made the improvement in the basic TLS demo (as have removed the feature of publish resend from this demo)


/* Sends an MQTT Connect packet using the established TLS session,
* then waits for connection acknowledgment (CONNACK) packet. */
returnStatus = establishMqttSession( &mqttContext, pNetworkContext, false, &sessionPresent );
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand if this was a requirement for this demo, but if it was only a design choice, does this demo need non-clean sessions? I feel like connection sharing and resending of publishes are separate features, and if we already show it in the TLS and mutual auth demos, then we don't need to show it again here. It would reduce the amount of code that's duplicated in multiple places.

Copy link
Author

Choose a reason for hiding this comment

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

That's a good suggestion in terms of reducing the number of lines of code in this demo. I have removed the session restore feature (of resending publish messages) from this demo.

/* Wait for acknowledgement packet (SUBACK) from the broker in response to the
* subscribe request. */
mqttStatus = MQTT_ProcessLoop( pMqttContext, MQTT_PROCESS_LOOP_TIMEOUT_MS );

Copy link
Contributor

Choose a reason for hiding this comment

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

If the intention here is to make sure the SUBACK was received, we could add a flag that's set in the event callback on SUBACK reception. Just a thought, I don't expect it to be changed for this PR.

Copy link
Author

Choose a reason for hiding this comment

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

If the suggestion is to call ProcessLoop avoid the large timeout value, we can consider that for a separate PR effort.

Archit Aggarwal and others added 3 commits August 20, 2020 09:44
@aggarw13 aggarw13 requested a review from muneebahmed10 August 20, 2020 17:17
muneebahmed10
muneebahmed10 previously approved these changes Aug 20, 2020
* @brief The MQTT message for the #DEMO_TEMPERATURE_HIGH_TOPIC topic published
* in this example.
*/
#define DEMO_TEMPERATURE_HIGH_MESSAGE "Today's High is 80 degree F"
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should use this opportunity to push Kelvin as the superior unit of measurement

Copy link
Author

Choose a reason for hiding this comment

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

Interesting point (to make the demo universally appealing) 😆

gkwicker
gkwicker previously approved these changes Aug 20, 2020
Copy link
Contributor

@gkwicker gkwicker left a comment

Choose a reason for hiding this comment

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

Approved, see comments. It looks like some of this demo code could be refactored out into a helpful library which many applications could use; not a blocker but something to think about.

* when a MQTT connection is established with a session that was already
* present. All the outgoing publish messages waiting to receive PUBACK
* are resent in this demo. In order to support retransmission all the outgoing
* publishes are stored until a PUBACK is received.
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I've missed something retransmission won't work across reboots, so you may want to mention this in the comments.

Copy link
Author

Choose a reason for hiding this comment

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

I have removed the text about re-transmission as I removed all the re-transmission related code from this demo to avoid code duplication across our demo files

@aggarw13 aggarw13 dismissed stale reviews from gkwicker and muneebahmed10 via 6be56c9 August 20, 2020 21:51
@aggarw13 aggarw13 merged commit a66e664 into aws:development Aug 20, 2020
@aggarw13 aggarw13 deleted the demo/mqtt-connection-sharing branch August 20, 2020 22:53
leegeth pushed a commit to leegeth/aws-iot-device-sdk-embedded-C that referenced this pull request Aug 27, 2020
leegeth pushed a commit to leegeth/aws-iot-device-sdk-embedded-C that referenced this pull request Aug 28, 2020
leegeth pushed a commit to leegeth/aws-iot-device-sdk-embedded-C that referenced this pull request Aug 31, 2020
leegeth pushed a commit to leegeth/aws-iot-device-sdk-embedded-C that referenced this pull request Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants