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

Tee
can someone please explain to me how Enum.reduce works with maps
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement