Current section
Files
Jump to
Current section
Files
lib/steemex.ex
defmodule Steemex do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
handler_mod = Application.get_env(:steemex, :handler)
url = Application.get_env(:steemex, :url)
unless is_function(&handler_mod.handle_jsonrpc_call/3), do: throw("Handler module handle_jsonrpc_call is NOT a function")
unless handler_mod, do: throw("Steemex Handler module is NOT configured.")
unless url, do: throw("Steemex WS url is NOT configured.")
children = [
worker(Steemex.IdAgent, []),
worker(Steemex.WS, [&handler_mod.handle_jsonrpc_call/3, url])
]
opts = [strategy: :one_for_one, name: Steemex.Supervisor]
Supervisor.start_link(children, opts)
end
def call(params) do
id = round(:rand.uniform * 1.0e16)
Steemex.IdAgent.put(id, {params, nil})
send_event Steemex.WS, %{jsonrpc: "2.0", params: params, id: id, method: "call"}
end
def call_sync(params) do
# implementation for blocking call goes here, most probably simply by using receive
id = round(:rand.uniform * 1.0e16)
Steemex.IdAgent.put(id, {params, self()})
send_event Steemex.WS, %{jsonrpc: "2.0", params: params, id: id, method: "call"}
response_data = receive do
{:response, {id, params, data}} -> data
end
{:ok, response_data}
end
def get_block(height) do
Steemex.call ["database_api", "get_block", [height]]
end
def get_content(author, permlink) do
Steemex.call ["database_api", "get_content", [author, permlink]]
end
@doc """
Sends an event to the WebSocket server
"""
def send_event(server_pid, msg) do
send server_pid, {:send, msg}
end
end