-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,47 @@ | ||
defmodule Exstreme.Gnode.Behaviour do | ||
defmodule Exstreme.GNode.Behaviour do | ||
@moduledoc """ | ||
""" | ||
|
||
defmacro __using__(_) do | ||
quote do | ||
use GenServer | ||
import Exstreme.GNode.Behaviour | ||
end | ||
end | ||
|
||
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: []] | ||
@type graph_func :: ((term, Data.t) -> {:ok, term} | :error) | ||
|
||
@type t :: %Data{next: [pid], pid: pid, nid: atom, func: graph_func, funnel_queue: [term], received_counter: non_neg_integer, sent_counter: non_neg_integer, opts: [key: term]} | ||
defstruct [next: [], pid: nil, nid: nil, func: nil, funnel_queue: [], received_counter: 0, sent_counter: 0, opts: []] | ||
|
||
#TODO use counters | ||
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 | ||
def start_link(opts \\ []) do | ||
GenServer.start_link(__MODULE__, :ok, opts) | ||
end | ||
|
||
def init(params = [func: func, nid: nid]) do | ||
opts = Keyword.drop(params, [:func, :type, :nid]) | ||
{:ok, %Data{func: func, pid: self, nid: nid, opts: opts}} | ||
end | ||
|
||
def handle_cast({:connect, to_pid}, data) do | ||
new_data = update_in(data.next, fn(next) -> [to_pid | next] end) | ||
{:noreply, new_data} | ||
end | ||
|
||
def handle_cast({:send_next, next, msg}, data) do | ||
Enum.each(next, &(GenServer.cast(&1, {:next, self, msg}))) | ||
{:noreply, data} | ||
end | ||
|
||
def send_next(pid, next, msg) do | ||
GenServer.cast(pid, {:send_next, next, msg}) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
defmodule Exstreme.GNode.Broadcast do | ||
use Exstreme.GNode.Behaviour | ||
|
||
def handle_cast({:next, {_, msg}}, stats) do | ||
send_next(stats.next, msg) | ||
def handle_cast({:next, _, msg}, data) do | ||
send_next(self, data.next, msg) | ||
{:noreply, data} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
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) | ||
def handle_cast({:next, _, msg}, data) do | ||
{:ok, result} = data.func.(msg, data) | ||
send_next(self, data.next, result) | ||
{:noreply, data} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
defmodule Exstreme.GNode.Funnel do | ||
use Exstreme.GNode.Behaviour | ||
|
||
def handle_cast({:next, {from_data, msg}}, stats) do | ||
def handle_cast({:next, from_data, msg}, data) do | ||
{res, new_queue} = | ||
data.funnel_queue | ||
|> add_queue(from_data.nid, msg) | ||
|> get_queue(data.opts[:before_nodes]) | ||
send_message(res, data.next) | ||
{:noreply, update_in(data.funnel_queue, new_queue)} | ||
end | ||
|
||
#private | ||
|
||
defp send_message(res, next) do | ||
if res != nil do | ||
new_msg = | ||
res | ||
|> Map.values | ||
|> List.to_tuple | ||
send_next(self, next, new_msg) | ||
end | ||
end | ||
|
||
defp add_queue(queue, from, msg) do | ||
idx = Enum.find_index(queue, &(!Map.has_key?(&1, from))) | ||
if idx != nil do | ||
List.update_at(queue, idx, &(Map.put(&1, from, msg))) | ||
else | ||
[queue | Map.new |> Map.put(from, msg)] | ||
end | ||
end | ||
|
||
defp get_queue(queue = [head | tail], before_nodes) do | ||
before_nodes_set = MapSet.new(before_nodes) | ||
if MapSet.equal?(MapSet.new(Map.keys(head)), before_nodes_set) do | ||
{head, tail} | ||
else | ||
{nil, queue} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
defmodule Exstreme.GraphBuilderTest do | ||
use ExUnit.Case | ||
use Exstreme.Common | ||
alias Exstreme.GraphBuilder | ||
doctest Exstreme.GraphBuilder | ||
|
||
test "" do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters