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

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement