Skip to content

Commit 63f2272

Browse files
authored
Add more statistics (#18)
1 parent 4c1917b commit 63f2272

File tree

9 files changed

+71
-18
lines changed

9 files changed

+71
-18
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ jobs:
66
name: lint OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
77
strategy:
88
matrix:
9-
otp: ['25']
10-
elixir: ['1.13.2']
9+
otp: ['26']
10+
elixir: ['1.15']
1111
steps:
1212
- uses: actions/checkout@v2
1313
- uses: erlef/setup-beam@v1
@@ -24,8 +24,8 @@ jobs:
2424
name: test OTP ${{matrix.otp}} / Elixir ${{matrix.elixir}}
2525
strategy:
2626
matrix:
27-
otp: ['25']
28-
elixir: ['1.13.2']
27+
otp: ['26']
28+
elixir: ['1.15']
2929
env:
3030
MIX_ENV: test
3131
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ as WebRTC multiplexes traffic on a single socket but PRs are welcomed
2727
```elixir
2828
def deps do
2929
[
30-
{:ex_ice, "~> 0.5.0"}
30+
{:ex_ice, "~> 0.6.0"}
3131
]
3232
end
3333
```

lib/candidate.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ defmodule ExICE.Candidate do
33
ICE candidate representation.
44
"""
55

6+
alias ExICE.Utils
7+
68
@type type() :: :host | :srflx | :prflx | :relay
79

810
@type t() :: %__MODULE__{
11+
id: integer(),
912
address: :inet.ip_address(),
1013
base_address: :inet.ip_address() | nil,
1114
base_port: :inet.port_number() | nil,
@@ -19,6 +22,7 @@ defmodule ExICE.Candidate do
1922

2023
@derive {Inspect, except: [:socket]}
2124
defstruct [
25+
:id,
2226
:address,
2327
:base_address,
2428
:base_port,
@@ -46,6 +50,7 @@ defmodule ExICE.Candidate do
4650
priority = opts[:priority] || priority(type)
4751

4852
%__MODULE__{
53+
id: Utils.id(),
4954
address: address,
5055
base_address: base_address,
5156
base_port: base_port,
@@ -90,6 +95,7 @@ defmodule ExICE.Candidate do
9095
{:ok, type} <- parse_type(ty_str) do
9196
{:ok,
9297
%__MODULE__{
98+
id: Utils.id(),
9399
address: address,
94100
foundation: foundation,
95101
port: port,

lib/candidate_pair.ex

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ defmodule ExICE.CandidatePair do
44
"""
55
require Logger
66

7-
alias ExICE.ICEAgent
8-
alias ExICE.Candidate
7+
alias ExICE.{Candidate, ICEAgent, Utils}
98

109
# Tr timeout (keepalives) in ms
1110
@tr_timeout 15 * 1000
@@ -43,10 +42,8 @@ defmodule ExICE.CandidatePair do
4342
def new(local_cand, remote_cand, agent_role, state, opts \\ []) do
4443
priority = priority(agent_role, local_cand, remote_cand)
4544

46-
<<id::12*8>> = :crypto.strong_rand_bytes(12)
47-
4845
%__MODULE__{
49-
id: id,
46+
id: Utils.id(),
5047
local_cand: local_cand,
5148
remote_cand: remote_cand,
5249
priority: priority,

lib/ice_agent.ex

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,20 @@ defmodule ExICE.ICEAgent do
211211
212212
* `bytes_sent` - data bytes sent. This does not include connectivity checks and UDP/IP header sizes.
213213
* `bytes_received` - data bytes received. This does not include connectivity checks and UDP/IP header sizes.
214-
* `candidate_pairs` - list of current candidate pairs. It will change after doing an ICE restart.
214+
* `packets_sent` - data packets sent. This does not include connectivity checks.
215+
* `packets_received` - data packets received. This does not include connectivity checks.
216+
* `candidate_pairs` - list of current candidate pairs. Changes after doing an ICE restart.
215217
"""
216218
@spec get_stats(pid()) :: %{
217219
bytes_sent: non_neg_integer(),
218-
bytes_received: non_neg_integer,
220+
bytes_received: non_neg_integer(),
221+
packets_sent: non_neg_integer(),
222+
packets_received: non_neg_integer(),
223+
state: atom(),
224+
role: atom(),
225+
local_ufrag: binary(),
226+
local_candidates: [Candidate.t()],
227+
remote_candidates: [Candidate.t()],
219228
candidate_pairs: [CandidatePair.t()]
220229
}
221230
def get_stats(ice_agent) do
@@ -299,7 +308,9 @@ defmodule ExICE.ICEAgent do
299308
turn_servers: [],
300309
# stats
301310
bytes_sent: 0,
302-
bytes_received: 0
311+
bytes_received: 0,
312+
packets_sent: 0,
313+
packets_received: 0
303314
}
304315

305316
{:ok, state}
@@ -335,6 +346,13 @@ defmodule ExICE.ICEAgent do
335346
stats = %{
336347
bytes_sent: state.bytes_sent,
337348
bytes_received: state.bytes_received,
349+
packets_sent: state.packets_sent,
350+
packets_received: state.packets_received,
351+
state: state.state,
352+
role: state.role,
353+
local_ufrag: state.local_ufrag,
354+
local_candidates: state.local_cands,
355+
remote_candidates: state.remote_cands,
338356
candidate_pairs: Map.values(state.checklist)
339357
}
340358

@@ -470,7 +488,15 @@ defmodule ExICE.ICEAgent do
470488

471489
dst = {pair.remote_cand.address, pair.remote_cand.port}
472490
bytes_sent = do_send(pair.local_cand.socket, dst, data)
473-
{:noreply, %{state | bytes_sent: state.bytes_sent + bytes_sent}}
491+
# if we didn't manage to send any bytes, don't increment packets_sent
492+
packets_sent = if bytes_sent == 0, do: 0, else: 1
493+
494+
{:noreply,
495+
%{
496+
state
497+
| bytes_sent: state.bytes_sent + bytes_sent,
498+
packets_sent: state.packets_sent + packets_sent
499+
}}
474500
end
475501

476502
@impl true
@@ -617,7 +643,13 @@ defmodule ExICE.ICEAgent do
617643
end
618644
else
619645
notify(state.on_data, {:data, packet})
620-
{:noreply, %{state | bytes_received: state.bytes_received + byte_size(packet)}}
646+
647+
{:noreply,
648+
%{
649+
state
650+
| bytes_received: state.bytes_received + byte_size(packet),
651+
packets_received: state.packets_received + 1
652+
}}
621653
end
622654
end
623655

lib/utils.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ defmodule ExICE.Utils do
88
{_, _, _, _, _, _, _, _} -> :ipv6
99
end
1010
end
11+
12+
@spec id() :: non_neg_integer()
13+
def id() do
14+
<<id::12*8>> = :crypto.strong_rand_bytes(12)
15+
id
16+
end
1117
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule ExICE.MixProject do
22
use Mix.Project
33

4-
@version "0.5.0"
4+
@version "0.6.0"
55
@source_url "https://github.com/elixir-webrtc/ex_ice"
66

77
def project do

test/candidate_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ defmodule ExICE.CandidateTest do
3636
expected_c = Candidate.new(:host, ip, port, nil, nil, nil, priority: 1234)
3737

3838
assert {:ok, c} = Candidate.unmarshal(m_c)
39-
39+
c = %Candidate{c | id: expected_c.id}
4040
assert c == expected_c
4141
end
4242
end

test/ice_agent_test.exs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,21 @@ defmodule ExICE.ICEAgentTest do
2121
test "get_stats/1" do
2222
{:ok, agent} = ICEAgent.start_link(:controlling)
2323

24-
assert %{bytes_sent: 0, bytes_received: 0, candidate_pairs: candidate_pairs} =
24+
assert %{
25+
bytes_sent: 0,
26+
bytes_received: 0,
27+
packets_sent: 0,
28+
packets_received: 0,
29+
state: :new,
30+
role: :controlling,
31+
local_ufrag: local_ufrag,
32+
local_candidates: [],
33+
remote_candidates: [],
34+
candidate_pairs: candidate_pairs
35+
} =
2536
ICEAgent.get_stats(agent)
2637

2738
assert is_list(candidate_pairs)
39+
assert is_binary(local_ufrag)
2840
end
2941
end

0 commit comments

Comments
 (0)