Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Only review purpose, don't merge] Changes for supporting stickiness for load balancers #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions lib/janus.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Janus.Util

defmodule Janus do

@moduledoc """
This library is a client for the [Janus REST API](https://janus.conf.meetecho.com/docs/rest.html).
"""
Expand All @@ -10,6 +9,5 @@ defmodule Janus do
Retrieves details on the Janus server located at `url`
"""

def info(url), do: get("#{url}/info")

def info(url), do: get("#{url}/info", "")
end
92 changes: 68 additions & 24 deletions lib/janus/plugin.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Janus.Util

defmodule Janus.Plugin do

@moduledoc """
Send messages, trickle candidates, and detach plugins from Janus sessions.
"""
Expand All @@ -10,26 +9,53 @@ defmodule Janus.Plugin do
defstruct [
:id,
:base_url,
:event_manager
:event_manager,
:cookie,
:room_number
]

@doc """
Send `body` as a message to the specified plugin, along with an optional JSEP payload.
"""

def message(pid, body, jsep \\ nil) do
plugin = Agent.get(pid, &(&1))
plugin = Agent.get(pid, & &1)

IO.inspect("~~~ see body ": body)

body =
if body.request == "join" or body.request == "start" or body.request == "exists" do
IO.inspect("~~~ in if ": body.request)
maybe_add_key(body, :room, plugin.room_number)
else
IO.inspect("~~~ in else ": body.request)
body
end

msg = %{body: body, janus: "message"}
post(plugin.base_url, maybe_add_key(msg, :jsep, jsep))

post(plugin.base_url, plugin.cookie, maybe_add_key(msg, :jsep, jsep))
end

def check_room_exists(plugin_base_url, body, cookie) do
msg = %{body: body, janus: "message"}

post(plugin_base_url, cookie, msg)
end

def create(plugin_base_url, body, cookie) do
msg = %{body: body, janus: "message"}
post(plugin_base_url, cookie, msg)
end

@doc """
Hang up any Web RTC connections associated with this plugin.
"""

def hangup(pid) do
plugin = Agent.get(pid, &(&1))
case post(plugin.base_url, %{janus: :hangup}) do
plugin = Agent.get(pid, & &1)

case post(plugin.base_url, plugin.cookie, %{janus: :hangup}) do
{:ok, _} -> :ok
v -> v
end
Expand All @@ -46,13 +72,16 @@ defmodule Janus.Plugin do

def trickle(pid, candidates \\ nil) do
msg = %{janus: :trickle}
msg = case candidates do
nil -> Map.put(msg, :candidate, %{completed: true})
v when is_list(v) -> Map.put(msg, :candidates, v)
v when is_map(v) -> Map.put(msg, :candidate, v)
end
plugin = Agent.get(pid, &(&1))
post(plugin.base_url, msg)

msg =
case candidates do
nil -> Map.put(msg, :candidate, %{completed: true})
v when is_list(v) -> Map.put(msg, :candidates, v)
v when is_map(v) -> Map.put(msg, :candidate, v)
end

plugin = Agent.get(pid, & &1)
post(plugin.base_url, plugin.cookie, msg)
end

@doc """
Expand All @@ -62,33 +91,48 @@ defmodule Janus.Plugin do
"""

def detach(pid) do
base_url = Agent.get(pid, &(&1.base_url))
post(base_url, %{janus: :detach})
base_url = Agent.get(pid, & &1.base_url)
cookie = Agent.get(pid, & &1.cookie)
post(base_url, cookie, %{janus: :detach})
Agent.stop(pid)
end

@doc "See `GenEvent.add_handler/3`."
def add_handler(plugin, handler, args), do: Agent.get plugin, &(GenEvent.add_handler(&1.event_manager, handler, args))
def add_handler(plugin, handler, args),
do: Agent.get(plugin, &GenEvent.add_handler(&1.event_manager, handler, args))

@doc "See `GenEvent.add_mon_handler/3`."
def add_mon_handler(plugin, handler, args), do: Agent.get plugin, &(GenEvent.add_mon_handler(&1.event_manager, handler, args))
def add_mon_handler(plugin, handler, args),
do: Agent.get(plugin, &GenEvent.add_mon_handler(&1.event_manager, handler, args))

@doc "See `GenEvent.call/4`."
def call(plugin, handler, timeout, request \\ 5000), do: Agent.get plugin, &(GenEvent.call(&1.event_manager, handler, request, timeout))
def call(plugin, handler, timeout, request \\ 5000),
do: Agent.get(plugin, &GenEvent.call(&1.event_manager, handler, request, timeout))

@doc "See `GenEvent.remove_handler/3`."
def remove_handler(plugin, handler, args), do: Agent.get plugin, &(GenEvent.remove_handler(&1.event_manager, handler, args))
def remove_handler(plugin, handler, args),
do: Agent.get(plugin, &GenEvent.remove_handler(&1.event_manager, handler, args))

@doc "See `GenEvent.stream/2`."
def stream(plugin, options \\ []), do: Agent.get plugin, &(GenEvent.stream(&1.event_manager, options))
def stream(plugin, options \\ []),
do: Agent.get(plugin, &GenEvent.stream(&1.event_manager, options))

@doc "See `GenEvent.swap_handler/5`."
def swap_handler(plugin, handler1, args1, handler2, args2), do: Agent.get plugin, &(GenEvent.swap_handler(&1.event_manager, handler1, args1, handler2, args2))
def swap_handler(plugin, handler1, args1, handler2, args2),
do:
Agent.get(
plugin,
&GenEvent.swap_handler(&1.event_manager, handler1, args1, handler2, args2)
)

@doc "See `GenEvent.swap_mon_handler/5`."
def swap_mon_handler(plugin, handler1, args1, handler2, args2), do: Agent.get plugin, &(GenEvent.swap_mon_handler(&1.event_manager, handler1, args1, handler2, args2))
def swap_mon_handler(plugin, handler1, args1, handler2, args2),
do:
Agent.get(
plugin,
&GenEvent.swap_mon_handler(&1.event_manager, handler1, args1, handler2, args2)
)

@doc "See `GenEvent.which_handlers/1`."
def which_handlers(plugin), do: Agent.get plugin, &(GenEvent.which_handlers(&1.event_manager))

def which_handlers(plugin), do: Agent.get(plugin, &GenEvent.which_handlers(&1.event_manager))
end
Loading