Skip to content

Commit

Permalink
Modify Room.add_peer function (#63)
Browse files Browse the repository at this point in the history
* Modify Room.add_peer function

* Fix test
  • Loading branch information
Rados13 authored Apr 17, 2024
1 parent 8480d29 commit 5d9d457
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
19 changes: 14 additions & 5 deletions lib/jellyfish/room.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ defmodule Jellyfish.Room do
...> config: %{max_peers: 10, video_codec: nil},
...> peers: []}
true
iex> assert {:ok, %Jellyfish.Peer{
iex> assert {:ok, %{peer: %Jellyfish.Peer{
...> status: :disconnected,
...> type: Jellyfish.Peer.WebRTC,
...> tracks: []
...> } = peer, _peer_token} = Jellyfish.Room.add_peer(client, room.id, Jellyfish.Peer.WebRTC)
...> } = peer}} = Jellyfish.Room.add_peer(client, room.id, Jellyfish.Peer.WebRTC)
iex> %Jellyfish.Peer{
...> id: peer.id,
...> status: :disconnected,
Expand Down Expand Up @@ -64,6 +64,14 @@ defmodule Jellyfish.Room do
"""
@type peer_token :: String.t()

@typedoc """
Jellyfish response to adding a peer to room. It consists of:
* peer structure
* token used for authentication when connecting through websocket to jellyfish
* ws_url that is a websocket adress to which this specific peer have to connect
"""
@type peer_create_response :: %{peer: Peer.t(), token: peer_token(), ws_url: String.t()}

@typedoc """
Options used for creating a room.
Expand Down Expand Up @@ -164,7 +172,8 @@ defmodule Jellyfish.Room do
Adds a peer to the room with `room_id`.
"""
@spec add_peer(Client.t(), id(), Peer.options() | Peer.type()) ::
{:ok, Peer.t(), peer_token()} | {:error, atom() | String.t()}
{:ok, peer_create_response()}
| {:error, atom() | String.t()}
def add_peer(client, room_id, peer) do
peer = if is_atom(peer), do: struct!(peer), else: peer

Expand All @@ -175,9 +184,9 @@ defmodule Jellyfish.Room do
Map.from_struct(peer)
|> Map.new(fn {k, v} -> {snake_case_to_camel_case(k), v} end)
}),
%{"peer" => peer, "token" => token} <- data,
%{"peer" => peer, "token" => token, "peer_websocket_url" => peer_websocket_url} <- data,
result <- Peer.from_json(peer) do
{:ok, result, token}
{:ok, %{peer: result, token: token, ws_url: peer_websocket_url}}
end
end

Expand Down
10 changes: 6 additions & 4 deletions test/jellyfish/notifier_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ defmodule Jellyfish.NotifierTest do
peer_disconnected_timeout_s: @peer_disconnected_timeout_s
)

{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = Room.add_peer(client, room_id, @peer_opts)
{:ok, %{peer: %Jellyfish.Peer{id: peer_id}, token: peer_token}} =
Room.add_peer(client, room_id, @peer_opts)

{:ok, peer_ws} = WS.start_link("ws://#{jellyfish_address}/socket/peer/websocket")

Expand Down Expand Up @@ -238,16 +239,17 @@ defmodule Jellyfish.NotifierTest do
end

defp create_room_and_auth_ws(client, room_opts \\ []) do
{:ok, %Jellyfish.Room{id: room_id}, jellyfish_address} =
{:ok, %Jellyfish.Room{id: room_id}, _jellyfish_address} =
Room.create(client,
max_peers: Keyword.get(room_opts, :max_peers, @max_peers),
video_codec: Keyword.get(room_opts, :video_codec, @video_codec),
webhook_url: Keyword.get(room_opts, :webhook_url, @webhook_address)
)

{:ok, %Jellyfish.Peer{id: peer_id}, peer_token} = Room.add_peer(client, room_id, @peer_opts)
{:ok, %{peer: %Jellyfish.Peer{id: peer_id}, token: peer_token, ws_url: peer_ws_url}} =
Room.add_peer(client, room_id, @peer_opts)

{:ok, peer_ws} = WS.start_link("ws://#{jellyfish_address}/socket/peer/websocket")
{:ok, peer_ws} = WS.start_link("ws://#{peer_ws_url}")

auth_request = %PeerMessage{content: {:auth_request, %AuthRequest{token: peer_token}}}
:ok = WS.send_frame(peer_ws, auth_request)
Expand Down
8 changes: 4 additions & 4 deletions test/jellyfish/room_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@ defmodule Jellyfish.RoomTest do
setup [:create_room]

test "when request is valid", %{client: client, room_id: room_id} do
assert {:ok, peer, _peer_token} = Room.add_peer(client, room_id, @peer_opts)
assert {:ok, %{peer: peer}} = Room.add_peer(client, room_id, @peer_opts)
assert %Jellyfish.Peer{type: Peer.WebRTC} = peer

assert {:ok, peer, _peer_token} = Room.add_peer(client, room_id, @peer_opts_module)
assert {:ok, %{peer: peer}} = Room.add_peer(client, room_id, @peer_opts_module)
assert %Jellyfish.Peer{type: Peer.WebRTC} = peer
end

Expand All @@ -440,7 +440,7 @@ defmodule Jellyfish.RoomTest do
{:ok, %Jellyfish.Room{id: room_id}, _jellyfish_address} =
Room.create(client, max_peers: 1, video_codec: @video_codec)

assert {:ok, _peer, _peer_token} = Room.add_peer(client, room_id, @peer_opts)
assert {:ok, _response} = Room.add_peer(client, room_id, @peer_opts)

error_msg = "Request failed: Reached peer limit in room #{room_id}"

Expand Down Expand Up @@ -582,7 +582,7 @@ defmodule Jellyfish.RoomTest do
end

defp create_peer(state) do
assert {:ok, %Jellyfish.Peer{id: id}, _token} =
assert {:ok, %{peer: %Jellyfish.Peer{id: id}}} =
Room.add_peer(state.client, state.room_id, @peer_opts)

%{peer_id: id}
Expand Down

0 comments on commit 5d9d457

Please sign in to comment.