dev-master
9999999-devSymfony 2 Erlang Port Bundle
MIT
The Requires
- php >=5.3.0
- symfony/symfony 2.*
by Yevhen Shyshkin
port erlang
Symfony 2 Erlang Port Bundle
This bundle provides functionality to allow interaction between Erlang programs and Symfony application using Erlang ports. Bundle has library of functions to work with port from the Erlang side and CLI command and port command registry to work with registered port commands from the Symfony side., (*1)
To install bundle you need to add string "ystools/erlang-port-bundle": "dev-master"
to require section
of Symfony's main composer.json and run composer update
., (*2)
There is the library symfony_port.erl that provides list of functions that should be used to work with Symfony port:, (*3)
By default all Erlang files are available in directory erl, but after installation all required files should be copied to other location where they should be compiled and used., (*4)
Configuration of port is stored in file symfony_port_config.hrl that contains following parameters (macroses):, (*5)
php /opt/www/symfony/app/console ystools:erlang-port
);init
;exit
;ok:
;error:
;Bundle provides PortCommandInterface and
AbstractPortCommand that should be used to implement port commands.
Each port command must be registered in DI container as a service with tag ys_tools.erlang_port.port_command
-
tag alias is a command name., (*6)
Main entry point for Erlang is a console command ystools:erlang-port
that should be specified as CLI command
on the Erlang side. This command also can be used to debug existing commands - all you need is to start it
and enter command name and parameters., (*7)
Let's assume that we need to implement port command that will calculate SHA-1 hash of specified string. We need to follow a few steps., (*8)
Port command class must implement PortCommandInterface. Let's use existing AbstractPortCommand that already implements this interface to create a command class:, (*9)
use YsTools\ErlangPortBundle\PortCommand\AbstractPortCommand; class Sha1Calculator extends AbstractPortCommand { /** * @param string $string * @return string */ public function processCommand($string) { return sha1($string); } }
Here processCommand is a default function name used to execute command. If you want to change it you should to override constant CALLBACK_METHOD., (*10)
Now we need to tell that this class should be used as Erlang port command. To do that we need to register it as a service with specific tag, alias will be used as a command name:, (*11)
parameters: acme.demo.port_command.sha1_calculator.class: Acme\DemoBundle\PortCommand\Sha1Calculator services: acme.demo.port_command.sha1_calculator: class: %acme.demo.port_command.sha1_calculator.class% tags: - { name: ys_tools.erlang_port.port_command, alias: calculate_sha1 }
Let's check that command is available and works. To do that we can manually start port command and enter required data:, (*12)
login@host:/opt/www/symfony$ app/console ystools:erlang-port calculate_sha1 qwerty ok:b1b3773a05c0ed0176787a4f1574ff0075f7521e exit ok:exit
As you can see, command returns result with a response status prefix. Command exit
is used to stop port process., (*13)
To work with Erlang code we need to compile existing library. Also bundle provides library
symfony_port_demo.erl that allow us to test command without additional Erlang code.
Let's compile libraries symfony_port
and symfony_port_demo
:, (*14)
login@host:/opt/www/symfony/src/YsTools/ErlangPortBundle/erl$ erl Eshell V5.10.3 (abort with ^G) 1> c(symfony_port). {ok,symfony_port} 2> c(symfony_port_demo). {ok,symfony_port_demo}
After compilation for each library there must be appeared file with extension *.beam - these are compiled libraries., (*15)
Library symfony_port_demo
provides function execute(Name, Parameters) that automatically opens port,
executes a command Name with Parameters, prints result and closes port. Let's do it:, (*16)
login@host:/opt/www/symfony/src/YsTools/ErlangPortBundle/erl$ erl Eshell V5.10.3 (abort with ^G) 1> symfony_port_demo:execute("calculate_sha1", ["qwerty"]). b1b3773a05c0ed0176787a4f1574ff0075f7521e stop
As you can see, port command was successfully executed and it returned valid result., (*17)
Symfony 2 Erlang Port Bundle
MIT
port erlang