add support for automatic connection to upstream hosts

This commit is contained in:
Alain Zscheile 2022-11-24 10:34:32 +01:00
parent 420ba2fd64
commit 9dc3bbed71
3 changed files with 21 additions and 1 deletions

View file

@ -27,6 +27,16 @@ defmodule Floof do
loop_acceptor(socket)
end
def connect(host, port) do
{:ok, socket} = :gen_tcp.connect(host, port, [
packet: 4,
active: true
])
send(Floof.Distributor, {:register, self()})
Logger.info("Established connection to #{host}:#{port}")
serve(socket, %{})
end
defp serve(client, backlog) do
receive do
{:tcp, _, message} ->

View file

@ -9,6 +9,7 @@ defmodule Floof.Application do
@impl true
def start(_type, _args) do
port = Application.fetch_env!(:floof, :port)
upstreams = Application.fetch_env!(:floof, :upstreams)
markers = Application.fetch_env!(:floof, :markers)
attrs = Application.fetch_env!(:floof, :attrs)
set_markers = Application.fetch_env!(:floof, :set_markers)
@ -23,7 +24,14 @@ defmodule Floof.Application do
)},
{Floof.SessionManager, %{}},
{Task, fn -> Floof.accept(port) end},
]
] ++ (for upstream <- upstreams do
{host, port} = case upstream do
{host, port} -> {host, port}
{host} -> {host, 2540}
end
# this establishes connections to upstream hosts
{Task, fn -> Floof.connect(host, port) end}
end)
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options

View file

@ -20,6 +20,8 @@ defmodule Floof.MixProject do
mod: {Floof.Application, []},
env: [
port: 2540,
# upstreams have structure {host, port} or {host} (port defaults to 2540)
upstreams: [],
# markers have the structure {bool(), {:RelativeDistinguishedName, oid-as-list(), value()}}
# all of the markers marked with 'true' should be included in each message to-be-processed
# and none of the markers marked with 'false' should be included. the same holds for attrs.