Rename Peer to 'Backing Agent'

This commit is contained in:
bluepython508
2024-09-26 13:06:58 +01:00
parent 85bf788307
commit bdacd9905e
6 changed files with 39 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
defmodule Frajtano.Agent do
alias Frajtano.Peer
alias Frajtano.BackingAgent
use GenServer
require Logger
@@ -15,49 +15,49 @@ defmodule Frajtano.Agent do
}
end
def initial_peers() do
initial_peers = Application.fetch_env!(:frajtano, :initial_peers)
|> Enum.map(&GenServer.call(__MODULE__, {:add_peer, &1}))
Logger.info("Started initial peers: #{inspect initial_peers}")
def initial_backing_agents() do
initial_backing_agents = Application.fetch_env!(:frajtano, :initial_backing_agents)
|> Enum.map(&GenServer.call(__MODULE__, {:add_backing_agent, &1}))
Logger.info("Started initial backing_agents: #{inspect initial_backing_agents}")
end
# select: list of specs, where specs are a tuple of match, guards, and outputs
# match is {key, pid, value}, :"$1" is a match variable
def peer_paths() do
Registry.select(Frajtano.Peers, [{{:"$1", :_, :_}, [], [:"$1"]}])
def backing_agent_paths() do
Registry.select(Frajtano.BackingAgents, [{{:"$1", :_, :_}, [], [:"$1"]}])
end
def peer_pids() do
Registry.select(Frajtano.Peers, [{{:_, :"$1", :_}, [], [:"$1"]}])
def backing_agent_pids() do
Registry.select(Frajtano.BackingAgents, [{{:_, :"$1", :_}, [], [:"$1"]}])
end
@impl true
def handle_call({:identities}, _from, _state) do
idents =
Task.async_stream(
peer_pids(),
&{&1, Peer.identities(&1)},
backing_agent_pids(),
&{&1, BackingAgent.identities(&1)},
ordered: false,
on_timeout: :kill_task
)
idents = for {:ok, {peer, {:ok, idents}}} <- idents, do: {idents, peer}
idents = for {:ok, {backing_agent, {:ok, idents}}} <- idents, do: {idents, backing_agent}
{
:reply,
{:ok, idents |> Enum.flat_map(&elem(&1, 0)) |> Enum.uniq},
for({idents, peer} <- idents, {key, _comment} <- idents, into: %{}, do: {key, peer})
for({idents, backing_agent} <- idents, {key, _comment} <- idents, into: %{}, do: {key, backing_agent})
}
end
@impl true
def handle_call({:sign, {key, _, _} = req}, _from, state) do
{:reply, Peer.sign(state[key], req), state}
{:reply, BackingAgent.sign(state[key], req), state}
end
@impl true
def handle_call({:add_peer, spec}, _from, state) do
case Peer.start(spec) do
def handle_call({:add_backing_agent, spec}, _from, state) do
case BackingAgent.start(spec) do
{:ok, _} -> {:reply, :ok, state}
{:error, error} -> {:reply, {:error, error}, state}
end
@@ -73,10 +73,10 @@ defmodule Frajtano.Agent do
end
def assimilate(path) do
GenServer.call(__MODULE__, {:add_peer, {:socket, path}})
GenServer.call(__MODULE__, {:add_backing_agent, {:socket, path}})
end
def spawn_peer(spec) do
GenServer.call(__MODULE__, {:add_peer, {:spawn, spec}})
def spawn_backing_agent(spec) do
GenServer.call(__MODULE__, {:add_backing_agent, {:spawn, spec}})
end
end