Skip to content

Commit

Permalink
remove aliases for Erlang modules
Browse files Browse the repository at this point in the history
  • Loading branch information
savonarola committed Jul 23, 2023
1 parent e69b18a commit 579fa51
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 77 deletions.
20 changes: 9 additions & 11 deletions lib/smppex/mc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ defmodule SMPPEX.MC do
Note that each received connection is served with its own process which uses passed callback module (`MyESMESession`) for handling connection events. Each process has his own state initialized by `init` callback receiving `socket`, `transport` and a copy of arguments (`session_arg`).
"""

alias :ranch, as: Ranch

alias SMPPEX.Session.Defaults

@default_transport :ranch_tcp
@default_acceptor_count 50

@spec start({module, args :: term}, opts :: Keyword.t()) ::
{:ok, listener_ref :: Ranch.ref()}
{:ok, listener_ref :: :ranch.ref()}
| {:error, reason :: term}

@doc """
Expand All @@ -60,7 +58,7 @@ defmodule SMPPEX.MC do
{ref, transport, transport_opts, protocol, protocol_opts} =
ranch_start_args(mod_with_args, opts)

start_result = Ranch.start_listener(ref, transport, transport_opts, protocol, protocol_opts)
start_result = :ranch.start_listener(ref, transport, transport_opts, protocol, protocol_opts)

case start_result do
{:error, _} = error -> error
Expand Down Expand Up @@ -89,16 +87,16 @@ defmodule SMPPEX.MC do
* `:session` (required) a `{module, arg}` tuple, where `module` is the callback module
which should implement `SMPPEX.Session` behaviour, while `arg` is the argument passed
to the `init` callback each time a new connection is received.
* `:transport` is Ranch transport used for TCP connections: either `ranch_tcp` (the default)
* `:transport` is :ranch transport used for TCP connections: either `ranch_tcp` (the default)
or `ranch_ssl`;
* `:transport_opts` is a map of Ranch transport options.
* `:transport_opts` is a map of :ranch transport options.
The major key is `socket_opts` which contains a list of important options such as `{:port, port}`.
The port is set to `0` by default, which means that the listener will accept
connections on a random free port. For backward compatibility one can pass a list of socket options
instead of `transport_opts` map (as in Ranch 1.x).
instead of `transport_opts` map (as in :ranch 1.x).
* `:session_module` is a module to use as an alternative to `SMPPEX.Session`
for handling sessions (if needed). For example, `SMPPEX.TelemetrySession`.
* `:acceptor_count` is the number of Ranch listener acceptors, #{@default_acceptor_count} by default.
* `:acceptor_count` is the number of :ranch listener acceptors, #{@default_acceptor_count} by default.
* `:mc_opts` is a keyword list of MC options:
- `:session_init_limit` is the maximum time for which a session waits an incoming bind request.
If no bind request is received within this interval of time, the session stops.
Expand Down Expand Up @@ -138,7 +136,7 @@ defmodule SMPPEX.MC do
{ref, transport, transport_opts, protocol, protocol_opts} =
ranch_start_args(mod_with_args, opts)

Ranch.child_spec(ref, transport, transport_opts, protocol, protocol_opts)
:ranch.child_spec(ref, transport, transport_opts, protocol, protocol_opts)
end

defp ranch_start_args({_module, _args} = mod_with_args, opts) do
Expand Down Expand Up @@ -172,13 +170,13 @@ defmodule SMPPEX.MC do
Map.put_new(opts, :num_acceptors, acceptor_count)
end

@spec stop(Ranch.ref()) :: :ok
@spec stop(:ranch.ref()) :: :ok

@doc """
Stops MC listener and all its sessions.
"""

def stop(listener) do
Ranch.stop_listener(listener)
:ranch.stop_listener(listener)
end
end
10 changes: 4 additions & 6 deletions lib/smppex/pdu/multipart.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ defmodule SMPPEX.Pdu.Multipart do
Module for operating with multipart information packed as UDH in message body.
"""

alias :proplists, as: Proplists

alias SMPPEX.Pdu
alias SMPPEX.Pdu.UDH

Expand Down Expand Up @@ -175,11 +173,11 @@ defmodule SMPPEX.Pdu.Multipart do
"""
def extract_from_ies(ies) do
cond do
Proplists.is_defined(@concateneated_8bit_ref_ie_id, ies) ->
@concateneated_8bit_ref_ie_id |> Proplists.get_value(ies) |> parse_8bit
:proplists.is_defined(@concateneated_8bit_ref_ie_id, ies) ->
@concateneated_8bit_ref_ie_id |> :proplists.get_value(ies) |> parse_8bit

Proplists.is_defined(@concateneated_16bit_ref_ie_id, ies) ->
@concateneated_16bit_ref_ie_id |> Proplists.get_value(ies) |> parse_16bit
:proplists.is_defined(@concateneated_16bit_ref_ie_id, ies) ->
@concateneated_16bit_ref_ie_id |> :proplists.get_value(ies) |> parse_16bit

true ->
{:ok, :single}
Expand Down
28 changes: 13 additions & 15 deletions lib/smppex/pdu_storage.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule SMPPEX.PduStorage do
@moduledoc false

alias :ets, as: ETS

alias SMPPEX.PduStorage
alias SMPPEX.Pdu

Expand All @@ -22,8 +20,8 @@ defmodule SMPPEX.PduStorage do
def new(id, ttl, ttl_threshold) do
%PduStorage{
id: id,
by_sequence_number: ETS.new(:pdu_storage_by_sequence_number, [:set]),
by_expire: ETS.new(:pdu_storage_by_expire, [:ordered_set]),
by_sequence_number: :ets.new(:pdu_storage_by_sequence_number, [:set]),
by_expire: :ets.new(:pdu_storage_by_expire, [:ordered_set]),
ttl: ttl,
ttl_threshold: ttl_threshold
}
Expand All @@ -35,18 +33,18 @@ defmodule SMPPEX.PduStorage do
sequence_number = Pdu.sequence_number(pdu)
ref = Pdu.ref(pdu)
create_time = Klotho.monotonic_time(:millisecond)
ETS.insert_new(storage.by_sequence_number, {sequence_number, {create_time, pdu}})
ETS.insert_new(storage.by_expire, {{create_time, ref}, sequence_number})
:ets.insert_new(storage.by_sequence_number, {sequence_number, {create_time, pdu}})
:ets.insert_new(storage.by_expire, {{create_time, ref}, sequence_number})
schedule_expire(storage, create_time)
end

@spec fetch(t, non_neg_integer) :: {t, [Pdu.t()]}

def fetch(storage, sequence_number) do
case ETS.take(storage.by_sequence_number, sequence_number) do
case :ets.take(storage.by_sequence_number, sequence_number) do
[{^sequence_number, {create_time, pdu}}] ->
ref = Pdu.ref(pdu)
ETS.delete(storage.by_expire, {create_time, ref})
:ets.delete(storage.by_expire, {create_time, ref})
new_storage = reschedule_expire(storage, create_time)
{new_storage, [pdu]}

Expand All @@ -66,12 +64,12 @@ defmodule SMPPEX.PduStorage do
{ref, sequence_number}
end

expired = ETS.select(storage.by_expire, expired_ms)
expired = :ets.select(storage.by_expire, expired_ms)

pdus =
for {ref, sequence_number} <- expired, into: [] do
[{_sn, {create_time, pdu}}] = ETS.take(storage.by_sequence_number, sequence_number)
ETS.delete(storage.by_expire, {create_time, ref})
[{_sn, {create_time, pdu}}] = :ets.take(storage.by_sequence_number, sequence_number)
:ets.delete(storage.by_expire, {create_time, ref})
pdu
end

Expand All @@ -81,9 +79,9 @@ defmodule SMPPEX.PduStorage do
@spec fetch_all(t) :: {t, [Pdu.t()]}

def fetch_all(storage) do
pdus = for {_sn, {_ex, pdu}} <- ETS.tab2list(storage.by_sequence_number), do: pdu
ETS.delete_all_objects(storage.by_sequence_number)
ETS.delete_all_objects(storage.by_expire)
pdus = for {_sn, {_ex, pdu}} <- :ets.tab2list(storage.by_sequence_number), do: pdu
:ets.delete_all_objects(storage.by_sequence_number)
:ets.delete_all_objects(storage.by_expire)
{cancel_expire(storage), pdus}
end

Expand All @@ -110,7 +108,7 @@ defmodule SMPPEX.PduStorage do

# This is called when a PDU is fetched from storage.
defp reschedule_expire(storage, fetched_create_time) do
case ETS.first(storage.by_expire) do
case :ets.first(storage.by_expire) do
{create_time, _ref} ->
# There is still something left in
if create_time > fetched_create_time or fetched_create_time == nil do
Expand Down
20 changes: 8 additions & 12 deletions lib/smppex/transport_session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ defmodule SMPPEX.TransportSession do
use GenServer
require Logger

alias :proc_lib, as: ProcLib
alias :ranch, as: Ranch
alias :gen_server, as: GenServerErl

alias SMPPEX.Protocol, as: SMPP
alias SMPPEX.Pdu
alias __MODULE__, as: TransportSession
Expand Down Expand Up @@ -70,17 +66,17 @@ defmodule SMPPEX.TransportSession do
{:ok, state}
| {:error, reason}

# Ranch protocol behaviour
# :ranch protocol behaviour

def start_link(ref, transport, opts) do
start_link(:mc, {ref, transport, opts})
end

def start_link(mode, args) do
ProcLib.start_link(__MODULE__, :init_loop, [{mode, args}])
:proc_lib.start_link(__MODULE__, :init_loop, [{mode, args}])
end

# Manual start, without Ranch
# Manual start, without :ranch

def start_esme(socket, transport, opts) do
ref = make_ref()
Expand Down Expand Up @@ -110,8 +106,8 @@ defmodule SMPPEX.TransportSession do
end

def init_loop({:mc, {ref, transport, opts}}) do
:ok = ProcLib.init_ack({:ok, self()})
{:ok, socket} = Ranch.handshake(ref)
:ok = :proc_lib.init_ack({:ok, self()})
{:ok, socket} = :ranch.handshake(ref)
{module, module_opts} = opts

case module.init(socket, transport, module_opts) do
Expand Down Expand Up @@ -140,7 +136,7 @@ defmodule SMPPEX.TransportSession do

case module.init(socket, transport, module_opts) do
{:ok, module_state} ->
:ok = ProcLib.init_ack({:ok, self()})
:ok = :proc_lib.init_ack({:ok, self()})
:ok = accept_grant(ref)

state = %TransportSession{
Expand All @@ -157,7 +153,7 @@ defmodule SMPPEX.TransportSession do
enter_loop(state)

{:stop, reason} ->
ProcLib.init_ack({:error, reason})
:proc_lib.init_ack({:error, reason})
end
end

Expand All @@ -170,7 +166,7 @@ defmodule SMPPEX.TransportSession do
defp enter_loop(state) do
case wait_for_data(state) do
{:noreply, new_state} ->
GenServerErl.enter_loop(__MODULE__, [], new_state)
:gen_server.enter_loop(__MODULE__, [], new_state)

{:stop, reason, _state} ->
Process.exit(self(), reason)
Expand Down
6 changes: 2 additions & 4 deletions test/esme_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule SMPPEX.ESMETest do
use ExUnit.Case

alias :gen_tcp, as: GenTCP

alias Support.TCP.Server
alias Support.Session, as: SupportSession
alias SMPPEX.ESME
Expand Down Expand Up @@ -52,8 +50,8 @@ defmodule SMPPEX.ESMETest do
test "start_link when MC is down" do
server = Server.start_link()
port = Server.port(server)
{:ok, sock} = GenTCP.connect('localhost', port, [])
:ok = GenTCP.close(sock)
{:ok, sock} = :gen_tcp.connect('localhost', port, [])
:ok = :gen_tcp.close(sock)

{:ok, pid} = Agent.start_link(fn -> [] end)
handler = fn {:init, _socket, _transport}, st -> {:ok, st} end
Expand Down
6 changes: 2 additions & 4 deletions test/pdu/pp_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule SMPPEX.Pdu.PPTest do
alias :erlang, as: Erlang

alias SMPPEX.Pdu.PP
alias SMPPEX.Pdu.Factory

Expand All @@ -10,13 +8,13 @@ defmodule SMPPEX.Pdu.PPTest do
pdu = Factory.submit_sm({"from", 1, 2}, {"to", 1, 2}, "message")

# Just check that the function does not fail and that its result is printable
assert pdu |> PP.format() |> Erlang.iolist_to_binary()
assert pdu |> PP.format() |> :erlang.iolist_to_binary()
end

test "format: error status and tlvs" do
pdu = SMPPEX.Pdu.new({0x01, 0x01, 0x01}, %{}, %{0x0005 => 1, 0x2222 => "val"})

# Just check that the function does not fail and that its result is printable
assert pdu |> PP.format() |> Erlang.iolist_to_binary()
assert pdu |> PP.format() |> :erlang.iolist_to_binary()
end
end
14 changes: 6 additions & 8 deletions test/session_test.exs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
defmodule SMPPEX.SessionTest do
use ExUnit.Case

alias :sys, as: Sys

alias Support.TCP.Server
alias SMPPEX.Session
alias SMPPEX.Pdu
Expand Down Expand Up @@ -1470,9 +1468,9 @@ defmodule SMPPEX.SessionTest do
{:code_change, _old_vsn, _extra}, st -> {:ok, st}
end)

Sys.suspend(esme)
Sys.change_code(esme, Support.Session, '0.0.1', :some_extra)
Sys.resume(esme)
:sys.suspend(esme)
:sys.change_code(esme, Support.Session, '0.0.1', :some_extra)
:sys.resume(esme)

assert [
{:init, _, _},
Expand All @@ -1487,9 +1485,9 @@ defmodule SMPPEX.SessionTest do
{:code_change, _old_vsn, _extra}, _st -> {:error, :oops}
end)

Sys.suspend(esme)
Sys.change_code(esme, Support.Session, '0.0.1', :some_extra)
Sys.resume(esme)
:sys.suspend(esme)
:sys.change_code(esme, Support.Session, '0.0.1', :some_extra)
:sys.resume(esme)

assert [
{:init, _, _},
Expand Down
9 changes: 3 additions & 6 deletions test/support/tcp/helpers.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
defmodule Support.TCP.Helpers do
@moduledoc false

alias :gen_tcp, as: GenTCP
alias :inet, as: INET

def find_free_port do
{:ok, socket} = GenTCP.listen(0, [])
{:ok, port} = INET.port(socket)
:ok = GenTCP.close(socket)
{:ok, socket} = :gen_tcp.listen(0, [])
{:ok, port} = :inet.port(socket)
:ok = :gen_tcp.close(socket)
# assume no one will immediately take this port
port
end
Expand Down

0 comments on commit 579fa51

Please sign in to comment.