-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from esl/allocate-reserved-relay-address
Allocate reserved relay address
- Loading branch information
Showing
14 changed files
with
267 additions
and
68 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
defmodule Fennec.ReservationLog do | ||
@moduledoc false | ||
|
||
## Runtime support for storing and fetching pending reservations. | ||
## I.e. an ETS table owner process. | ||
|
||
alias Fennec.TURN.Reservation | ||
alias Jerboa.Format.Body.Attribute.ReservationToken | ||
|
||
def start_link() do | ||
Agent.start_link(fn -> init_db(__MODULE__) end, name: __MODULE__) | ||
end | ||
|
||
def child_spec() do | ||
Supervisor.Spec.worker(Fennec.ReservationLog, []) | ||
end | ||
|
||
@spec register(Reservation.t) :: :ok | {:error, :exists} | ||
def register(%Reservation{} = r) do | ||
case :ets.insert_new(__MODULE__, Reservation.to_tuple(r)) do | ||
false -> {:error, :exists} | ||
_ -> :ok | ||
end | ||
end | ||
|
||
@spec take(ReservationToken.t) :: Reservation.t | nil | ||
def take(%ReservationToken{} = token) do | ||
case :ets.take(__MODULE__, token.value) do | ||
[] -> nil | ||
[r] -> Reservation.from_tuple(r) | ||
end | ||
end | ||
|
||
defp init_db(table_name) do | ||
## TODO: largely guesswork here, not load tested | ||
perf_opts = [write_concurrency: true] | ||
^table_name = :ets.new(table_name, [:public, :named_table] ++ perf_opts) | ||
end | ||
|
||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule Fennec.TURN.Reservation do | ||
@moduledoc false | ||
|
||
## A Reservation represents a relay address reserved | ||
## by an allocation request with a positive `EvenPort.reserved?`. | ||
## The relay address is created along with a RESERVATION-TOKEN | ||
## which is returned to the client requesting the allocation, | ||
## and is stored until another allocation request with the same | ||
## reservation token is sent by the client. | ||
## The Reservation is then turned into a full-blown Allocation. | ||
## This mechanism is used to allocate consecutive port pairs, | ||
## for example for RTP and RTCP transmissions. | ||
|
||
alias Jerboa.Format.Body.Attribute.ReservationToken | ||
|
||
defstruct [:token, :socket] | ||
|
||
@type t :: %__MODULE__{ | ||
token: ReservationToken.t, | ||
socket: Fennec.UDP.socket | ||
} | ||
|
||
@spec new(Fennec.UDP.socket) :: t | ||
def new(socket) do | ||
%__MODULE__{token: ReservationToken.new(), | ||
socket: socket} | ||
end | ||
|
||
## Only intended for storing in ETS | ||
def to_tuple(%__MODULE__{} = r) do | ||
%__MODULE__{token: %ReservationToken{value: token}} = r | ||
{token, r.socket} | ||
end | ||
|
||
## Only intended for storing in ETS | ||
def from_tuple({token, socket}) when is_binary(token) and is_port(socket) do | ||
%__MODULE__{token: %ReservationToken{value: token}, | ||
socket: socket} | ||
end | ||
|
||
end |
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
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
Oops, something went wrong.