5

Francesco Cesarini's "Erlang Programming" book provides a nice and simple-to-start-with example of connecting Erlang to Ruby (implemented through ports):

module(test.erl).
compile(export_all).    

test() ->
    Cmd = "ruby echoFac.rb",
    Port = open_port({spawn, Cmd}, [{packet, 4}, use_stdio, exit_status, binary]),
    Payload = term_to_binary({fac, list_to_binary(integer_to_list(23))}),
    port_command(Port, Payload),
    receive
     {Port, {data, Data}} ->
      {result, Text} = binary_to_term(Data),
      Blah = binary_to_list(Text),
      io:format("~p~n", [Blah])
    end.

However, the Ruby code used in this example uses the Erlictricity library, which does all the low-level things for the programmer:

require 'rubygems'
require 'erlectricity'
require 'stringio'
def fac n
if (n<=0) then 1 else n*(fac (n-1)) end
end
receive do |f|
f.when(:fac, String) do |text|
n = text.to_i
f.send!(:result, "#{n}!=#{(fac n)}")
f.receive_loop
end
end

I have tried to use this slightly modified test.erl code:

test(Param) ->
        Cmd = "./add",
        Port = open_port({spawn, Cmd}, [{packet, 4}, use_stdio, exit_status, binary]),
        Payload = term_to_binary({main, list_to_binary(integer_to_list(Param))}),
...

to speak with a very simple C file:

/* add.c */
#include <stdio.h>
int main(int x) {
 // return x+1;
 printf("%i\n",x+1);
}

But unfortunately the receive loop in test.erl gets a message {#Port<0.2028>,{exit_status,2}}

My question is: is it possible to implement anything similar in C/C++? Are there any ready-made libraries for Erlang to talk with C/C++ via ports similar to Erlictricity for Ruby?

skanatek
  • 5,133
  • 3
  • 47
  • 75
  • A crucial point is what sort of data you want to pass between Erlang and C. Just strings (or binary blobs in general), integers or floating-point numbers? Or do you want to send structured data, like arbitrary Erlang tuples, to the C side? And what should the C code return to Erlang - just a status code, a string, integer or float, or is it supposed to produce some compound data structure? – RichardC Oct 08 '11 at 16:27
  • It can be lists of integers and floats or lists of tuples (holding integer and float elements) that I want to pass from Erlang to C. C side should return lists of integers or lists of tuples with integer elements. – skanatek Oct 08 '11 at 21:31
  • In that case, you don't really need the help (and overhead) of any library. Just design a very simple protocol and send raw bytes back and forth. – RichardC Oct 08 '11 at 23:04
  • Dear RichardC, thanks! Unfortunately my knowledge of communication between languages on Unix (and on any OS actually) is very limited. Could you please reference me somewhere where I can read about this language-to-language communication? The only two things that I know now is that (a) with an ad-hoc protocol I define the form of the passed data on the both sides and (b) I could probably call shell commands from Erlang with stringified parameters (like "./add X", where X comes from an Erlang variable). – skanatek Oct 09 '11 at 19:10
  • RichardC, could you please post your last comment as an answer? I will mark it as accepted. – skanatek Oct 12 '11 at 09:03

2 Answers2

1

Start by reading the Interoperability Tutorial in the Erlang/OTP online documentation: http://erlang.org/doc/tutorial/users_guide.html. When communicating with a C program, you just write the C code to read from stdin and write to stdout, and this will be hooked up to the Erlang port. You could also read chapter 12 in http://manning.com/logan.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
RichardC
  • 10,412
  • 1
  • 23
  • 24
0

Have you checked out Erl Interface here: http://www.erlang.org/doc/tutorial/erl_interface.html ?
Other interesting links i have found are listed below:

http://www.erlang.org/documentation/doc-4.9.1/pdf/erl_interface-3.2.pdf
http://www.erlang.org/doc/apps/erl_interface/index.html
http://dukesoferl.blogspot.com/2010/01/minor-erlang-interface-tricks.html

I hope those will help :)

Muzaaya Joshua
  • 7,736
  • 3
  • 47
  • 86
  • I have read it and I have to admit that I was unable to make the example work (on the other side the Ruby example is much simpler). If it is the only option, then I have to devote it more time :) – skanatek Oct 08 '11 at 12:29