Sha256: ee8357aaba3454db64abf3995cdd773441934fb4f10a40383a7bf2125c50c879

Contents?: true

Size: 1.43 KB

Versions: 9

Compression:

Stored size: 1.43 KB

Contents

-module(gruff).
-export([start/0, stop/0, plot/4]).

start() ->
  spawn(fun() ->
    register(gruff, self()),
    process_flag(trap_exit, true),
    Cmd = "ruby ./gruff_provider.rb",
    Port = open_port({spawn, Cmd}, [{packet, 4}, nouse_stdio, exit_status, binary]),
    port_loop(Port)
  end).

stop() -> gruff ! stop.

plot(Name, Font, Data, Labels) ->
  gruff ! {plot, self(), Name, Font, Data, Labels},
  receive
    {result, Bin} -> Bin
  end.

send_data(_Port, []) -> ok;
send_data(Port, [{Name, Points}|Rest]) ->
  Data = {data, Name, Points},
  Port ! {self(), {command, term_to_binary(Data)}},
  send_data(Port, Rest).

send_labels(Port, Labels) ->
  Data = {labels, Labels},
  Port ! {self(), {command, term_to_binary(Data)}}.

port_loop(Port) ->
  receive
    {plot, Caller, Name, Font, Data, Labels} ->
      PlotData = term_to_binary({plot, Name, 'Line', Font}),
      Port ! {self(), {command, PlotData}},

      send_data(Port, Data),
      send_labels(Port, Labels),

      EndData = term_to_binary('end'),
      Port ! {self(), {command, EndData}},
      Result = get_result(Port),
      Caller ! {result, Result },

      port_loop(Port);

    stop ->
      Port ! {self(), close},
      receive
        {Port, closed} -> exit(normal)
      end
  end.

get_result(Port) ->
  receive
    {Port, {data, Data}} ->
      {result, Bin} = binary_to_term(Data),
      Bin;
    {'EXIT', Port, Reason} ->
      exit({port_terminated,Reason})
    end.

Version data entries

9 entries across 9 versions & 4 rubygems

Version Path
mojombo-erlectricity-1.0.0 examples/gruff/gruff.erl
mojombo-erlectricity-1.0.1 examples/gruff/gruff.erl
mojombo-erlectricity-1.0.2 examples/gruff/gruff.erl
erlectricity-funbox-1.1.2 examples/gruff/gruff.erl
beambridge-0.9.0 examples/gruff/gruff.erl
erlectricity-1.1.1 examples/gruff/gruff.erl
erlectricity-1.1.0 examples/gruff/gruff.erl
erlectricity-1.0.0 examples/gruff/gruff.erl
erlectricity-1.0.1 examples/gruff/gruff.erl