aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkballou <kballou@devnulllabs.io>2017-11-16 22:53:15 -0700
committerkballou <kballou@devnulllabs.io>2017-11-24 09:54:20 -0700
commitaef21fff8e18e33da0b08e274c0c7d216bc10241 (patch)
tree88f126162be2baf6092ca836e4bfb49b1fa4c71e
parent263ec967e18a0dbb874f2297cbeb7b3ef042cd9a (diff)
downloadoctonetcat-aef21fff8e18e33da0b08e274c0c7d216bc10241.tar.gz
octonetcat-aef21fff8e18e33da0b08e274c0c7d216bc10241.tar.xz
Add connection accepter0.1.0
-rw-r--r--config/config.exs2
-rw-r--r--lib/octonetcat/accepter.ex25
-rw-r--r--lib/octonetcat/supervisor.ex4
3 files changed, 30 insertions, 1 deletions
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)