Elixir Game Costume Design Four

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.