Skip to content

Commit af36884

Browse files
committed
chg: [sender] support for supervisor and some doc
1 parent 78e7c61 commit af36884

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,58 @@ Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_do
1919
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
2020
be found at <https://hexdocs.pm/ex_d4>.
2121

22-
# Test
22+
## Usage
23+
First, create Exd4 map:
24+
```elixir
25+
d4_connection = %Exd4{
26+
destination: {192, 168, 0, 1},
27+
port: 4443,
28+
uuid: "b5062e1f-beef-45a0-dead-2557d6e70ef6",
29+
type: 3,
30+
version: 1,
31+
snaplen: 4096,
32+
key: "toto"
33+
}
34+
```
35+
36+
Then the Sender can be run by itself:
37+
```elixir
38+
pry(2)> {:ok, pid} = Sender.start_link(d4_connection: d4_connection)
39+
{:ok, #PID<0.267.0>}
40+
# one can also define a name
41+
pry(3)> {:ok, pid} = Sender.start_link([d4_connection: d4_connection, name: :d4sender])
42+
{:ok, #PID<0.275.0>}
43+
```
44+
Or under a supervisor:
45+
```elixir
46+
def start(_type, _args) do
47+
children = [
48+
{Sender,
49+
d4_connection: %Exd4{
50+
destination: {192, 168, 0, 1},
51+
uuid: "b5062e1f-674e-45a0-9c30-2557d6e70ef8"
52+
},
53+
name: :d4_sender}
54+
]
55+
opts = [strategy: :one_for_one, name: MyApp.Supervisor]
56+
Supervisor.start_link(children, opts)
57+
end
58+
```
59+
60+
Then, we can send packets using `send`:
61+
```elixir
62+
pry(2)> {:ok} = Sender.send(pid, "{\"this\": \"is my test\"}\n")
63+
64+
14:44:38.813 [debug] Sent payload to d4 server.
65+
{:ok}
66+
#using a name if defined on creation, or launched under the supervisor
67+
pry(3)> {:ok} = Sender.send(:d4sender, "toto\n")
68+
69+
14:46:55.494 [debug] Sent payload to d4 server.
70+
{:ok}
71+
```
72+
73+
## Test
2374
D4 server with the following registered sensors:
2475
`b5062e1f-674e-45a0-9c30-2557d6e70ef5`
2576
`b5062e1f-674e-45a0-9c30-2557d6e70ef6`

lib/sender.ex

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,58 @@
11
defmodule Sender do
2+
@moduledoc """
3+
Provides access to a d4 server
4+
It should be supervised since it will terminate after reaching max retries
5+
TODO ^^
6+
"""
27
@behaviour :gen_statem
38

49
require Logger
510

611
defstruct [:d4_connection, :socket, callers: []]
712

13+
@type option :: {:d4_connection, Exd4.t()}
14+
15+
@typedoc """
16+
Current state of the server:
17+
- `:disconnected` - not connected to the d4 server
18+
- `:connected` - connected to the d4 server
19+
"""
20+
@type state :: :disconnected | :connected
21+
22+
@doc """
23+
Returns the default Child Specification for this Server for use in Supervisors.
24+
You can override this with `Supervisor.child_spec/2` as required.
25+
"""
26+
# @spec child_spec([option()]) :: Supervisor.child_spec()
27+
def child_spec(options) do
28+
%{
29+
id: __MODULE__,
30+
start: {__MODULE__, :start_link, [options]},
31+
type: :worker,
32+
restart: :permanent,
33+
shutdown: 500,
34+
strategy: :one_for_one
35+
}
36+
end
37+
838
## Public API
939

40+
@doc """
41+
Starts a new Server.
42+
See `t:option/0` for further details.
43+
"""
44+
@spec start_link([option()]) :: :gen_statem.start_ret()
1045
def start_link(opts) do
46+
{name, opts} = Keyword.pop(opts, :name)
1147
d4_connection = Keyword.fetch!(opts, :d4_connection)
12-
:gen_statem.start_link(__MODULE__, d4_connection, [])
48+
49+
case name do
50+
nil -> :gen_statem.start_link(__MODULE__, d4_connection, [])
51+
name when is_atom(name) -> :gen_statem.start_link({:local, name}, __MODULE__, d4_connection, [])
52+
{:global, _} -> :gen_statem.start_link(name, __MODULE__, d4_connection, [])
53+
{:via, _, _} -> :gen_statem.start_link(name, __MODULE__, d4_connection, [])
54+
{:local, _} -> :gen_statem.start_link(name, __MODULE__, d4_connection, [])
55+
end
1356
end
1457

1558
def send(pid, payload) do

mix.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"earmark_parser": {:hex, :earmark_parser, "1.4.31", "a93921cdc6b9b869f519213d5bc79d9e218ba768d7270d46fdcf1c01bacff9e2", [:mix], [], "hexpm", "317d367ee0335ef037a87e46c91a2269fef6306413f731e8ec11fc45a7efd059"},
44
"ecto": {:hex, :ecto, "3.9.4", "3ee68e25dbe0c36f980f1ba5dd41ee0d3eb0873bccae8aeaf1a2647242bffa35", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "de5f988c142a3aa4ec18b85a4ec34a2390b65b24f02385c1144252ff6ff8ee75"},
55
"ex_doc": {:hex, :ex_doc, "0.29.2", "dfa97532ba66910b2a3016a4bbd796f41a86fc71dd5227e96f4c8581fdf0fdf0", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "6b5d7139eda18a753e3250e27e4a929f8d2c880dd0d460cb9986305dea3e03af"},
6+
"json": {:hex, :json, "1.4.1", "8648f04a9439765ad449bc56a3ff7d8b11dd44ff08ffcdefc4329f7c93843dfa", [:mix], [], "hexpm", "9abf218dbe4ea4fcb875e087d5f904ef263d012ee5ed21d46e9dbca63f053d16"},
67
"makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"},
78
"makeup_elixir": {:hex, :makeup_elixir, "0.16.0", "f8c570a0d33f8039513fbccaf7108c5d750f47d8defd44088371191b76492b0b", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "28b2cbdc13960a46ae9a8858c4bebdec3c9a6d7b4b9e7f4ed1502f8159f338e7"},
89
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},

test/sender_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ defmodule SenderTest do
6868
assert {:error, :disconnected} = Sender.get_status(pid)
6969
end
7070

71-
@tag :debug
7271
test "D4 metaheaders type" do
7372
d4_connection = %Exd4{
7473
destination: Exd4.d4_ip(),
@@ -86,5 +85,6 @@ defmodule SenderTest do
8685
assert {:ok} = Sender.get_status(pid)
8786
assert {:ok} = Sender.send(pid, "{\"this\": \"is my test\"}\n")
8887
assert {:ok} = Sender.send(pid, "{\"this\": \"is my another test\"}\n")
88+
8989
end
9090
end

0 commit comments

Comments
 (0)