[Erl_question08] How does Erlang build a application?

Source: Internet
Author: User

Questions:

When we put all the supervision tree in a project through a simple function game:start (), we find that the tree structure is particularly complex, with only one root node, and then it expands.

Here, this is why application appears, designing a sub-block (application) that can be switched on at any time. For example: In the Log app, DB app, game app, connect app.

So the switch management of these applications is very convenient, "Imagine if you use supervisor, the runtime also to manually stop the process tree, and then also to remove the monitoring tree, but also to do clean work, the next start to do the start work ...", these definitions of good application, Nature will treat this as a unit! This is probably the embodiment of the idea of programming.

How to construct a typical Erlang application? Here we try to do the graphical programming of [Erl_question07] Erlang: pure Erlang to do 2048 games instead of the Application launcher for demonstration

The original through the Erl Script start is possible, to become application what are the benefits?

That can is started and stopped as a unit, and which can be re-used on other systems as well. Use application convenient at any time only to start or close the application unit of the application, the other application completely unaffected, which can easily manage the various functions of a large project, these functions are divided into small applications, but also a level and convenient management.

Steps:

1. Define the. app file in the following format

%%Game2048.app{application, game2048, [{description,"Pure Erlang game 2048 for Fun"},%%Description Application Note, default to ""{VSN, "1"},%%Version number{modules, []},%%all modules introduced by this application,systools uses this list to generate the boot script and tar file tar, which must only be one and only one APPL Ication defined.{registerd,[]},%%All names of registered processes in the application. SysTools uses this list to detect name clashes between Applicati Ons. Defaults to [].{applications, [kernel, Stdlib]},%%All applications which must are started before this application are started. SysTools uses this list to generate correct Boot scripts. Defaults to [], but note this all applications has dependencies to at least kernel and stdlib.{mod, {game2048_app, []}},%%Call Game2048_app:start (Normal, []), Game2048_app:stop ([]){env, []}]}.

2. Add the monitoring process to the game: Game2048_sup.erl

= One_for_one    ,= +,    = 3600,    = {restartstrategy,     Maxrestarts, maxsecondsbetweenrestarts},    = Permanent,    =     , = Worker,    percent of the Monitor is Game2048:start ()        Restart, Shutdown, Type, []},    {OK, {supflags, [Achild]}}.

3. The original Game2048:start () return value is changed to {ok,pid} mode, which is required by the Supervisor specification.

4. Recompile the code and transform the application work to completion.

You can start the application client in the following ways.

1. Start an Erlang shell:

Erl-name test-pa "app directory"-PA "Ebin directory"    >application:start (game2048).
>application:stop (game2048).

2. Of course you can put application and other application common use, "soon I will put lager application also used here, overqualified to learn good code or better:)"

Become application, good happy, actually see and kernel and together, is not advanced point "use Observer:start ()." View:

Tip: When you start a shell with Erl, the Net_kernel module is not started, resulting in a distributed error, and if you add –name, the specified node name will start.

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.