tres/controller: Add handler pid register (#11)
This commit is contained in:
parent
c7c0e168b5
commit
910c6ee255
6 changed files with 35 additions and 7 deletions
|
|
@ -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)
|
||||
]
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue