Oberzhang said we were going to introduce SYN https://github.com/ostinelli/syn/.
Having read the document, it does not directly provide methods related to {via, Module, Name}. We need to encapsulate it.
Name can be used for a temporary ID, if necessary, and then adjusted
If there is a callback requirement, it can be done in that module.
Add {: Syn, ' 1.4.0 ' to the Player_server_manager project to run the command to get the dependency
Modify the application method to increase dependency
Do [Applications: [: Logger,: syn], mod: {playerservermanager, []}] end
Then add the Playerregistry module to the project.
defmodule playerregistry dodefregister_name (name, PID) when Is_pid (PID) does Case:syn.register (name, pid) Do:ok-: Yes _-: No end enddefunregister_name (name) do:syn.unregister (name): OK enddefwhereis_name (name) do:syn.find_by_key (name) EnddefSend (name, msg) do case whereis_name (name) do:undefined-exit ({: Badarg, {name, MSG}}) PID-kernel.send PID, msg pid End EndEnd
Reference: The global module is implemented.
Modify Playerserver.start_link as follows
def via_tuple (ID), do: {: Via, playerregistry, id} def Start_link (player) do Genserver.start_link (__module__,%{player:player, socket:nil}, Name:via_tuple (Player.base_ info.id)) End
Playerservermanager.start modified to increase SYN initialization,
def Start (_type, _args) do Import Supervisor.spec, Warn:false : Syn.init () = [ # Define workers and child Supervisors to be supervised worker (Playerserver, [], restart:: temporary), ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other Strategies and supported options opts = [strategy:: Simple_one_for_one, Name: Playerservermanager.supervisor] Supervisor.start_link (children, opts) end
Playerservermanagertest Add test Code
" Player_registry ",%{player:player }doassert {: OK, pid} = Playerservermanager.start_player_ Server (player) assert Playerserver.gem (playerserver.via_tuple (player.base_info.id)) = = 0 End
Test successful, OK. It looks fine at the moment. Distributed registration and conflict handling are left to be tested later.
Writing to this, found that via_tuple to be able to accept the player and Id2 parameters better, convenient point.
Elixir Game Costume Design Four