Skip to content

Instantly share code, notes, and snippets.

@santosh79
Last active January 10, 2016 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santosh79/08e3cb6555b7b44832ff to your computer and use it in GitHub Desktop.
Save santosh79/08e3cb6555b7b44832ff to your computer and use it in GitHub Desktop.
defmodule SimpleDNS do
def start do
pid = spawn __MODULE__, :loop, [%{}]
{:ok, pid}
end
def get_entry(pid, hostname) do
send pid, {self(), {:get_entry, hostname}}
get_response pid
end
def add_entry(pid, hostname, ip) do
send pid, {self(), {:add_entry, hostname, ip}}
get_response pid
end
def loop(dns_entries) do
receive do
{from, {:get_entry, hostname}} ->
ip = Map.get dns_entries, hostname
send from, {self(), ip}
loop dns_entries
{from, {:add_entry, hostname, new_ip}} ->
updated_dns_entries = Map.put dns_entries, hostname, new_ip
send from, {self(), :ok}
loop updated_dns_entries
end
end
defp get_response(pid) do
receive do
{^pid, msg} ->
msg
_ -> :error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment