floof-webchat/lib/chat/application.ex
2022-11-26 18:08:04 +01:00

60 lines
1.9 KiB
Elixir

defmodule Chat.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
require Logger
@impl true
def start(_type, _args) do
children = [
# Start the Telemetry supervisor
ChatWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: Chat.PubSub},
# Start the Endpoint (http/https)
ChatWeb.Endpoint,
# Start a worker by calling: Chat.Worker.start_link(arg)
# {Chat.Worker, arg}
# Start the message handler
Chat.Floof,
# Start the relay
{Task, fn ->
Floof.Distributor.register(self())
relay_loop()
end}
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Chat.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
ChatWeb.Endpoint.config_change(changed, removed)
:ok
end
defp relay_loop do
receive do
{:fwdxfer, {_, {:XferBlob, source, _, _, _, xfinner}}} ->
{:ok, dcdi} = :FloofProtocol.decode(:XferInner, xfinner)
{:XferInner, topic, attrs, _, data} = dcdi
if topic == [{:RelativeDistinguishedName, {0, 9, 2342, 19200300, 100, 1, 14}, "Chat"}] do
[{:RelativeDistinguishedName, {0, 9, 2342, 19200300, 100, 1, 4}, name}] = attrs
{:ok, source_encd} = :FloofProtocol.encode(:RDNSequence, source)
source_encd = Base.url_encode64(source_encd)
ChatWeb.Endpoint.broadcast!("room:lobby", "shout", %{name: source_encd <> " :: " <> name, message: data})
end
x -> Logger.error("unable to handle message #{inspect(x)}")
end
relay_loop()
end
end