performance.ex 1.18 KB
Newer Older
Jens-Christian Fischer's avatar
Jens-Christian Fischer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
defmodule Grengine.Performance do

  use GenServer


  alias Grengine.Performance
  alias Grengine.Grain

  def start_link(name) do
    GenServer.start_link(__MODULE__, [], name: via_tuple(name))
  end

  defp via_tuple(performance_name) do
    {:via, :gproc, {:n, :l, {:performance, performance_name}}}
  end

  def set_values(performance_name, params) do
    GenServer.cast(via_tuple(performance_name), {:set_values, params})
  end

  def get_values(performance_name) do
    GenServer.call(via_tuple(performance_name), :get_values)
  end

  def send_values(performance_name) do
    GenServer.cast(via_tuple(performance_name), {:send_values, performance_name})
  end

  # SERVER

  def init(params) do
    {:ok, grain} = Grain.start_link()
    {:ok, grain}
  end

  def handle_cast({:set_values, params}, grain) do
    Enum.each(params, fn {k, v} ->
      Grain.set(grain, k, v)
    end)
    {:noreply, grain}
  end

  def handle_cast({:send_values, name}, grain) do
    IO.puts("sending: #{name} #{inspect grain}")
    # GrainsEngine.OscSender.send_grain(name, grain)
    {:noreply, grain}
  end

  def handle_call(:get_values, _from, grain) do
    {:reply, Grain.get_values(grain), grain}
  end


end