Skip to content

Instantly share code, notes, and snippets.

@cigzigwon
Last active October 18, 2018 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cigzigwon/53788192ea48bfe2c8fbca30bedde7c0 to your computer and use it in GitHub Desktop.
Save cigzigwon/53788192ea48bfe2c8fbca30bedde7c0 to your computer and use it in GitHub Desktop.
defmodule Hive.Seeker do
require IO
require Logger
@cluster "hive"
@label "[Seeker]:"
def discover() do
Hive.Colony.start_link([name: :settings])
if Node.alive? === true do
ips = []
Task.Supervisor.start_child(Hive.TaskSupervisor, fn -> boot(1, ips) end)
else
Logger.info "#{@label} Single-node discovery mode"
end
end
defp boot(n, ips) do
hostent = :inet.gethostbyname(:"#{@cluster}#{n}.local")
if elem(hostent, 0) !== :error do
{:ok, hostent} = hostent
Logger.info "#{@label} #{n} nodes found"
ips =
if Node.self !== :"hive@#{@cluster}#{n}.local" do
ips ++ elem(hostent, 5)
else
ips
end
boot(n + 1, ips)
else
Hive.Colony.create(:settings, "device_ips")
{:ok, colony} = Hive.Colony.lookup(:settings, "device_ips")
Hive.Cell.put(colony, 1, ips)
Logger.info "#{@label} end discovery after #{n - 1} nodes found"
end
end
end
@cigzigwon
Copy link
Author

This is a simple TCP Elixir based supervisor service to discover all devices on a subnet which have the correct EPMD based cookie. The Hive.Colony module is just a simple ETS based registry to store data in memory. If a device is rebooted it should be discovered again. I use this service to relay data via websockets to each device and so that it can also talk to real-time web and native applications. I hope you like that it is a short and sweet solution compared to something like EPMDless discovery or Nerves.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment