https://github.com/selectel/alberto
A simple OCaml library for writing Erlang port drivers
https://github.com/selectel/alberto
community
Last synced: about 1 year ago
JSON representation
A simple OCaml library for writing Erlang port drivers
- Host: GitHub
- URL: https://github.com/selectel/alberto
- Owner: selectel
- License: other
- Created: 2013-02-12T08:06:43.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2020-04-12T13:01:01.000Z (about 6 years ago)
- Last Synced: 2025-04-30T16:17:45.572Z (about 1 year ago)
- Topics: community
- Language: OCaml
- Homepage:
- Size: 109 KB
- Stars: 43
- Watchers: 24
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
README
```
__
/ \
/ ..|\
(_\ |_)
/ \@' Alberto!
/ \ 0.5
_ _/ ` |
\\/ \ | _\
\ /_ || \\_
\____)|_) \_)
```
Alberto is an implementation of
[Erlang External Term Format](http://erlang.org/doc/apps/erts/erl_ext_dist.html),
a protocol, used by Erlang nodes to communicate with so called *ports*. See
Erlang [documentation](http://www.erlang.org/doc/tutorial/c_port.html) for
details.
Installation
------------
The simplest way to get `alberto` is to use [`OPAM`](http://opam.ocamlpro.com):
```bash
opam install alberto
```
Example
-------
On Erlang part we have a simple echo server, which sends `<<"Hello world!">>`
to the port and echoes whatever comes back to stdout:
```erlang
#!/usr/bin/env escript
%%! -noshell -noinput
-mode(compile).
-define(ME, filename:basename(escript:script_name())).
-define(PRINT(STR, PARAMS), io:format("~s: " ++ STR ++ "~n", [?ME | PARAMS])).
main([Bin]) ->
Port = open_port({spawn, Bin},
[binary, {packet, 4}]),
port_command(Port, term_to_binary("Hello world!")),
receive
{_, {data, Data}} ->
?PRINT("~p", [binary_to_term(Data)]),
main([Bin])
end;
main(_) ->
io:format("~s", ["usage: echo.erl path/to/port"]).
```
OCaml part is even more consice:
```ocaml
Alberto.interact (fun x -> x)
```
Okay, let's see it in action (sources are available in `examples/` directory):
```bash
$ dune build examples/port_simple.exe
$ examples/echo.erl ./_build/default/examples/port_simple.exe
echo.erl: "Hello world!"
echo.erl: "Hello world!"
echo.erl: "Hello world!"
echo.erl: "Hello world!"
```