Last active
January 10, 2016 08:19
-
-
Save santosh79/08e3cb6555b7b44832ff to your computer and use it in GitHub Desktop.
This file contains 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 characters
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