-
Notifications
You must be signed in to change notification settings - Fork 95
/
socket_id.ex
43 lines (38 loc) · 947 Bytes
/
socket_id.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
defmodule Poxa.SocketId do
@doc """
Generates a string on the form 123.456
"""
@spec generate! :: binary
def generate! do
<<part1::32, part2::32>> = :crypto.rand_bytes(8)
"#{part1}.#{part2}"
end
@doc """
Returns wheter a socket id is valid or not.
iex> Poxa.SocketId.valid? "123.456"
true
iex> Poxa.SocketId.valid? "123456"
false
iex> Poxa.SocketId.valid? "123.456:channel-private"
false
"""
@spec valid?(binary) :: boolean
def valid?(socket_id) do
Regex.match?(~r/\A\d+\.\d+\z/, socket_id)
end
@doc """
Register the `socket_id` as a property of the process
"""
@spec register!(binary) :: true
def register!(socket_id) do
Poxa.registry.register!(:socket_id, socket_id)
end
@doc """
Get the socket_id of the current process
It throws an ArgumentError if the process has not socket_id
"""
@spec mine :: binary
def mine do
Poxa.registry.fetch(:socket_id)
end
end