Current section

Files

Jump to
driver8 lib driver8.ex
Raw

lib/driver8.ex

defmodule Driver8 do
@moduledoc """
"""
use GenServer
@doc """
Starts a `Driver8` process linked to the current process (to be used as a part of supervision tree).
Once process is started it will register itself with default name `Driver8`. This name is used by `Driver8.Plug` to
call for request handling.
## Options
* :name` - value used for name registration, if not passed defaults to `Driver8`
"""
def start_link(opts) do
name = Keyword.get(opts, :name, Driver8)
GenServer.start_link(__MODULE__, [prefix: name], name: name)
end
@impl true
def init(opts) do
{:ok, supervisor_pid} = DynamicSupervisor.start_link(strategy: :one_for_one)
prefix = Keyword.get(opts, :prefix, Driver8)
registry = String.to_atom("#{prefix}.Registry")
{:ok, _} = Registry.start_link(keys: :unique, name: registry)
{:ok, [supervisor: supervisor_pid, registry: registry]}
end
@impl true
def handle_cast({:reset_timer, session_id}, state) do
Driver8.Session.reset_timer(state[:registry], session_id)
{:noreply, state}
end
@impl true
def handle_call(:is_ready, _from, state) do
{:reply, true, state}
end
@impl true
def handle_call({:new_session, params}, _from, state) do
{:reply, Driver8.Session.new(state[:supervisor], state[:registry], params), state}
end
@impl true
def handle_call({:remove_session, session_id}, _from, state) do
{:reply, Driver8.Session.remove(state[:registry], session_id), state}
end
@impl true
def handle_call({:find_session, session_id}, _from, state) do
{:reply, Driver8.Session.find(state[:registry], session_id), state}
end
@impl true
def handle_call(request, _from, state) do
case request do
{:ext, path, _session_id, _opts} ->
{:reply,
{:error,
"Driver8: Cannot process session level request: #{path |> Enum.join("/")}. Make sure your extension and client library use the same URL",
request}, state}
_ ->
{:reply, {:error, "Driver8: Cannot process request", request}, state}
end
end
end