Created
September 13, 2017 11:58
-
-
Save schmalz/792f0a056e8d1f74112a2af1f7fd3c40 to your computer and use it in GitHub Desktop.
Designing for Scalability with Erlang/OTP - Ch 2 - HLR Module - Elixir
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 characters
defmodule HLR do | |
@moduledoc""" | |
Maintain the associations between Mobile Subscriber Integrated Services Digital Network (MSISDN) numbers and process | |
identifiers (PID). | |
""" | |
def new() do | |
:ets.new(:msisdn_pid, [:named_table]) | |
:ets.new(:pid_msisdn, [:named_table]) | |
:ok | |
end | |
@doc""" | |
Associate an MSISDN number, `ms`, with the current process. | |
""" | |
def attach(ms) do | |
:ets.insert(:msisdn_pid, {ms, self()}) | |
:ets.insert(:pid_msisdn, {self(), ms}) | |
end | |
@doc""" | |
Disassociate the current process from its MSISDN number. | |
""" | |
def detach() do | |
case :ets.lookup(:pid_msisdn, self()) do | |
[{pid, ms}] -> | |
:ets.delete(:pid_msisdn, pid) | |
:ets.delete(:msisdn_pid, ms) | |
[] -> | |
:ok | |
end | |
end | |
@doc""" | |
Lookup the PID associated with an MSISDN `ms`. | |
""" | |
def lookup_id(ms) do | |
case :ets.lookup(:msisdn_pid, ms) do | |
[] -> | |
{:error, :invalid} | |
[{^ms, pid}] -> | |
{:ok, pid} | |
end | |
end | |
@doc""" | |
Lookup the MSISDN associated with a PID. | |
""" | |
def lookup_ms(pid) do | |
case :ets.lookup(:pid_msisdn, pid) do | |
[] -> | |
{:error, :invalid} | |
[{^pid, ms}] -> | |
{:ok, ms} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment