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

Implementing the MQTT 5 specific parts of the protocol #89

Open
wants to merge 221 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
221 commits
Select commit Hold shift + click to select a range
c305d9a
Implement encoding and decoding of properties
gausby Sep 12, 2018
67ffcfc
Update the disconnect package to MQTT 5
gausby Sep 12, 2018
a0e31b6
Add the auth package
gausby Sep 13, 2018
82d0376
Added a note about the allowed properties for each of the packages
gausby Sep 13, 2018
784b692
Encode and decode properties in publish packages
gausby Sep 13, 2018
16511dd
Prepare making non allowed properties protocol violations in publish
gausby Sep 14, 2018
edb635b
WIP restructure of the entire application to support MQTT 5
gausby Oct 3, 2018
4627abe
Update the protocol packages and their encoders/decoders to MQTT 5
gausby Oct 3, 2018
36fcd18
Change subscription collectable to last write wins
gausby Oct 3, 2018
975a01c
Support properties in the options list given to the publish command
gausby Oct 4, 2018
85d5fe6
Note that we need to handle error cases in unsubscribe now
gausby Oct 4, 2018
fa44c8e
Improve the error report when the scripted server receive unexpected
gausby Oct 4, 2018
e7b08b4
Make it possible to check membership of a topic in a subscription
gausby Oct 4, 2018
7bd597e
Update the finalizing of subscription exchange to reflect new world
gausby Oct 4, 2018
0dcfc4a
The handler sit on the connection_opts, not the opts
gausby Oct 4, 2018
e999d8b
A proper unsuback message contain at least one result
gausby Oct 4, 2018
0ec35f2
Update the subscription tests in connection to reflect the new world
gausby Oct 4, 2018
d3a357a
Make `Tortoise.publish_sync/4` work with the new return format
gausby Oct 6, 2018
2097178
Make the inflight test suite pass
gausby Oct 6, 2018
c14bf37
The atom :reason was returned instead of the `reason`
gausby Oct 6, 2018
56f34b5
Change the pipe await and pipe tests to use the new format
gausby Oct 6, 2018
e228270
Fix the tortoise_test.exs suite
gausby Oct 6, 2018
fe52fb6
Move the ping tests from the controller to the connection process
gausby Oct 6, 2018
0cf1b55
Make sure we close down all processes when we disconnect
gausby Oct 6, 2018
329b868
Remove test from controller_test that is covered in connection_test
gausby Oct 6, 2018
07255f2
Added more protocol violation tests
gausby Oct 6, 2018
df3c11c
Removing more of the old controller code
gausby Oct 6, 2018
d8a2782
The return format of ping requests is now shaped other results
gausby Oct 6, 2018
d9e7fe0
Test that the connection handler callbacks are called
gausby Oct 7, 2018
c9ed2be
Dispatch round-trip time to events when a ping response is returned
gausby Oct 7, 2018
5caf609
Minor clean up
gausby Oct 7, 2018
13417a6
Make :ignore or {:stop, reason} possible returns on handler init
gausby Oct 7, 2018
e699278
Clean controller_test up and move more into connection_test
gausby Oct 7, 2018
2ca95d7
Call the user defined callbacks on subscription changes
gausby Oct 7, 2018
fd89114
Make sure the connection callback is called on connection
gausby Oct 7, 2018
91384bd
Add modules from test/support to load path when running in test
gausby Oct 7, 2018
f7c034b
Get rid of the old controller code and out comment the tests for it
gausby Oct 7, 2018
e359fe4
Make sure the receiver is up before trying to connect
gausby Oct 10, 2018
458c8eb
Start implementing disconnect messages and handle_disconnect
gausby Oct 10, 2018
d736426
Reintroduce the keep alive ping interval
gausby Oct 11, 2018
54a8a44
Re-do the ping/keep-alive logic
gausby Oct 13, 2018
5be84bc
Clean up unused variable warnings
gausby Oct 13, 2018
066bb5b
No need for the status event after we started monitoring receiver
gausby Oct 13, 2018
8f69784
Remove unused subscribe_all/1 from Tortoise.Connection
gausby Oct 13, 2018
ae1a491
Reintroduce incremental backoff on reconnects
gausby Oct 13, 2018
6eb8bef
Replace the ping/keep-alive logic with a stage-timeout approach
gausby Oct 14, 2018
d193111
Remove some dead code
gausby Oct 14, 2018
c8a686c
Extract the handle init callback to its own function
gausby Oct 14, 2018
644bfe5
Extract the execute terminate to execute_terminate/2
gausby Oct 14, 2018
b1878e9
Extract the connection callback handler into handle_connection
gausby Oct 14, 2018
e3e3ac9
Clean up the typespec of Handler.execute after refactoring
gausby Oct 14, 2018
8017cb5
Extract unsubscribe handling from Handler.execute
gausby Oct 17, 2018
ac84183
Extract the handle subscribe from the user defined callback
gausby Oct 17, 2018
780c527
Extract handle_message from the old Handle.execute function
gausby Oct 17, 2018
0baeaae
Have the callbacks for publish handled in one place
gausby Oct 22, 2018
f10b080
Add a handle_puback callback
gausby Oct 25, 2018
528eec6
MASSIVE BREAKING CHANGE handle_message is now called handle_publish
gausby Oct 25, 2018
fa7e018
Add and expose a handle_pubrel callback
gausby Oct 25, 2018
fcb2609
Add and expose a handle_pubrec callback
gausby Oct 25, 2018
6bddacd
Introduce and expose a handle_pubcomp callback
gausby Oct 25, 2018
f7b200b
Support user defined properties for protocol packages
gausby Nov 4, 2018
997c416
Get rid of unused variables
gausby Nov 4, 2018
e3acdf2
Started working on adding suback to the user defined callbacks
gausby Nov 10, 2018
dd1a7a5
Refactor the reply to pid handler code
gausby Nov 17, 2018
a52df8e
Use the reply-event to handle reply from unsubscribe operations
gausby Nov 17, 2018
783a915
Revamped the subscription/unsubscribe logic
gausby Jan 9, 2019
831cccf
Added some QoS=2 related tests
gausby Jan 9, 2019
0695c35
Add todo notes for missing tests
gausby Jan 9, 2019
c299dcc
Remove the old connection controller module
gausby Jan 9, 2019
e92f5b8
Add a missing implementation for handle_unsuback/3
Jan 10, 2019
1e4fd51
Add notes about what to implement as callbacks in the handler
Jan 10, 2019
f69e5df
Ignore my personal todo notes
Jan 10, 2019
797d7ce
mix format
Jan 10, 2019
30840b9
Rename execute_disconnect -> handle_execute_disconnect
gausby Jan 12, 2019
d4aedb0
Introduce a handle_connack
gausby Jan 12, 2019
864f942
The handle_publish callback will now receive the publish as 2nd arg
gausby Jan 12, 2019
b4f130a
Removed the subscriptions field from the internal state
gausby Jan 27, 2019
9f83fd3
Rename the :active_subscriptions to :subscriptions in connection
gausby Jan 27, 2019
1c12d90
Remove a section that was previously commented out
gausby Jan 27, 2019
6a0eb27
Remove a resolved todo item
gausby Feb 2, 2019
dbf125a
Make sure async unsubscribes result in a msg passed to the caller
gausby Feb 2, 2019
050f144
Make it possible to transform packages in user initialized publishes
gausby Feb 7, 2019
8720081
Make it possible to define pubrel and pubcomp for incoming flows
gausby Feb 7, 2019
524c6e6
Allow a list of properties as continuation value in user handler
gausby Feb 7, 2019
847cdf9
Changed the pubcomp handler to use the :cont interface as well
gausby Feb 7, 2019
06a5fda
Validate next actions and pass them on to publishes
gausby Feb 7, 2019
042eef4
Use a returned a list of values for publishes as user defined props
gausby Feb 8, 2019
3d28350
Update the callback for handle_unsuback
gausby Feb 8, 2019
405ddfb
Support the new callback style for handle_suback
gausby Feb 8, 2019
3a22285
Update handle_puback to the new callback return expression
gausby Feb 8, 2019
1927a38
Support more than just empty lists as next actions for pubcomp
gausby Feb 8, 2019
bee3d68
Updated handle_connack return expression to the new format
gausby Feb 8, 2019
c2cb8fe
Update the connection callback to follow the new format
gausby Feb 8, 2019
9d96907
Get rid of the now unused handle_result helper in handler.ex
gausby Feb 8, 2019
a1f8210
Update the disconnect callback to follow the new style
gausby Feb 8, 2019
c295683
Make the handler tests a bit more flexible for the publish tests
gausby Feb 9, 2019
3e1eb8a
Make sure we only accept arity 2 functions in the test handler
gausby Feb 9, 2019
add5c82
Abstracted the response in the test handler callback module
gausby Feb 9, 2019
ccc0bb4
Make the connection callback tests more extendable
gausby Feb 9, 2019
1025a86
Test more aspects of pubrel callback handling
gausby Feb 9, 2019
a996e61
Add more tests for the pubcomp callback handler
gausby Feb 9, 2019
996ed26
Add more tests to the pubrel callback handler
gausby Feb 9, 2019
56d47bc
Correct the names of some tests
gausby Feb 9, 2019
c536b62
Test that handle_connack return next_actions
gausby Feb 9, 2019
2e6fcfb
Update handle_publish callback handler tests
gausby Feb 9, 2019
c4a5f2d
Update the tests for the handle_suback callback handler
gausby Feb 9, 2019
4b10251
More tests for the unsuback callback handler
gausby Feb 9, 2019
8084fd4
Move terminate to the bottom of the file
gausby Feb 9, 2019
4c17cfc
Corrected the names of two tests
gausby Feb 9, 2019
c1fe1fa
Update tests for the handle_puback callback
gausby Feb 9, 2019
301a73b
Update handle_pubrec callback tests
gausby Feb 9, 2019
ff9845e
Update the handle_pubrel callback tests
gausby Feb 9, 2019
a907f70
Update the pubcomp tests, ran code formatter
gausby Feb 9, 2019
7b515bc
Update the handle_disconnect callback tests
gausby Feb 9, 2019
5a212ac
Make sure we can return next actions when receiving a disconnect
gausby Feb 10, 2019
7ad34b5
Remove the need for a magic pid variable in the callback TestHander
gausby Feb 10, 2019
3638adf
Allow user defined properties for unsubscribe packages
gausby Feb 10, 2019
80ad257
Make it possible to assign user properties on subscribe packages
gausby Feb 10, 2019
6bf1b13
The main connection init call nolonger rely on an 5-tuple
gausby Feb 10, 2019
e560921
Fix the return expression of a couple of default callbacks
gausby Mar 8, 2019
700d979
Remove a stray IO.inspect/2
Apr 29, 2019
d4e6750
Implement next actions for handle_connack, suback, and unsuback
Apr 29, 2019
7a5c6b9
Increased the timeout on a assert_receive
May 2, 2019
b3bdb60
Introduce a eval-next action
May 3, 2019
0ce143f
Make it possible to customize all callbacks in the TestHandler
May 3, 2019
5271e77
Test next actions generated by user defined callbacks
May 3, 2019
5ada5db
Support user defined next actions for the connection callback
May 3, 2019
1f41e09
mix format
May 3, 2019
c51bf47
Made it possible to customize the callbacks in the disconnect tests
May 3, 2019
4dacd97
Support next actions in the handle_disconnect callback
May 4, 2019
072f114
Handle next actions in callback handler for QoS=0 publish
May 4, 2019
9527aa9
Change the format of user properties
gausby May 24, 2019
fa29f5e
Merge branch 'mqtt-5' of github.com:gausby/tortoise into mqtt-5
gausby May 24, 2019
98a534f
Need to trap exits in a test process
gausby May 24, 2019
14bde6c
Update the Tortoise.Handler.Logger to follow the new behaviour
gausby May 26, 2019
6948c86
Renamed the connection/2 to status_change/2
May 29, 2019
9a85a94
Made the handle_pub{ack|rec|rel|comp} callbacks optional
May 29, 2019
80f3508
Make the terminate/2 callback on the Tortoise.Handler optional
May 29, 2019
ac392d2
Ran mix format
May 29, 2019
ac83845
No need to send an empty next action list for default handle publish
May 29, 2019
bdaa16e
Remove some old comments no longer needed
May 29, 2019
2dd211c
Allow the a `{:error, reason}`- return in the Publish QoS=0 handler
May 31, 2019
d1ac78d
Allow a topic alias instead of a topic in Tortoise.publish(_sync)/4
gausby Jun 2, 2019
d40f17c
Don't use the using Tortoise.Handler macro for the TestHandler
gausby Jun 2, 2019
d8ac3c5
Made it possible to specify properties and reasons on disconnect
gausby Jun 2, 2019
8b0380e
Update the client subscription state when unsubscribing from topics
Jan 13, 2020
c719fb4
Don't run handle_puback if it is not defined in the callback handler
Jan 13, 2020
2bc696c
Pass on next actions for all of the callback handlers
Jan 14, 2020
d833e62
Add config struct that contains the server config for a connection
Jan 16, 2020
80390b9
Have the handle_connack callback decide for all connack packages
Jan 17, 2020
eaa58a4
Store the server set keep alive interval if present in connection
Jan 17, 2020
4389a7f
It is 268_435_455
Jan 17, 2020
e4c94fc
Package properties now uses booleans for values where it makes sense
Jan 17, 2020
f5be274
Simplified merging of properties into connection config
Jan 17, 2020
18331bc
Use the server set keep alive interval for keep alive (if set)
Jan 18, 2020
d618bf9
Handle empty subscription topic filter list by sending err to caller
Jan 18, 2020
ffc3846
Don't allow shared subscriptions if the server doesn't allow them
Jan 20, 2020
c98a6ab
Guard against using wildcard if the server doesn't support them
Jan 20, 2020
bd0fdc5
Guard against subscription identifiers if the server disallow them
Jan 20, 2020
33d42a3
Rename config to info, and store capabilities under capabilities
Jan 21, 2020
9d9277d
Get rid of unused config.ex file
Jan 21, 2020
1cdd80b
Store connection subscription info in the info data structure
Jan 21, 2020
6407eb4
Out comment an unused module attribute
Jan 21, 2020
7deaa73
Do not enforce client ids on connect packages
Jan 21, 2020
6783be9
Introduce a session_ref and make the receiver work with it
Jan 21, 2020
45158aa
Update gen_state_machine dependency
Jan 21, 2020
b412b02
Tortoise.Registry.delete_meta/1 was only used in a test
Jan 21, 2020
fef117b
Rename Tortoise.App to Tortoise.Application
Jan 22, 2020
8290f47
Dead code: The public interface for handle socket had been removed
Feb 11, 2020
8ed618c
First step in getting rid of the Tortoise.Events
Feb 11, 2020
4899c00
Make pings work without the need for a client id
Feb 12, 2020
4785305
Subscribe and unsubscribe now works without the client id
Feb 12, 2020
045ab80
Get rid of the status events from Tortoise.Events
Feb 12, 2020
7cf8f45
Get rid of the ping response event type
Feb 12, 2020
0fbd4d9
Got rid of the Events and EventsTest modules
Feb 12, 2020
5d9d0c6
Update project lock file
May 9, 2020
d820c10
Add a Nix Shell file with a known good Elixir version
May 9, 2020
97a6c5a
Connection will be referenced by their pid, not client_id
May 9, 2020
487cbb6
Get rid of EQC Mini, add StreamData as a dependency
May 9, 2020
151b053
Comment out a call to connection via_name
May 9, 2020
61682e9
Implemented a generator for connack messages
May 11, 2020
b99e3b9
Add a StreamData generator for Pubrel packages
May 11, 2020
ae824b3
Add a StreamData generator for Puback packages
May 11, 2020
3ba0800
Add a StreamData generator for Pubcomp packages
May 11, 2020
e582096
Add a StreamData generator for Pubrec packages
May 11, 2020
ed72bf5
Add a StreamData generator for Disconnect packages
May 11, 2020
2ade95d
Add a StreamData generator for Pingreq packages
May 11, 2020
d34219b
Add a StreamData generator for Pingresp packages
May 11, 2020
26a64ad
Use a frequency generator to make property generator fail less often
May 13, 2020
0ef9073
Add a data generator for suback messages and a prop test for them
May 14, 2020
f6ccf1c
Add a generator for unsubacks and test them
May 16, 2020
f6ebe70
Add a generator for topics and topic filters
May 18, 2020
9a8de24
Add a data generator for subscribe packages and prop test them
May 23, 2020
d5169b2
Add a data generator for unsubscribe packages and test them
May 23, 2020
17ab260
Add a auth packet generator and test them
May 24, 2020
0f9fe61
Add a data generator for Publish packets and test them
May 24, 2020
ed44811
Publish payload is a binary
May 24, 2020
d8e577e
Generate all the available user properties on publish packages
May 24, 2020
10f3966
Make the Publish generator generate a nil as a payload now and then
May 24, 2020
09aa3e9
Make the publish generator a bit more customizable
May 25, 2020
26fbdac
Add a todo comment to the publish packet generator
May 25, 2020
af397d0
Add a data generator for Connect packets and prop test them
May 25, 2020
1d96fc3
Remove some cruft from the nix shell file
gausby Jul 9, 2020
900bbfc
Get rid of the Tortoise.Events registry
Sep 1, 2020
a039df7
Replace the inflight system with an ETS based one
Sep 17, 2020
58109b4
Always pass the session data back from the session callback
Sep 17, 2020
9463ce6
Make sure we release the id in the session state on outgoing pubcomp
Sep 18, 2020
5378cec
Make the Tortoise.publish function work with the connection pid
Sep 22, 2020
8db7f12
Make the public interface use the pid as identity for a connection
Sep 23, 2020
78a5855
Don't start a inflight process for a connection
Sep 23, 2020
e0a48ff
Reply directly to a ping request if the connection is not up yet
Sep 23, 2020
1b62650
Remember to update the session state when progressing the state
Sep 23, 2020
8acf78e
Narrow the allowed success reasons when processing a pubrec
Sep 28, 2020
115cfae
Release the package id if pubrec returned failure and has no cb
Sep 28, 2020
5820e8d
Code style change
Oct 13, 2020
592debd
Restructure the transmitter supervision for the connections
Oct 15, 2020
cd03ac9
Clean up the old inflight tracking
Oct 16, 2020
86bcda3
Ignore code that reference a now undefined function
Oct 22, 2020
c743300
Add an option list to Package.encode, default empty list
Oct 22, 2020
30fec0d
Add an option list to Package.decode, default empty list
Oct 22, 2020
dfb3e3e
Simplify the nix shell
gausby Mar 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eval "$(lorri direnv)"
3 changes: 2 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Used by "mix format"
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"],
import_deps: [:stream_data]
]
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ tortoise-*.tar
# Files generated by Erlang/Elixir QuickCheck
/current_counterexample.eqc
/.eqc-info

/todo.org
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Tortoise.Handler.Example do
{:ok, args}
end

def connection(status, state) do
def status_change(status, state) do
# `status` will be either `:up` or `:down`; you can use this to
# inform the rest of your system if the connection is currently
# open or closed; tortoise should be busy reconnecting if you get
Expand Down
117 changes: 85 additions & 32 deletions lib/tortoise.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ defmodule Tortoise do

alias Tortoise.Package
alias Tortoise.Connection
alias Tortoise.Connection.Inflight

@typedoc """
An identifier used to identify the client on the server.
Expand Down Expand Up @@ -147,6 +146,12 @@ defmodule Tortoise do
"""
@type topic() :: String.t()

# todo, documentation
@type topic_alias :: 0x0001..0xFFFF

# todo, documentation
@type topic_or_topic_alias() :: topic() | topic_alias()

@typedoc """
A topic filter for a subscription.

Expand Down Expand Up @@ -246,35 +251,59 @@ defmodule Tortoise do
with `Tortoise` so it is easy to see where the message originated
from.
"""
@spec publish(client_id(), topic(), payload, [options]) ::
:ok | {:ok, reference()} | {:error, :unknown_connection}
@spec publish(Process.dest(), topic_or_topic_alias(), payload, [options]) ::
:ok | {:ok, reference()} | {:error, reason}
when payload: binary() | nil,
options:
{:qos, qos()}
| {:retain, boolean()}
| {:identifier, package_identifier()}
def publish(client_id, topic, payload \\ nil, opts \\ []) do
| {:identifier, package_identifier()},
reason: :unknown_connection | :topic_alias_specified_twice
def publish(name_or_pid, topic, payload \\ nil, opts \\ [])

def publish(name_or_pid, topic, payload, opts) when is_binary(topic) do
{opts, properties} = Keyword.split(opts, [:retain, :qos, :transforms])
qos = Keyword.get(opts, :qos, 0)

publish = %Package.Publish{
topic: topic,
qos: qos,
payload: payload,
retain: Keyword.get(opts, :retain, false)
retain: Keyword.get(opts, :retain, false),
properties: properties
}

with {:ok, {transport, socket}} <- Connection.connection(client_id) do
case publish do
%Package.Publish{qos: 0} ->
case publish do
%Package.Publish{qos: 0} ->
with {:ok, {transport, socket}} <- Connection.connection(name_or_pid) do
encoded_publish = Package.encode(publish)
apply(transport, :send, [socket, encoded_publish])
else
{:error, :unknown_connection} ->
{:error, :unknown_connection}
end

%Package.Publish{qos: qos} when qos in [1, 2] ->
# transforms = Keyword.get(opts, :transforms, {[], nil})
Connection.publish(name_or_pid, publish)
end
end

%Package.Publish{qos: qos} when qos in [1, 2] ->
Inflight.track(client_id, {:outgoing, publish})
end
else
{:error, :unknown_connection} ->
{:error, :unknown_connection}
# Support passing in a topic alias instead of a proper topic, in
# this case we will lift the topic alias into a topic_alias value in
# the property list, set the topic to an empty string and pass it on
# to the regular `publish/4`
def publish(client_id, topic_alias, payload, opts)
when is_integer(topic_alias) and topic_alias > 0 do
case Keyword.get(opts, :topic_alias) do
nil ->
publish(client_id, "", payload, Keyword.put(opts, :topic_alias, topic_alias))

^topic_alias ->
publish(client_id, "", payload, opts)

_otherwise ->
{:error, :topic_alias_specified_twice}
end
end

Expand Down Expand Up @@ -304,37 +333,61 @@ defmodule Tortoise do

See the documentation for `Tortoise.publish/4` for configuration.
"""
@spec publish_sync(client_id(), topic(), payload, [options]) ::
:ok | {:error, :unknown_connection}
@spec publish_sync(Process.dest(), topic_or_topic_alias(), payload, [options]) ::
:ok | {:error, reason}
when payload: binary() | nil,
options:
{:qos, qos()}
| {:retain, boolean()}
| {:identifier, package_identifier()}
| {:timeout, timeout()}
def publish_sync(client_id, topic, payload \\ nil, opts \\ []) do
timeout = Keyword.get(opts, :timeout, :infinity)
| {:timeout, timeout()},
reason: :unknown_connection | :topic_alias_specified_twice
def publish_sync(pid_or_name, topic, payload \\ nil, opts \\ [])

def publish_sync(pid_or_name, topic, payload, opts) when is_binary(topic) do
{opts, properties} = Keyword.split(opts, [:retain, :qos, :transforms, :timeout])
qos = Keyword.get(opts, :qos, 0)

publish = %Package.Publish{
topic: topic,
qos: qos,
payload: payload,
retain: Keyword.get(opts, :retain, false)
retain: Keyword.get(opts, :retain, false),
properties: properties
}

with {:ok, {transport, socket}} <- Connection.connection(client_id) do
case publish do
%Package.Publish{qos: 0} ->
encoded_publish = Package.encode(publish)
apply(transport, :send, [socket, encoded_publish])
case publish do
# %Package.Publish{qos: 0} ->
# with {:ok, {transport, socket}} <- Connection.connection(client_id) do
# encoded_publish = Package.encode(publish)
# apply(transport, :send, [socket, encoded_publish])
# else
# {:error, :unknown_connection} ->
# {:error, :unknown_connection}
# end

%Package.Publish{qos: qos} when qos in [1, 2] ->
# transforms = Keyword.get(opts, :transforms, {[], nil})
timeout = Keyword.get(opts, :timeout, :infinity)
Tortoise.Connection.publish_sync(pid_or_name, publish, timeout)
end
end

%Package.Publish{qos: qos} when qos in [1, 2] ->
Inflight.track_sync(client_id, {:outgoing, publish}, timeout)
end
else
{:error, :unknown_connection} ->
{:error, :unknown_connection}
# Support passing in a topic alias instead of a proper topic, in
# this case we will lift the topic alias into a topic_alias value in
# the property list, set the topic to an empty string and pass it on
# to the regular `publish_sync/4`
def publish_sync(client_id, topic_alias, payload, opts)
when is_integer(topic_alias) and topic_alias > 0 do
case Keyword.get(opts, :topic_alias) do
nil ->
publish(client_id, "", payload, Keyword.put(opts, :topic_alias, topic_alias))

^topic_alias ->
publish(client_id, "", payload, opts)

_otherwise ->
{:error, :topic_alias_specified_twice}
end
end
end
5 changes: 3 additions & 2 deletions lib/tortoise/app.ex → lib/tortoise/application.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Tortoise.App do
defmodule Tortoise.Application do
@moduledoc false

use Application
Expand All @@ -10,7 +10,8 @@ defmodule Tortoise.App do

children = [
{Registry, [keys: :unique, name: Tortoise.Registry]},
{Registry, [keys: :duplicate, name: Tortoise.Events]},
{Tortoise.TransmitterSupervisor, []},
{Tortoise.Session, [backend: Tortoise.Session.Ets]},
{Tortoise.Supervisor, [strategy: :one_for_one]}
]

Expand Down
Loading