Skip to content

Implement MQTT Message Publishing in MqttManager (CATROID-1673) - #5225

Open
Paras-ydv wants to merge 2 commits into
Catrobat:developfrom
Paras-ydv:CATROID-1673
Open

Paras-ydv wants to merge 2 commits into
Catrobat:developfrom
Paras-ydv:CATROID-1673

Conversation

@Paras-ydv

@Paras-ydv Paras-ydv commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Summary

Extends MqttManager to support publishing MQTT messages to a broker using Eclipse Paho.

This PR previously carried the dependency and settings groundwork (CATROID-1670, CATROID-1672) and the connection lifecycle (CATROID-1671) as extra commits, because none of them were on develop yet. All three have since been merged (#5220, #5222), so the branch has been rebased onto develop and those commits removed. What remains is a single commit containing only the publish work.

CATROID-1673 — Publish implementation

Changes

  • Added publish(topic, message) to MqttClientInterface and implemented it in PahoMqttClient
  • MqttManager.publish() validates the topic (rejects blank, rejects the # and + wildcards, which are only legal in subscriptions) and the QoS range (0–2)
  • Lazy-connects before publishing when no connection is active
  • publishFromContext() convenience wrapper for the brick layer
  • No forced unwraps; failures are logged and returned as false rather than thrown
  • connectionLost now passes the Throwable to Log.e so the stacktrace is kept

Tests

24 new tests covering publish and message building:

  • Successful publish, and that the correct topic and payload reach the client
  • QoS 0, 1 and 2 accepted; out-of-range QoS rejected
  • Retained flag set both ways
  • Blank topic rejected, and the client is not called
  • Wildcard topics rejected (# and +)
  • Lazy connect succeeds before publishing
  • No reconnect when a connection already exists
  • No publish when the lazy connect fails
  • Client exception returns false without crashing
  • buildMessage() sets payload, QoS and retained correctly

Two existing tests were updated in this rebase. MqttManager now takes an MqttClientFactory rather than a client instance, following the change made during the CATROID-1671 review, so tests that assumed an injected client had to connect first. Without that, testPublishWhenAlreadyConnectedDoesNotReconnect failed and testSubscribeAfterDisconnectSucceedsAgain did not compile.

Full suite: 4709 tests, 0 failures.

Acceptance criteria

  • publish() API added to MqttManager
  • publish() added to MqttClientInterface and PahoMqttClient
  • Messages published successfully to a connected broker
  • Lazy connection established automatically if none exists
  • Configurable QoS and retained flag
  • Failures logged without crashing
  • Unit tests covering the success paths, the validation rejections and the failure modes
  • All existing and new tests pass

Your Checklist

  • Include the name of the Jira ticket in the PR's title
  • Include a summary of the changes plus the relevant context
  • Choose the proper base branch (develop)
  • Confirm that the changes follow the project's coding guidelines
  • Verify that the changes generate no compiler or linter warnings
  • Perform a self-review of the changes
  • Verify to commit no other files than the intentionally changed ones
  • Include reasonable and readable tests verifying the added or changed behavior
  • Confirm that new and existing unit tests pass locally
  • Check that the commits' message style matches the project's guideline
  • Stick to the project's gitflow workflow
  • Verify that your changes do not have any conflicts with the base branch
  • After the PR, verify that all CI checks have passed
  • Post a message in the catroid-stage or catroid-ide Slack channel and ask for a code reviewer

@sonarqubecloud

sonarqubecloud Bot commented Jul 5, 2026

Copy link
Copy Markdown

- Add publish() to MqttClientInterface and PahoMqttClient
- Implement publish() in MqttManager with topic validation (blank,
  wildcards), QoS range check (0-2), and retained flag support
- Lazy-connect before publishing if no active connection exists
- Rename publishFromContext() to avoid Detekt MethodOverloading
- Log failures without crashing; no forced unwrap
- Add comprehensive unit tests: empty payload, QoS boundaries,
  retained flag, lazy connect, no reconnect when already connected,
  no publish when connect fails (51 tests total)

@harshsomankar123-tech harshsomankar123-tech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @Paras-ydv, Thanks! some blockers from my side. PTAL

Comment thread catroid/src/main/java/org/catrobat/catroid/devices/mqtt/MqttManager.kt Outdated
Comment thread catroid/src/main/java/org/catrobat/catroid/devices/mqtt/MqttClientInterface.kt Outdated
Comment thread catroid/src/test/java/org/catrobat/catroid/test/mqtt/MqttManagerTest.kt Outdated
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

GSoC-2026 This ticket is assigned to the GSoC contributor.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants