tres/controller: Add handler pid register (#11)

This commit is contained in:
Eishun Kondoh 2018-10-06 00:01:17 +09:00 committed by GitHub
parent c7c0e168b5
commit 910c6ee255
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 7 deletions

View file

@ -4,12 +4,14 @@ defmodule Tres.Application do
use Application
alias Tres.SwitchRegistry
alias Tres.HandlerRegistry
def start(_type, _args) do
import Supervisor.Spec
children = [
worker(Registry, [[keys: :unique, name: SwitchRegistry]], id: SwitchRegistry),
worker(Registry, [[keys: :unique, name: HandlerRegistry]], id: HandlerRegistry),
supervisor(Tres.MessageHandlerSup, [], id: MessageHandlerSup),
supervisor(OVSDB, [], id: OVSDB)
]

View file

@ -3,6 +3,7 @@ defmodule Tres.Controller do
quote location: :keep do
import Tres.SwitchRegistry,
only: [
lookup_handler_pid: 1,
send_message: 2,
send_message: 3,
blocking_send_message: 2,

View file

@ -19,7 +19,9 @@ defmodule Tres.MessageHandlerSup do
def start_child(dpid) do
{cb_mod, _cb_args} = Tres.Utils.get_callback_module()
child_spec = cb_mod.handler_spec(dpid)
Supervisor.start_child(__MODULE__, child_spec)
{:ok, pid} = Supervisor.start_child(__MODULE__, child_spec)
:ok = Tres.SwitchRegistry.register_handler_pid(dpid, pid)
{:ok, pid}
end
def terminate_child(dpid) do

View file

@ -609,19 +609,19 @@ defmodule Tres.SecureChannel do
defp close_connection(:handler_error, state_data) do
debug("connection terminated: Got handler error")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :handler_error})
{:stop, :normal, %{state_data | socket: nil}}
end
defp close_connection(:ping_failed, state_data) do
debug("connection terminated: Exceeded to max_ping_fail_count")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :ping_failed})
{:stop, :normal, %{state_data | socket: nil}}
end
defp close_connection({:main_closed, reason}, state_data) do
debug("connection terminated: Main connection down by #{inspect(reason)}")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :main_closed})
{:stop, :normal, %{state_data | socket: nil}}
end
@ -632,19 +632,19 @@ defmodule Tres.SecureChannel do
defp close_connection({:trap_detected, reason}, state_data) do
debug("connection terminated: Trapped by #{inspect(reason)}")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :trap_detected})
{:stop, :normal, %{state_data | socket: nil}}
end
defp close_connection(:tcp_closed, state_data) do
debug("connection terminated: TCP Closed by peer")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :tcp_closed})
{:stop, :normal, %{state_data | socket: nil}}
end
defp close_connection({:tcp_error, reason}, state_data) do
debug("connection terminated: TCP Error occured: #{inspect(reason)}")
Tres.MessageHandlerSup.terminate_child({state_data.datapath_id, state_data.aux_id})
_ = send(state_data.handler_pid, {:switch_disconnected, :tcp_error})
{:stop, :normal, %{state_data | socket: nil}}
end
end

View file

@ -3,6 +3,29 @@ defmodule Tres.SwitchRegistry do
Dispatcher
"""
# For DatapathHandler
def register_handler_pid({_dpid, _aux_id} = datapath_id, pid) do
case Registry.register(Tres.HandlerRegistry, datapath_id, pid) do
{:ok, _owner} ->
:ok
{:error, {:already_registered, _owner}} ->
:ok
end
end
def lookup_handler_pid({_dpid, _aux_id} = datapath_id) do
case Registry.lookup(Tres.HandlerRegistry, datapath_id) do
[{_owner, pid}] -> pid
[] -> nil
end
end
def lookup_handler_pid(datapath_id) when is_binary(datapath_id),
do: lookup_handler({datapath_id, 0})
# For Datapath
def register({_dpid, _aux_id} = datapath_id) do
{:ok, _} = Registry.register(__MODULE__, datapath_id, [])
end