jonasbhjulstad

jonasbhjulstad

Bringing child nodes up on network

Hi!
I’ve been able to setup child nodes, but I’m trying to get them online. The intention is to make the nodes act as separate computers, such that I can broadcast messages to them via udp.

Code I’ve been using for parent node:

Node.start(:master@localhost, :shortnames)

(When running :longnames, I get a name error when initializing child nodes)

Code for initializing child nodes:

defmodule Child_Nodes do

  def init child_names do
    :os.cmd('epmd -daemon')
    for child_name <- child_names do
      {:ok, node} = start_child child_name
      load_code node
    end
  end

  def project_module_path do
    Path.absname('_build/dev/ebin') |>
    to_charlist()
  end

  def start_child name do
    :slave.start(:localhost, name)
  end

  def load_code node do
    :rpc.block_call(node, :code, :add_paths, [:code.get_path()])
    :rpc.block_call(node, :code, :add_paths, [project_module_path()])
  end
end

This allows me to run my projects code on the child_nodes and access them using Node.ping(). However, each node are going to find eachother over udp using this module, with different ports:

defmodule Broadcasting do

  @broadcast_timeout 1000

  @localhost {10, 0, 0, 68}

  #Node-name format: OrderHandler_<port>@<address>

  def init port do

    {:ok, socket} = :gen_udp.open(port, [active: true, broadcast: true])

    Task.async fn -> broadcast socket, port end

    IO.puts "Node at port #{port} initialized"

    listen socket, port

  end

  def broadcast socket, port do

    :gen_udp.send(socket, {255,255,255,255}, port, to_string(port))

    :timer.sleep(@broadcast_timeout)

    broadcast socket, port

  end

  def listen socket, port do

    local_msg = to_string(port)

    receive do

      {:udp, _socket, _address, port, ^local_msg} ->

        IO.inspect port

        IO.puts "localmsg"

        listen socket, port

      {:udp, _socket, address, remote_port, _msg} ->

        string_address = ip_to_string(address)

        IO.puts "new node added at port: #{port}"

        #"OrderHandler_" <> to_string(remote_port) <> "@" <> string_address |> String.to_atom |> Node.ping

        #localhost-version

        "OrderHandler_" <> to_string(remote_port) <> "@localhost" |> String.to_atom |> Node.ping

      end

  end

  def ip_to_string ip do

    :inet.ntoa(ip) |> to_string()

  end

end

Is it possible to bring all child nodes online?

First Post!

ityonemo

ityonemo

What is going to call this broadcasting module? A task? I think this is a place you should use gen_servers.

Also, the nodes have to be on the same layer-2 network that supports udp broadcasting and most major cloud providers don’t support this.

You might need to give beam.smp special setuid permissions (CAP_NET_RAW iirc) for it to be able to broadcast.

Finally you may run out of udp connections if you’re using nf_conntrack on Ubuntu 18.04.

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement