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

Extract easy commits out of cubq branch #71

Merged
merged 2 commits into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
configs: [
%{
name: "default",
strict: true,
checks: [
{Credo.Check.Readability.ImplTrue, tags: []},
{Credo.Check.Readability.LargeNumbers, only_greater_than: 86400},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, parens: true},
{Credo.Check.Readability.Specs, tags: []},
{Credo.Check.Design.TagTODO, false}
{Credo.Check.Design.TagTODO, false},
{Credo.Check.Readability.StrictModuleLayout, tags: []}
]
}
]
Expand Down
8 changes: 4 additions & 4 deletions lib/jackalope.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Jackalope do
use Supervisor

require Logger

@moduledoc "README.md"
|> File.read!()
|> String.split("<!-- MDOC !-->")
|> Enum.fetch!(1)

use Supervisor

require Logger

@default_mqtt_server {
Tortoise311.Transport.Tcp,
host: "localhost", port: 1883
Expand Down
4 changes: 2 additions & 2 deletions lib/jackalope/handler/logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ defmodule Jackalope.Handler.Logger do
specified in the option list passed to `Jackalope.start_link/1`.
"""

require Logger

@behaviour Jackalope.Handler

require Logger

@impl Jackalope.Handler
def connection(status) do
Logger.info("Connection status is: #{inspect(status)}")
Expand Down
6 changes: 3 additions & 3 deletions lib/jackalope/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ defmodule Jackalope.Session do

use GenServer

require Logger

alias __MODULE__, as: State
alias Jackalope.{TortoiseClient, WorkList}

require Logger

@publish_options [:qos, :retain]
@work_list_options [:ttl]
# One hour
Expand Down Expand Up @@ -202,7 +202,7 @@ defmodule Jackalope.Session do
# drop the message, it is outside of the time to live
if function_exported?(state.handler, :handle_error, 1) do
reason = {:publish_error, cmd, :ttl_expired}
apply(state.handler, :handle_error, [reason])
state.handler.handle_error(reason)
end

{:noreply, state, {:continue, :consume_work_list}}
Expand Down
4 changes: 3 additions & 1 deletion lib/jackalope/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ defmodule Jackalope.Supervisor do
# Supervisor for the MQTT connection specific processes

use Supervisor
require Logger

alias Jackalope.TortoiseClient

require Logger

@type init_arg ::
{:app_handler, module()} | {:client_id, atom()} | {:connection_options, Keyword.t()}

Expand Down
5 changes: 2 additions & 3 deletions lib/jackalope/tortoise_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ defmodule Jackalope.TortoiseClient do

use GenServer

require Logger

alias Jackalope.Session
require Logger

defmodule State do
@moduledoc false
Expand Down Expand Up @@ -213,7 +212,7 @@ defmodule Jackalope.TortoiseClient do
{:error, reason} ->
if function_exported?(state.handler, :handle_error, 1) do
reason = {:publish_error, {topic, payload, opts}, reason}
apply(state.handler, :handle_error, [reason])
state.handler.handle_error(reason)
end

{:error, reason}
Expand Down
14 changes: 7 additions & 7 deletions lib/jackalope/tortoise_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ defmodule Jackalope.TortoiseHandler do

@behaviour Tortoise311.Handler

require Logger

alias __MODULE__, as: State
alias Jackalope.Session

require Logger

defstruct handler: nil, default_last_will: nil

@impl Tortoise311.Handler
Expand All @@ -22,7 +22,7 @@ defmodule Jackalope.TortoiseHandler do

@impl Tortoise311.Handler
def last_will(%State{} = state) do
last_will = apply(state.handler, :last_will, []) || state.default_last_will
last_will = state.handler.last_will() || state.default_last_will
packaged_last_will = package_last_will(last_will)

{{:ok, packaged_last_will}, state}
Expand All @@ -34,7 +34,7 @@ defmodule Jackalope.TortoiseHandler do
Session.report_connection_status(status)

if function_exported?(state.handler, :connection, 1) do
_ignored = apply(state.handler, :connection, [status])
_ignored = state.handler.connection(status)
end

{:ok, state}
Expand All @@ -43,7 +43,7 @@ defmodule Jackalope.TortoiseHandler do
@impl Tortoise311.Handler
def subscription(status, topic_filter, %State{} = state) when status in [:up, :down] do
if function_exported?(state.handler, :subscription, 2) do
_ignored = apply(state.handler, :subscription, [status, topic_filter])
_ignored = state.handler.subscription(status, topic_filter)
end

{:ok, state}
Expand All @@ -54,15 +54,15 @@ defmodule Jackalope.TortoiseHandler do
case Jason.decode(payload_string) do
{:ok, payload} ->
# Dispatch to the handle message callback on the jackalope handler
apply(handler, :handle_message, [topic_levels, payload])
handler.handle_message(topic_levels, payload)
{:ok, state}

{:error, reason} ->
# Dispatch to the handle error callback on the jackalope handler if
# implemented
if function_exported?(handler, :handle_error, 1) do
reason = {:payload_decode_error, reason, {topic_levels, payload_string}}
apply(handler, :handle_error, [reason])
handler.handle_error(reason)
end

{:ok, state}
Expand Down
6 changes: 3 additions & 3 deletions test/support/scripted_mqtt_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ defmodule JackalopeTest.ScriptedMqttServer do

use GenServer

alias Tortoise311.Package
alias __MODULE__, as: State

defstruct transport: nil,
server_socket: nil,
script: [],
client_pid: nil,
client: nil,
server_info: nil

alias Tortoise311.Package
alias __MODULE__, as: State

# Client API
@spec start_link() :: GenServer.on_start()
def start_link() do
Expand Down