aboutsummaryrefslogtreecommitdiff
path: root/shard/lib
diff options
context:
space:
mode:
Diffstat (limited to 'shard/lib')
-rw-r--r--shard/lib/application.ex7
-rw-r--r--shard/lib/net/addr.ex32
-rw-r--r--shard/lib/net/manager.ex4
3 files changed, 30 insertions, 13 deletions
diff --git a/shard/lib/application.ex b/shard/lib/application.ex
index 0daf48e..74b0dfd 100644
--- a/shard/lib/application.ex
+++ b/shard/lib/application.ex
@@ -14,14 +14,15 @@ defmodule Shard.Application do
# Define workers and child supervisors to be supervised
children = [
{ DynamicSupervisor, strategy: :one_for_one, name: Shard.DynamicSupervisor },
-
- # Applications & data store
- Shard.Manager,
# Networking
+ SNet.Addr,
SNet.Manager,
SNet.TCPServer,
+ # Applications & data store
+ Shard.Manager,
+
# Keys & identities
Shard.Keys,
{ Task, fn -> Shard.Keys.get_any_identity end },
diff --git a/shard/lib/net/addr.ex b/shard/lib/net/addr.ex
index 630f95a..c1d2f05 100644
--- a/shard/lib/net/addr.ex
+++ b/shard/lib/net/addr.ex
@@ -1,4 +1,25 @@
defmodule SNet.Addr do
+ use Agent
+
+ require Logger
+
+ def start_link(_) do
+ Agent.start_link(__MODULE__, :init, [], name: __MODULE__)
+ end
+
+ def init() do
+ Application.ensure_all_started(:inets)
+ Application.ensure_all_started(:ssl)
+ case :httpc.request('http://api.ipify.org') do
+ {:ok, {_, _, body}} ->
+ {:ok, addr} = :inet.parse_address body
+ Logger.info "Public IP address: #{body}"
+ [addr]
+ _ ->
+ Logger.info "Could not get public IP address"
+ []
+ end
+ end
def get_if_inet4 do
{:ok, ifs} = :inet.getifaddrs
@@ -12,20 +33,15 @@ defmodule SNet.Addr do
end
def get_pub_inet4 do
- Application.ensure_all_started(:inets)
- Application.ensure_all_started(:ssl)
- {:ok, {_, _, body}} = :httpc.request('http://api.ipify.org')
- {:ok, addr} = :inet.parse_address body
- addr
+ Agent.get(__MODULE__, &(&1))
end
def get_all_inet4 do
- addrset = for x <- get_if_inet4(), into: %MapSet{}, do: x
- addrset = MapSet.put(addrset, get_pub_inet4())
+ addrset = for x <- get_if_inet4() ++ get_pub_inet4(), into: %MapSet{}, do: x
MapSet.to_list addrset
end
def is_local?({:inet, ip, port}) do
- port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_if_inet4())
+ port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_all_inet4())
end
end
diff --git a/shard/lib/net/manager.ex b/shard/lib/net/manager.ex
index 624d7e6..dd80347 100644
--- a/shard/lib/net/manager.ex
+++ b/shard/lib/net/manager.ex
@@ -72,7 +72,7 @@ defmodule SNet.Manager do
[] ->
my_port = Application.get_env(:shard, :port)
{:ok, pid} = SNet.TCPConn.start_link(%{connect_to: peer_info, my_port: my_port, auth: auth})
- :ets.insert(:connections, {peer_info, pid, nil})
+ :ets.insert(:connections, {peer_info, pid, auth})
pid
end
end
@@ -93,7 +93,7 @@ defmodule SNet.Manager do
Return the list of all connected peers
"""
def list_connections() do
- for [x] <- :ets.match(:connections, :"$1"), do: x
+ :ets.tab2list(:connections)
end
@doc"""