Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Simulator do
- def main(args) do
- {:ok, super_pid} = PersonManager.start_link(args)
- IO.inspect super_pid, label: "Supervisor to supervise all 100 miners started with pid "
- num_of_persons = 1
- start_persons(num_of_persons)
- IO.inspect PersonManager.count_children, label: "The Persons started in main method are"
- end
- def start_persons(num_of_persons) when num_of_persons >=1 do
- IO.inspect PersonManager.count_children, label: "The number of persons in start_persons"
- IO.inspect num_of_persons, label: "Starting person number"
- {public_key, private_key} = Keypair.generate()
- PersonManager.add_node(private_key, public_key, 0)
- start_persons(num_of_persons-1)
- end
- def start_persons(num_of_persons) when num_of_persons == 0 do
- IO.inspect PersonManager.count_children, label: "Started all persons, returning"
- end
- end
- ===================================================================================
- defmodule PersonManager do
- use DynamicSupervisor
- def start_link(args) do
- DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
- #DynamicSupervisor.start_link(__MODULE__, :ok)
- end
- def init(:ok) do
- DynamicSupervisor.init(strategy: :one_for_one)
- end
- def add_node() do
- IO.puts("Person with wrong Arguments")
- end
- def add_node(private_key, public_key, num_of_coins) do
- IO.inspect public_key, label: "Adding a person with public key"
- child_spec = {Person, {private_key, public_key, num_of_coins}}
- DynamicSupervisor.start_child(__MODULE__, child_spec)
- #IO.inspect person, label: "The name of the person is"
- end
- def remove_node(public_key) do
- DynamicSupervisor.terminate_child(__MODULE__, public_key)
- end
- def children do
- DynamicSupervisor.which_children(__MODULE__)
- end
- def count_children do
- DynamicSupervisor.count_children(__MODULE__)
- end
- end
- =============================================================
- defmodule Person do
- use GenServer
- def init({private_key, public_key, address, num_of_coins}) do
- IO.puts "Starting a person"
- {:ok, %{private_key: private_key, public_key: public_key, num_of_coins: num_of_coins}}
- end
- def start_link({private_key, public_key, address, num_of_coins}) do
- IO.puts "Starting a person"
- GenServer.start_link(
- __MODULE__,
- {private_key, public_key, address, num_of_coins},
- name: {:global, "node:#{public_key}"}
- )
- end
- def get(pid) do
- GenServer.call(pid, :get)
- end
- def handle_call(:get, _from, state) do
- {:reply, {:ok, state}, state}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement