Compare commits

..

2 commits

7 changed files with 65 additions and 8 deletions

View file

@ -11,11 +11,13 @@ defmodule Scopes.CSys.Application do
def start(type, args) do def start(type, args) do
IO.puts("Hello CSys") IO.puts("Hello CSys")
Logger.info(Util.show [type, args]) Logger.info(Util.show [type, args])
# input_neurons = Environ.create_input
IO.inspect Scopes.CSys.Server.Endpoint.child_spec(input: %{[:csys, :s01] => "dummy"})
children = [ children = [
#Scopes.Web.Server.Endpoint #Scopes.CSys.Server.Endpoint
Scopes.CSys.Server.Endpoint {Scopes.CSys.Server.Endpoint, input: %{[:csys, :s01] => "dummy"}}
] ]
opts = [strategy: :one_for_one, name: Scopes.Supervisor] opts = [strategy: :one_for_one, name: Scopes.CSys.Supervisor]
Supervisor.start_link(children, opts) Supervisor.start_link(children, opts)
end end
end end

View file

@ -1,6 +1,12 @@
defmodule Scopes.CSys.Server.Endpoint do defmodule Scopes.CSys.Server.Endpoint do
use Phoenix.Endpoint, otp_app: :scopes_csys use Phoenix.Endpoint, otp_app: :scopes_csys
def init(arg) do
IO.inspect(arg)
IO.inspect(Process.get())
:ignore
end
plug Plug.Static, plug Plug.Static,
at: "/", at: "/",
from: :scopes_csys, from: :scopes_csys,
@ -15,5 +21,6 @@ defmodule Scopes.CSys.Server.Endpoint do
pass: ["*/*"], pass: ["*/*"],
json_decoder: Phoenix.json_library() json_decoder: Phoenix.json_library()
plug Scopes.Web.Server.Router plug Scopes.CSys.Server.Router
#input_neurons: @input
end end

View file

@ -0,0 +1,23 @@
defmodule Scopes.CSys.Server.Router do
use Scopes.Web.Server, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", Scopes.CSys.Server do
pipe_through :api
match :*, "/csys/:action/s01/:item", Controller, :index
end
end
defmodule Scopes.CSys.Server.Controller do
use Scopes.Web.Server, :controller
def index(conn, params) do
# input_neuron = Environ.get_input(:csys, :s01)
json(conn, params)
#render(conn, :index, message: "Hello CSys")
end
end

View file

@ -10,6 +10,9 @@ defmodule Scopes.Core.Actor do
bhv.(msg) bhv.(msg)
loop(bhv) loop(bhv)
{:become, bhv_n} -> loop(bhv_n) {:become, bhv_n} -> loop(bhv_n)
{:register, reg, name, value} ->
Registry.register(reg, name, value)
loop(bhv)
{:quit} -> nil {:quit} -> nil
end end
end end
@ -19,6 +22,12 @@ defmodule Scopes.Core.Actor do
spawn_link(fn -> loop(bhv) end) spawn_link(fn -> loop(bhv) end)
end end
def create(bhv, reg, name, value \\ []) do
spawn_link(fn ->
Registry.register(reg, name, value)
loop(bhv) end)
end
def send(ac, msg, delay \\ 0) do def send(ac, msg, delay \\ 0) do
if msg do if msg do
Process.sleep(delay) Process.sleep(delay)
@ -30,6 +39,10 @@ defmodule Scopes.Core.Actor do
Kernel.send(ac, {:become, bhv}) Kernel.send(ac, {:become, bhv})
end end
def register(ac, reg, name, value \\ []) do
Kernel.send(ac, {:register, reg, name, value})
end
def stop(ac) do def stop(ac) do
Kernel.send(ac, {:quit}) Kernel.send(ac, {:quit})
end end

View file

@ -19,8 +19,8 @@ defmodule Scopes.Web.Server.ErrorJSON do
# By default, Phoenix returns the status message from # By default, Phoenix returns the status message from
# the template name. For example, "404.json" becomes # the template name. For example, "404.json" becomes
# "Not Found". # "Not Found".
def render(template, assigns) do def render(template, resp) do
message = assigns.reason.message message = resp.reason.message
Logger.warning(Util.show [message]) Logger.warning(Util.show [message])
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}} %{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end end

View file

@ -17,8 +17,11 @@ defmodule Scopes.MixProject do
# Run "mix help compile.app" to learn about applications. # Run "mix help compile.app" to learn about applications.
def application do def application do
[ [
#mod: {Scopes.Application, []}, mod: {Scopes.Application, []},
extra_applications: [:logger, :runtime_tools] extra_applications: [:logger, :runtime_tools],
env: [
serve_endpoints: false
]
] ]
end end

View file

@ -4,10 +4,19 @@ defmodule Scopes.CoreActorTest do
describe "actors:" do describe "actors:" do
test "basic-life-cycle" do test "basic-life-cycle" do
Registry.start_link(keys: :unique, name: Actors)
this = self() this = self()
ac = Actor.create(fn msg -> send(this, msg) end) ac = Actor.create(fn msg -> send(this, msg) end)
Actor.register(ac, Actors, :demo, [])
# or: ac = Actor.create(fn msg -> send(this, msg) end, Actors, :demo)
Actor.send(ac, "Hello Actor!") Actor.send(ac, "Hello Actor!")
assert_receive "Hello Actor!" assert_receive "Hello Actor!"
[{pid, _value}] = Registry.lookup(Actors, :demo)
assert pid == ac
Actor.become(ac, fn _msg -> send(this, "Goodbye") end) Actor.become(ac, fn _msg -> send(this, "Goodbye") end)
Actor.send(ac, "Hello Actor!") Actor.send(ac, "Hello Actor!")
Actor.stop(ac) Actor.stop(ac)