2018-05-08 09:07:59 -07:00
|
|
|
defmodule POAAgent.Entity.Host.Information do
|
2018-05-09 07:23:23 -07:00
|
|
|
@moduledoc false
|
|
|
|
alias POAAgent.Format.Literal
|
2018-06-13 10:51:01 -07:00
|
|
|
alias POAAgent.Format.POAProtocol.Data
|
2018-07-03 08:50:59 -07:00
|
|
|
alias POAAgent.Entity
|
2018-05-09 07:23:23 -07:00
|
|
|
|
2018-05-08 09:07:59 -07:00
|
|
|
@type t :: %__MODULE__{
|
2018-05-09 07:23:23 -07:00
|
|
|
name: String.t(),
|
|
|
|
contact: URI.t(),
|
|
|
|
coinbase: Literal.Hex.t(),
|
|
|
|
node: String.t(),
|
|
|
|
net: Literal.Decimal.t(),
|
|
|
|
protocol: pos_integer,
|
|
|
|
api: Version.t(),
|
2018-07-18 10:22:50 -07:00
|
|
|
ip: String.t(),
|
2018-05-09 07:23:23 -07:00
|
|
|
port: Literal.Decimal.t(), ## :inet.port_number()
|
|
|
|
os: String.t(),
|
|
|
|
os_v: Version.t(),
|
|
|
|
client: Version.t(),
|
|
|
|
can_update_history?: boolean
|
2018-05-08 09:07:59 -07:00
|
|
|
}
|
2018-05-09 07:23:23 -07:00
|
|
|
|
2018-05-08 09:07:59 -07:00
|
|
|
defstruct [
|
2018-05-09 07:23:23 -07:00
|
|
|
:name,
|
|
|
|
:contact,
|
|
|
|
:coinbase,
|
|
|
|
:node,
|
|
|
|
:net,
|
|
|
|
:protocol,
|
|
|
|
:api,
|
2018-07-18 10:22:50 -07:00
|
|
|
:ip,
|
2018-05-09 07:23:23 -07:00
|
|
|
:port,
|
|
|
|
:os,
|
|
|
|
:os_v,
|
|
|
|
:client,
|
|
|
|
:can_update_history?
|
2018-05-08 09:07:59 -07:00
|
|
|
]
|
2018-05-10 03:16:29 -07:00
|
|
|
|
2018-05-15 07:42:35 -07:00
|
|
|
def new do
|
|
|
|
%__MODULE__{
|
|
|
|
name: nil,
|
|
|
|
contact: nil,
|
|
|
|
coinbase: nil,
|
|
|
|
node: nil,
|
|
|
|
net: nil,
|
|
|
|
protocol: nil,
|
|
|
|
api: "", # this field belongs to JS version, doesn't make sense here
|
2018-07-18 10:22:50 -07:00
|
|
|
ip: ip(),
|
2018-05-15 07:42:35 -07:00
|
|
|
port: "", # TODO
|
|
|
|
os: os(),
|
|
|
|
os_v: os_version(),
|
|
|
|
client: client(),
|
|
|
|
can_update_history?: true
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-07-18 10:22:50 -07:00
|
|
|
defp ip do
|
|
|
|
case :inet.getif() do
|
|
|
|
{:ok, [{ip, _, _} | _]} ->
|
|
|
|
ip
|
|
|
|
|> :inet_parse.ntoa
|
|
|
|
|> List.to_string
|
|
|
|
_ ->
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-15 07:42:35 -07:00
|
|
|
defp os do
|
|
|
|
{_, os} = :os.type()
|
|
|
|
Atom.to_string(os)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp os_version do
|
|
|
|
case :os.version() do
|
|
|
|
{_, _, _} = version ->
|
|
|
|
version
|
|
|
|
|> Tuple.to_list
|
|
|
|
|> Enum.map(&(Integer.to_string(&1)))
|
|
|
|
|> Enum.join(".")
|
|
|
|
version -> version
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp client do
|
|
|
|
{:ok, version} = :application.get_key(:poa_agent, :vsn)
|
|
|
|
List.to_string(version)
|
|
|
|
end
|
|
|
|
|
2018-05-10 03:16:29 -07:00
|
|
|
defimpl POAAgent.Entity.NameConvention do
|
|
|
|
def from_elixir_to_node(x) do
|
|
|
|
mapping = [
|
|
|
|
can_update_history?: :canUpdateHistory
|
|
|
|
]
|
2018-05-11 11:29:53 -07:00
|
|
|
x = Enum.reduce(mapping, x, &POAAgent.Entity.Name.change/2)
|
|
|
|
Map.from_struct(x)
|
2018-05-10 03:16:29 -07:00
|
|
|
end
|
|
|
|
end
|
2018-06-13 10:51:01 -07:00
|
|
|
|
|
|
|
defimpl Data.Format do
|
|
|
|
def to_data(x) do
|
2018-07-03 08:50:59 -07:00
|
|
|
x = Entity.NameConvention.from_elixir_to_node(x)
|
2018-06-13 10:51:01 -07:00
|
|
|
Data.new("information", x)
|
|
|
|
end
|
|
|
|
end
|
2018-05-15 07:42:35 -07:00
|
|
|
end
|