erlang-ffi: Send messages to an Erlang node using Haskell

[ foreign, library ] [ Propose Tags ] [ Report a vulnerability ]

Please see the README on GitHub at https://github.com/Szetty/erlang-ffi#readme


[Skip to Readme]

Modules

  • Foreign
    • Foreign.Erlang
      • Foreign.Erlang.Network
      • Foreign.Erlang.OTP
        • Foreign.Erlang.OTP.GenServer
        • Foreign.Erlang.OTP.Mnesia
      • Foreign.Erlang.Processes
      • Foreign.Erlang.Types
      • Foreign.Erlang.Utilities

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 1.0.0
Change log ChangeLog.md
Dependencies base (>=4.7 && <5), binary (>=0.2 && <0.9), bytestring (>=0.10 && <=0.11.1), directory (>=1.0 && <=1.3.6.1), filepath (>=1.0 && <=1.4.2.1), MissingH (>=1.0 && <=1.4.3.0), network (==3.1.2.1), random (>=1.0 && <=1.2) [details]
License LicenseRef-GPL
Copyright 2021 Arnold Szederjesi-Dragomir
Author Arnold Szederjesi-Dragomir
Maintainer szederjesiarnold@gmail.com
Category Foreign
Home page https://github.com/Szetty/erlang-ffi#readme
Bug tracker https://github.com/Szetty/erlang-ffi/issues
Source repo head: git clone https://github.com/Szetty/erlang-ffi
Uploaded by Szetty at 2021-03-18T17:37:12Z
Distributions
Downloads 204 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2021-03-18 [all 2 reports]

Readme for erlang-ffi-1.0.0

[back to package description]

IT IS A MODIFIED VERSION OF THE ORIGINAL erlang-ffi TO MAKE IT WORK WITH NEW AND MODERN STACK.

erlang-ffi

Should work with with OTP version < 23 (have tried only with OTP 22 and 23).

Speaks the Erlang network protocol and impersonates an Erlang node on the network. Fully capable of bi-directional communication with Erlang. Erlang types are, as far as reasonable, mapped to Haskell types. Messages to Erlang are just function calls in Haskell, and messages from Erlang are delivered to MVars.

Short example

We introduce the erlang library through a simple working example. We are going to write a simple ''Hello, Erlang!'' program.

Lets start with the Haskell code. Here we initialize the ffi with createSelf, and create a mailbox with createMBox. Then we send our greeting with our pid to a registered process p running on Erlang node erl@127.0.0.1. We expect an answer, so we get it from the mailbox and print it out to the standard output.

import Foreign.Erlang

main :: IO ()
main = do
  self <- createSelf "haskell@localhost"
  mbox <- createMBox self
  mboxSend mbox (Long "erl" "127.0.0.1") (Right "p") (mboxSelf mbox, "Hello, Erlang!")
  answer <- mboxRecv mbox
  putStrLn ("Received: " ++ fromErlang answer)

Alright, that wasn't so hard! Lets continue with the Erlang side. We are not going to do any fancy thing here neither. We register a process that receives a message and replies to it.

-module(hello).
-export([main/0]).

main() ->
    P = spawn(fun f/0),
    register(p, P).

f() ->
    receive
        {Hs, Msg} ->
            io:format("Received ~p~n", [Msg]),
            Hs ! "Hello, Haskell!"
    end.

Lets start up the engines! We are ready to compile and run our programs.

$ ghc Hello.hs
[1 of 1] Compiling Main             ( Hello.hs, Hello.o )
Linking Hello ...
$ erlc hello.erl

Firstly, we start an Erlang node:

$ erl -name "erl@127.0.0.1"
Eshell V6.2  (abort with ^G)
(erl@127.0.0.1)1> l(hello).
{module,hello}
(erl@127.0.0.1)2> hello:main().
true

Erlang node is started, and the process is registered as well. Now start the Haskell program:

./Hello 
Received: Hello, Haskell!

Taking a look at the Erlang shell, we see that the message is successfully delivered:

(erl@127.0.0.1)3> Received "Hello, Erlang!"