Skip to content

Commit

Permalink
Building the graph in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkaspa committed May 12, 2016
1 parent 846b088 commit d11322c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lib/exstreme/gnode/behaviour.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule Exstreme.Gnode.Behaviour do
@moduledoc """
"""

defmodule Data do
@moduledoc """
"""
alias __MODULE__

@type t :: %Data{next: [pid], pid: pid, func: (term) -> no_return, opts: [key: term]}
defstruct [next: [], pid: nil, func: nil, opts: []]
end

def __using__ do
quote do
use GenServer
alias Exstreme.Gnode.Behaviour.Data

def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts)
end

def init(params = [func: func]) do
opts = Keyword.drop(params, [:func, :type])
{:ok, %Data{func: func, pid: self, opts: opts}}
end

def handle_cast({:connect, to_pid}, data) do
new_data = update_in(data.next, fn(next) -> [pid | next] end)
{:noreply, new_data}
end

def send_next(next, msg) do
Enum.each(next, fn(pid) ->
GenServer.cast(pid, msg)
do)
end
end
end
end
7 changes: 7 additions & 0 deletions lib/exstreme/gnode/broadcast.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Exstreme.GNode.Broadcast do
use Exstreme.GNode.Behaviour

def handle_cast({:next, {_, msg}}, stats) do
send_next(stats.next, msg)
end
end
8 changes: 8 additions & 0 deletions lib/exstreme/gnode/common.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Exstreme.GNode.Common do
use Exstreme.GNode.Behaviour

def handle_cast({:next, {_, msg} }, stats) do
{:ok, result} = stats.func.(msg)
send_next(stats.next, result)
end
end
7 changes: 7 additions & 0 deletions lib/exstreme/gnode/funnel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Exstreme.GNode.Funnel do
use Exstreme.GNode.Behaviour

def handle_cast({:next, {from_data, msg}}, stats) do
end

end
2 changes: 1 addition & 1 deletion lib/exstreme/graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Exstreme.Graph do
"""
alias __MODULE__

@type t :: %Graph{params: [key: term], nodes: %{key: atom}, connections: %{key: atom}}
@type t :: %Graph{params: [key: term], nodes: %{key: [key: term]}, connections: %{key: atom}}
defstruct params: [], nodes: %{}, connections: %{}

@doc """
Expand Down
55 changes: 55 additions & 0 deletions lib/exstreme/graph_builder.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule Exstreme.GraphBuilder do
@moduledoc """
"""
alias Exstreme.GNode.Broadcast
alias Exstreme.GNode.Funnel
alias Exstreme.GNode.Common

@doc """
"""
@spec build(Graph.t) :: Graph.t
def build(graph) do
new_graph = update_in(graph.nodes, &start_nodes/1)
connect_nodes(new_graph)
new_graph
end

#private

@spec start_nodes(%{key: [key: term]}) :: %{key: [key: term]}
defp start_nodes(nodes) do
nodes
|> Enum.map(&start_node/1)
|> Map.new
end

@spec start_node({atom, [key: any]}) :: {atom, [key: any]}
defp start_node({node, params = [type: type]}) do
{:ok, pid} =
case type do
:broadcast -> Broadcast.start_link(params)
:funnel -> Funnel.start_link(params)
_ -> Common.start_link(params)
end
{node, Keyword.put(params, :pid, pid)}
end

@spec connect_nodes(Graph.t) :: no_return
defp connect_nodes(%Graph{nodes: nodes, connections: connections}) do
Enum.each(connections, &connect_pair/1)
end

@spec connect_pair({atom, [atom]}) :: no_return
defp connect_pair({from, to}) when is_list(to) do
Enum.map(1..Enum.count(to), fn(_) -> from end)
|> Enum.zip(to)
|> Enum.each(&connect_pair/1)
end

@spec connect_pair({atom, atom}) :: no_return
defp connect_pair({from, to}) when is_atom(to) do
[pid: pid_from] = nodes[from]
[pid: pid_to] = nodes[to]
GenServer.cast(pid_from, {:connect, pid_to})
end
end

0 comments on commit d11322c

Please sign in to comment.