35 lines
872 B
Elixir
35 lines
872 B
Elixir
defmodule RlRepo do
|
|
require Logger
|
|
|
|
@moduledoc """
|
|
"""
|
|
|
|
@doc """
|
|
Spin up the entire stack.
|
|
"""
|
|
def start(port) do
|
|
{:ok, listener_socket} =
|
|
:gen_tcp.listen(port, [:binary, packet: :raw, active: false, reuseaddr: true])
|
|
|
|
Logger.info("Listening on port #{port}")
|
|
|
|
loop_acceptor(listener_socket)
|
|
end
|
|
|
|
def loop_acceptor(listener_socket) do
|
|
Logger.debug("Waiting for connection.")
|
|
|
|
# TODO: error handle this
|
|
{:ok, client_socket} = :gen_tcp.accept(listener_socket)
|
|
|
|
Logger.info("Client Connected.")
|
|
|
|
process_pid = spawn(fn -> RlRepo.ClientHandler.process_request(client_socket) end)
|
|
Logger.info("Handling client connection at PID #{inspect(process_pid)}")
|
|
Logger.info("Handing over socket control.")
|
|
:ok = :gen_tcp.controlling_process(client_socket, process_pid)
|
|
|
|
loop_acceptor(listener_socket)
|
|
end
|
|
end
|