From 35325ce024b36672aca33f08bf61adf3570445bf Mon Sep 17 00:00:00 2001 From: kballou Date: Sun, 30 Oct 2016 22:54:39 -0600 Subject: Add connection acceptor This adds a basic connection acceptor that immediately closes accepted connections and beings to flesh out the supervision tree. --- config/config.exs | 3 +++ lib/octochat/acceptor.ex | 21 +++++++++++++++++++++ lib/octochat/supervisor.ex | 4 +++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 lib/octochat/acceptor.ex diff --git a/config/config.exs b/config/config.exs index d2d855e..52b37d2 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1 +1,4 @@ use Mix.Config + +config :octochat, + port: 9999 diff --git a/lib/octochat/acceptor.ex b/lib/octochat/acceptor.ex new file mode 100644 index 0000000..66d97fc --- /dev/null +++ b/lib/octochat/acceptor.ex @@ -0,0 +1,21 @@ +defmodule Octochat.Acceptor do + @moduledoc """ + Connection/socket accpetor + """ + + @listen_port Application.get_env(:octochat, :port) || 9999 + + def accept do + {:ok, socket} = :gen_tcp.listen( + @listen_port, + [:binary, packet: :line, active: false, reuseaddr: true]) + loop_acceptor(socket) + end + + defp loop_acceptor(socket) do + {:ok, client} = :gen_tcp.accept(socket) + :gen_tcp.close(client) + loop_acceptor(socket) + end + +end diff --git a/lib/octochat/supervisor.ex b/lib/octochat/supervisor.ex index b26f4de..0c38829 100644 --- a/lib/octochat/supervisor.ex +++ b/lib/octochat/supervisor.ex @@ -10,7 +10,9 @@ defmodule Octochat.Supervisor do end def init(_) do - children = [] + children = [ + worker(Task, [Octochat.Acceptor, :accept, []]) + ] opts = [strategy: :one_for_one] supervise(children, opts) -- cgit v1.2.1