aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkballou <kballou@devnulllabs.io>2017-11-24 10:16:06 -0700
committerkballou <kballou@devnulllabs.io>2017-11-24 10:22:44 -0700
commit9bae951c555f9c25876968fa98853253b7007a0f (patch)
tree9bdae36c1344d5c9d4d667bb218b729340d0ed90
parent3af086d168975a4122cee80c955da03516f55571 (diff)
downloadoctonetcat-9bae951c555f9c25876968fa98853253b7007a0f.tar.gz
octonetcat-9bae951c555f9c25876968fa98853253b7007a0f.tar.xz
Level up the Echo Server0.3.0
This should enable the upgrade process to retain connections - Replace our simple task supervisor with a custom `simple_one_for_one` supervisor - Upgrade the echo server to use a `GenServer` behaviour
-rw-r--r--lib/octonetcat/accepter.ex6
-rw-r--r--lib/octonetcat/server_supervisor.ex23
-rw-r--r--lib/octonetcat/servers/echo.ex28
-rw-r--r--lib/octonetcat/supervisor.ex2
-rw-r--r--mix.exs2
5 files changed, 42 insertions, 19 deletions
diff --git a/lib/octonetcat/accepter.ex b/lib/octonetcat/accepter.ex
index a398676..9a570c1 100644
--- a/lib/octonetcat/accepter.ex
+++ b/lib/octonetcat/accepter.ex
@@ -18,11 +18,7 @@ defmodule Octonetcat.Accepter do
{:ok, client} = :gen_tcp.accept(socket)
Logger.info("Accepted connection")
Logger.info("Passing to Echo Server")
- {:ok, pid} = Task.Supervisor.start_child(
- Octonetcat.TaskSupervisor,
- Octonetcat.Echo,
- :serve,
- [client])
+ {:ok, pid} = Octonetcat.ServerSupervisor.start_server(client)
:ok = :gen_tcp.controlling_process(client, pid)
loop_accepter(socket)
end
diff --git a/lib/octonetcat/server_supervisor.ex b/lib/octonetcat/server_supervisor.ex
new file mode 100644
index 0000000..7aa8e37
--- /dev/null
+++ b/lib/octonetcat/server_supervisor.ex
@@ -0,0 +1,23 @@
+defmodule Octonetcat.ServerSupervisor do
+ @moduledoc """
+ Simple One-for-One Supervisor for Socket servers
+ """
+
+ use Supervisor
+
+ def start_link do
+ Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
+ end
+
+ def init(:ok) do
+ children = [
+ worker(Octonetcat.Echo, [], restart: :temporary)
+ ]
+
+ supervise(children, strategy: :simple_one_for_one)
+ end
+
+ def start_server(socket) do
+ Supervisor.start_child(__MODULE__, [socket])
+ end
+end
diff --git a/lib/octonetcat/servers/echo.ex b/lib/octonetcat/servers/echo.ex
index e416d9d..8ad6aff 100644
--- a/lib/octonetcat/servers/echo.ex
+++ b/lib/octonetcat/servers/echo.ex
@@ -6,23 +6,27 @@ defmodule Octonetcat.Echo do
dies on failure or disconnect.
"""
+ use GenServer
require Logger
- def serve(socket) do
- socket
- |> read_line()
- |> write_line!(socket)
+ def start_link(socket) do
+ GenServer.start_link(__MODULE__, socket)
+ end
- serve(socket)
+ def init(socket) do
+ :ok = :inet.setopts(socket, active: true)
+ {:ok, %{socket: socket}}
+ end
+
+ def handle_info({:tcp, _, msg}, state = %{socket: socket}) do
+ msg
+ |> write_line!(socket)
+ {:noreply, state}
end
- def read_line(socket) do
- case :gen_tcp.recv(socket, 0) do
- {:ok, line} -> line
- {:error, reason} ->
- Logger.error(reason)
- Process.exit(self(), :normal)
- end
+ def handle_info({:tcp_closed, _}, _state) do
+ Logger.info("#{__MODULE__}: Connection closing...")
+ {:stop, :normal, %{}}
end
def write_line!(line, socket) do
diff --git a/lib/octonetcat/supervisor.ex b/lib/octonetcat/supervisor.ex
index 50a5f66..64bc27c 100644
--- a/lib/octonetcat/supervisor.ex
+++ b/lib/octonetcat/supervisor.ex
@@ -11,7 +11,7 @@ defmodule Octonetcat.Supervisor do
def init(_) do
children = [
- supervisor(Task.Supervisor, [[name: Octonetcat.TaskSupervisor]]),
+ supervisor(Octonetcat.ServerSupervisor, []),
worker(Task, [Octonetcat.Accepter, :accept, []])
]
diff --git a/mix.exs b/mix.exs
index e96830b..954f1ec 100644
--- a/mix.exs
+++ b/mix.exs
@@ -6,7 +6,7 @@ defmodule Octonetcat.Mixfile do
app: :octonetcat,
description: "Demo Echo Server Application",
package: package(),
- version: "0.2.1",
+ version: "0.3.0",
elixir: "~> 1.5",
start_permanent: Mix.env == :prod,
docs: docs(),