-
-
Save fishcakez/b39e9b619c0b22df4cf8 to your computer and use it in GitHub Desktop.
Revisions
-
fishcakez revised this gist
May 29, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,7 +22,7 @@ defmodule KVServer do defp loop_acceptor(socket) do {:ok, client} = :gen_tcp.accept(socket) {:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client) end) :ok = :gen_tcp.controlling_process(client, pid) # deliberately fail after 10 sec :timer.sleep(10_000) -
fishcakez revised this gist
May 29, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,15 +14,15 @@ defmodule KVServer do end def accept(port) do {:ok, socket} =:gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true]) IO.puts "Accepting connections on port #{port}" loop_acceptor(socket) end defp loop_acceptor(socket) do {:ok, client} = :gen_tcp.accept(socket) {:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client) end) :ok = gen_tcp.controlling_process(client, pid) # deliberately fail after 10 sec :timer.sleep(10_000) -
smanolloff revised this gist
May 29, 2015 . 1 changed file with 1 addition and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,21 +9,12 @@ defmodule KVServer do worker(Task, [KVServer, :accept, [4040]]) ] opts = [strategy: :one_for_one, name: KVServer.Supervisor] Supervisor.start_link(children, opts) end def accept(port) do {:ok, socket} =:gen_tcp.listen(port, [:binary, packet: :line, active: false]) IO.puts "Accepting connections on port #{port}" loop_acceptor(socket) end -
smanolloff created this gist
May 29, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ defmodule KVServer do use Application def start(_type, _args) do import Supervisor.Spec children = [ supervisor(Task.Supervisor, [[name: KVServer.TaskSupervisor]]), worker(Task, [KVServer, :accept, [4040]]) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options opts = [strategy: :one_for_one, name: KVServer.Supervisor] Supervisor.start_link(children, opts) end def accept(port) do # The options below mean: # # 1. `:binary` - receives data as binaries (instead of lists) # 2. `packet: :line` - receives data line by line # 3. `active: false` - block on `:gen_tcp.recv/2` until data is available # {:ok, socket} =:gen_tcp.listen(port, [:binary, packet: :line, active: false]) IO.puts "Accepting connections on port #{port}" loop_acceptor(socket) end defp loop_acceptor(socket) do {:ok, client} = :gen_tcp.accept(socket) {:ok, pid} = Task.Supervisor.start_child(KVServer.TaskSupervisor, fn -> serve(client) end) :gen_tcp.controlling_process(client, pid) # deliberately fail after 10 sec :timer.sleep(10_000) 1 = 2 loop_acceptor(socket) end defp serve(client) do client |> read_line() |> write_line(client) serve(client) end defp read_line(socket) do {:ok, data} = :gen_tcp.recv(socket, 0) data end defp write_line(line, socket) do :gen_tcp.send(socket, line) end end