From aef21fff8e18e33da0b08e274c0c7d216bc10241 Mon Sep 17 00:00:00 2001 From: kballou Date: Thu, 16 Nov 2017 22:53:15 -0700 Subject: Add connection accepter --- config/config.exs | 2 ++ lib/octonetcat/accepter.ex | 25 +++++++++++++++++++++++++ lib/octonetcat/supervisor.ex | 4 +++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/octonetcat/accepter.ex diff --git a/config/config.exs b/config/config.exs index d2d855e..1c9d862 100644 --- a/config/config.exs +++ b/config/config.exs @@ -1 +1,3 @@ use Mix.Config + +config :octonetcat, listen_port: 9999 diff --git a/lib/octonetcat/accepter.ex b/lib/octonetcat/accepter.ex new file mode 100644 index 0000000..c76d0af --- /dev/null +++ b/lib/octonetcat/accepter.ex @@ -0,0 +1,25 @@ +defmodule Octonetcat.Accepter do + @moduledoc """ + Simple Socket/Connection Accepter + """ + + require Logger + + @listen_port Application.get_env(:octonetcat, :listen_port) || 9999 + + def accept do + {:ok, socket} = :gen_tcp.listen( + @listen_port, + [:binary, packet: :line, active: false, reuseaddr: true]) + loop_accepter(socket) + end + + defp loop_accepter(socket) do + {:ok, client} = :gen_tcp.accept(socket) + Logger.info("Accepted connection") + Logger.info("Closing connection") + :gen_tcp.close(client) + loop_accepter(socket) + end + +end diff --git a/lib/octonetcat/supervisor.ex b/lib/octonetcat/supervisor.ex index e66b4e7..01029cd 100644 --- a/lib/octonetcat/supervisor.ex +++ b/lib/octonetcat/supervisor.ex @@ -10,7 +10,9 @@ defmodule Octonetcat.Supervisor do end def init(_) do - children = [] + children = [ + worker(Task, [Octonetcat.Accepter, :accept, []]) + ] opts = [strategy: :one_for_one] supervise(children, opts) -- cgit v1.2.1