Skip to content

Commit

Permalink
AWSCRT updates (#1)
Browse files Browse the repository at this point in the history
* Add JuliaFormatter

* Patch version bump

* Fixes for AWSCRT extension

* Add vscode files to gitignore
  • Loading branch information
Octogonapus authored Jan 1, 2024
1 parent 74cdf13 commit 2e076af
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 38 deletions.
3 changes: 3 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
margin = 120
remove_extra_newlines = true
format_docstrings = true
47 changes: 47 additions & 0 deletions .github/workflows/JuliaFormatter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Code Formatting

on:
pull_request:
branches:
- main

jobs:
format:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write
actions: write

steps:
- name: Cancel Previous Runs
uses: styfle/[email protected]

- uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}

- uses: julia-actions/setup-julia@v1
with:
version: 1.9

- name: Install JuliaFormatter and format
shell: bash
run: julia -e 'import Pkg; Pkg.add("JuliaFormatter"); using JuliaFormatter; format(".")'

- name: Create Pull Request
id: pr
uses: peter-evans/create-pull-request@v3
with:
commit-message: Format files using JuliaFormatter
title: ${{ format('[AUTO] Format {0} using JuliaFormatter', github.event.pull_request.number) }}
body: ${{ format('[JuliaFormatter.jl](https://github.com/domluna/JuliaFormatter.jl) would suggest these formatting changes against \#{0}.', github.event.pull_request.number) }}
labels: no changelog
branch: ${{ format('code-format/{0}', github.event.pull_request.number) }}
delete-branch: true

- name: Fail if a PR was needed
if: ${{ steps.pr.outputs.pull-request-operation == 'created' || steps.pr.outputs.pull-request-operation == 'updated' }}
shell: bash
run: exit 1
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,15 @@
/Manifest.toml
/docs/Manifest.toml
/docs/build/

.vscode/*
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MQTT"
uuid = "ebefff21-3b8f-497c-a71a-d17835ab79ba"
authors = ["Nicholas Shindler <[email protected]> and contributors"]
version = "0.0.1-DEV"
version = "0.0.2-DEV"

[deps]

Expand Down
34 changes: 15 additions & 19 deletions ext/AWSCRTExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,31 @@ module AWSCRTExt
import AWSCRT, MQTT

struct MQTT.MQTTConnection <: MQTT.AbstractConnection
client::AWSCRT.MQTTClient
connection::AWSCRT.MQTTConnection
endpoint::String
port::Int
id::String
will::AWSCRT.Will

MQTT.MQTTConnection(client, port, id, will) = new(client, AWSCRT.MQTTConnection(client), port, id, will)
connect_kwargs::Dict{Symbol,Any}
end

MQTT.MQTTConnection(connection, endpoint, port, id; connect_kwargs = Dict()) =
MQTT.MQTTConnection(connection, endpoint, port, id, connect_kwargs)

function MQTT._resolve(async_object)
fetch(async_object)
end

function MQTT._connect(c::MQTT.MQTTConnection)
AWSCRT.connect(
c.connection,
ENV["ENDPOINT"],
c.port,
c.id,
c.will
)
return AWSCRT.connect(c.connection, c.endpoint, c.port, c.id; c.connect_kwargs...)
end

function MQTT._subscribe(callback, c::MQTT.MQTTConnection, topic, qos)
task, id = AWSCRT.subscribe(
c.connection,
topic,
qos = AWSCRT.aws_mqtt_qos(Int(qos)),
callback,
)
task, id = AWSCRT.subscribe(c.connection, topic, qos = AWSCRT.aws_mqtt_qos(Int(qos)), _adapt_on_message(callback))
return task
end

function MQTT._publish(c::MQTT.MQTTConnection, topic, payload, qos, retain)
task, id = AWSCRT.publish(c.connection, topic, payload, qos=AWSCRT.aws_mqtt_qos(Int(qos)), retain=retain)
task, id = AWSCRT.publish(c.connection, topic, payload, qos = AWSCRT.aws_mqtt_qos(Int(qos)), retain = retain)
return task
end

Expand All @@ -47,7 +37,13 @@ function MQTT._unsubscribe(c::MQTT.MQTTConnection, topic)
end

function MQTT._disconnect(c::MQTT.MQTTConnection)
AWSCRT.disconnect(c.connection)
return AWSCRT.disconnect(c.connection)
end

function _adapt_on_message(cb::MQTT.OnMessage)
return function _awscrt_on_message(topic, payload, dup, qos, retain)
return cb(topic, payload)
end
end

end # module
8 changes: 2 additions & 6 deletions ext/MQTTClientExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ function MQTT._connect(c::MQTT.MQTTConnection)
end

function MQTT._subscribe(callback, c::MQTT.MQTTConnection, topic, qos::MQTT.QOS)
MQTTClient.subscribe_async(c.client, topic, on_msg, qos=MQTTClient.QOS(UInt8(qos)))
MQTTClient.subscribe_async(c.client, topic, on_msg, qos = MQTTClient.QOS(UInt8(qos)))
end

function MQTT._publish(c::MQTT.MQTTConnection, topic, payload, qos::MQTT.QOS, retain)
publish_async(c.client,
topic,
payload,
qos=MQTTClient.QOS(UInt8(qos)),
retain = retain)
publish_async(c.client, topic, payload, qos = MQTTClient.QOS(UInt8(qos)), retain = retain)
end

function MQTT._unsubscribe(c::MQTT.MQTTConnection, topic)
Expand Down
38 changes: 26 additions & 12 deletions src/MQTT.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,51 @@ abstract type AbstractConnection end
An enum representing the different Quality of Service (QoS) levels in MQTT.
# Values
- `AT_MOST_ONCE`: QoS level 0, at most once delivery. The message is delivered at most once, or it may not be delivered at all. This is also known as "fire and forget".
- `AT_LEAST_ONCE`: QoS level 1, at least once delivery. The message is guaranteed to be delivered at least once, but it may be delivered multiple times.
- `EXACTLY_ONCE`: QoS level 2, exactly once delivery. The message is guaranteed to be delivered exactly once.
- `AT_MOST_ONCE`: QoS level 0, at most once delivery. The message is delivered at most once, or it may not be delivered at all. This is also known as "fire and forget".
- `AT_LEAST_ONCE`: QoS level 1, at least once delivery. The message is guaranteed to be delivered at least once, but it may be delivered multiple times.
- `EXACTLY_ONCE`: QoS level 2, exactly once delivery. The message is guaranteed to be delivered exactly once.
"""
@enum QOS::UInt8 AT_MOST_ONCE=0x00 AT_LEAST_ONCE=0x01 EXACTLY_ONCE=0x02
@enum QOS::UInt8 AT_MOST_ONCE = 0x00 AT_LEAST_ONCE = 0x01 EXACTLY_ONCE = 0x02

## -- Interfaces --

connect(c::AbstractConnection) = _connect(c)
"""
on_message(topic::String, payload::String)
connect!(c::AbstractConnection) = _resolve(_connect(c))
A callback invoked when a message is received.
Arguments:
subscribe(callback, connection::AbstractConnection, topic, qos::QOS) = _subscribe(callback, connection, topic, qos)
- `topic (String)`: The topic receiving the message.
- `payload (String)`: The payload of the message.
subscribe!(callback, connection::AbstractConnection, topic, qos::QOS) = _resolve(_subscribe(callback, connection, topic, qos))
Returns `nothing`.
"""
const OnMessage = Function

connect(c::AbstractConnection) = _connect(c)

publish(connection::AbstractConnection, topic, payload, qos::QOS; retain=false) = _publish(connection, topic, payload, qos, retain)
connect!(c::AbstractConnection) = _resolve(_connect(c))

subscribe(callback::OnMessage, connection::AbstractConnection, topic, qos::QOS) =
_subscribe(callback, connection, topic, qos)

subscribe!(callback::OnMessage, connection::AbstractConnection, topic, qos::QOS) =
_resolve(_subscribe(callback, connection, topic, qos))

publish!(connection::AbstractConnection, topic, payload, qos::QOS; retain=false) = _resolve(_publish(connection, topic, payload, qos, retain))
publish(connection::AbstractConnection, topic, payload, qos::QOS; retain = false) =
_publish(connection, topic, payload, qos, retain)

publish!(connection::AbstractConnection, topic, payload, qos::QOS; retain = false) =
_resolve(_publish(connection, topic, payload, qos, retain))

unsubscribe(connection::AbstractConnection, topic) = _unsubscribe(connection, topic)

unsubscribe!(connection::AbstractConnection, topic) = _resolve(_unsubscribe(connection, topic))


disconnect(connection::AbstractConnection) = _disconnect(connection)

disconnect!(connection::AbstractConnection) = _resolve(_disconnect(connection))


end

0 comments on commit 2e076af

Please sign in to comment.