Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. defmodule Simulator do
  2.  
  3. def main(args) do
  4.  
  5.  
  6. {:ok, super_pid} = PersonManager.start_link(args)
  7. IO.inspect super_pid, label: "Supervisor to supervise all 100 miners started with pid "
  8. num_of_persons = 1
  9. start_persons(num_of_persons)
  10. IO.inspect PersonManager.count_children, label: "The Persons started in main method are"
  11.  
  12. end
  13.  
  14. def start_persons(num_of_persons) when num_of_persons >=1 do
  15. IO.inspect PersonManager.count_children, label: "The number of persons in start_persons"
  16. IO.inspect num_of_persons, label: "Starting person number"
  17. {public_key, private_key} = Keypair.generate()
  18. PersonManager.add_node(private_key, public_key, 0)
  19. start_persons(num_of_persons-1)
  20.  
  21. end
  22.  
  23. def start_persons(num_of_persons) when num_of_persons == 0 do
  24.  
  25. IO.inspect PersonManager.count_children, label: "Started all persons, returning"
  26.  
  27. end
  28.  
  29. end
  30.  
  31. ===================================================================================
  32.  
  33. defmodule PersonManager do
  34. use DynamicSupervisor
  35.  
  36. def start_link(args) do
  37. DynamicSupervisor.start_link(__MODULE__, :ok, name: __MODULE__)
  38. #DynamicSupervisor.start_link(__MODULE__, :ok)
  39. end
  40.  
  41. def init(:ok) do
  42. DynamicSupervisor.init(strategy: :one_for_one)
  43. end
  44.  
  45. def add_node() do
  46. IO.puts("Person with wrong Arguments")
  47. end
  48.  
  49. def add_node(private_key, public_key, num_of_coins) do
  50. IO.inspect public_key, label: "Adding a person with public key"
  51. child_spec = {Person, {private_key, public_key, num_of_coins}}
  52. DynamicSupervisor.start_child(__MODULE__, child_spec)
  53. #IO.inspect person, label: "The name of the person is"
  54.  
  55. end
  56.  
  57. def remove_node(public_key) do
  58. DynamicSupervisor.terminate_child(__MODULE__, public_key)
  59. end
  60.  
  61. def children do
  62. DynamicSupervisor.which_children(__MODULE__)
  63. end
  64.  
  65. def count_children do
  66. DynamicSupervisor.count_children(__MODULE__)
  67. end
  68. end
  69.  
  70. =============================================================
  71.  
  72.  
  73. defmodule Person do
  74. use GenServer
  75.  
  76. def init({private_key, public_key, address, num_of_coins}) do
  77. IO.puts "Starting a person"
  78. {:ok, %{private_key: private_key, public_key: public_key, num_of_coins: num_of_coins}}
  79. end
  80.  
  81. def start_link({private_key, public_key, address, num_of_coins}) do
  82. IO.puts "Starting a person"
  83. GenServer.start_link(
  84. __MODULE__,
  85. {private_key, public_key, address, num_of_coins},
  86. name: {:global, "node:#{public_key}"}
  87. )
  88. end
  89.  
  90. def get(pid) do
  91. GenServer.call(pid, :get)
  92. end
  93.  
  94. def handle_call(:get, _from, state) do
  95. {:reply, {:ok, state}, state}
  96. end
  97.  
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement