User Tools

Site Tools


cs330_f2016:lab13scaffold

This is an old revision of the document!


module.exs
defmodule Database do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :Database, self())
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    #check Depends
    {:reply, :ok, state}
  end
 
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
end
 
 
 
 
defmodule CustomerService do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :CustomerService, self())
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    #check Depends
    {:reply, :ok, state}
  end
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
end
 
 
defmodule Info do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :Info, self())
    send self(), {:start, ns}
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    if ! check_alive(state) do
      {:stop, "dependency not found", "dependency not found", state}
    end
    {:reply, :ok, state}
  end
 
  def check_alive(processes) do
    x = Enumerable.map(processes, &Process.alive/0)
    reducer =  fn l, r -> l and r end
    Enumberable.reduce(x, true, reducer)
  end
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info({:start, ns}, _state) do
    :timer.sleep(500)
    case NameServer.resolve(ns, :Database) do
      :error -> {:stop, "No Database Found", []}
      x -> {:noreply, [x]}
    end
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
end
 
defmodule Order do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :Order, self())
    send self(), {:start, ns}
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    if ! check_alive(state) do
      {:stop, "dependency not found", "dependency not found", state}
    end
    {:reply, :ok, state}
  end
 
  def check_alive(processes) do
    x = Enumerable.map(processes, &Process.alive/0)
    reducer = fn l, r -> l and r end
    Enumberable.reduce(x, true, reducer)
  end
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info({:start, ns}, _state) do
    :timer.sleep(500)
    case NameServer.resolve(ns, :Database) do
      :error -> {:stop, "No Database Found", []}
      x ->
        case NameServer.resolve(ns, :User) do
          :error -> {:stop, "User Module not Found", []}
          y -> {:noreply, [x, y]}
        end
    end
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
end
 
defmodule Shipper do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :Shipper, self())
    send self(), {:start, ns}
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    if ! check_alive(state) do
      {:stop, "dependency not found", "dependency not found", state}
    end
    {:reply, :ok, state}
  end
 
  def check_alive(processes) do
    x = Enumerable.map(processes, &Process.alive/0)
    reducer = fn l, r -> l and r end
    Enumberable.reduce(x, true, reducer)
  end
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info({:start, ns}, _state) do
    :timer.sleep(500)
    case NameServer.resolve(ns, :Database) do
      :error -> {:stop, "No Database Found", []}
      x ->{:noreply, [x]}
 
    end
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
end
 
defmodule User do
  use GenServer
 
  def start_link(name_server, opts \\ []) do
    GenServer.start_link(__MODULE__, name_server, [])
  end
 
  def start(name_server) do
    GenServer.start(__MODULE__, name_server,  [])
  end
 
  def call(mod) do
    GenServer.call(mod, "Message")
  end
 
  def kill(mod) do
    Process.exit(mod, "killed")
  end
 
  def init(ns) do
    # DB has no Depends
    IO.puts Atom.to_string(__MODULE__) <> " has started"
    NameServer.register(ns, :User, self())
    send self(), {:start, ns}
    {:ok, []}
  end
 
  def handle_call(_request, _From, state) do
    if ! check_alive(state) do
      {:stop, "dependency not found", "dependency not found", state}
    end
    {:reply, :ok, state}
  end
 
  def check_alive(processes) do
    x = Enumerable.map(processes, &Process.alive/0)
    reducer = fn l, r -> l and r end
    Enumberable.reduce(x, true, reducer)
  end
 
  def handle_cast(_request, state) do
    #check Depends (none)
    {:noreply, state}
  end
 
  def handle_info({:start, ns}, _state) do
    :timer.sleep(500)
    case NameServer.resolve(ns, :Database) do
      :error -> {:stop, "No Database Found", []}
      x ->
        case NameServer.resolve(ns, :Order) do
          :error -> {:stop, "User Module not Found", []}
          y -> {:noreply, [x, y]}
        end
    end
  end
 
  def handle_info(_, state) do
    #checkDepends (none)
    {:noreply, state}
  end
 
end
 
defmodule Crasher do
 
  def crash(ns, name) do
    IO.puts("Crashing the module...")
    pid = GenServer.call(ns, {:resolve, name})
    Process.exit(pid, :kill)
  end
 
end
cs330_f2016/lab13scaffold.1490226924.txt.gz · Last modified: 2021/06/30 23:40 (external edit)