How to send and receive messages using ERTS.

Torbjorn Tornkvist tobbe@REDACTED
Wed May 26 05:00:35 CEST 1999


Hi ! 

I assume you have read the erl_interface User's Manual (and the
Reference Manuals) ? If not, here are some examples:

> 1) Where I can find a sample, how to handle ErlMessage and receive
> information from them. As sample, if I'm expecting to receive something
> like {pid, atom, number, sting}, how to extract pid, atom, number and
> string from the ErlMessage. 

     ETERM *arr[2], *msg;
     int sockfd,rc;
     char buf[BUFSIZE];
     ErlMessage emsg;

     ....stuff here....
       
     if ((rc = erl_receive_msg(sockfd , buf, BUFSIZE, &emsg)) == ERL_MSG) {
        ETERM *pid,*atom,*number,*string;

        msg = erl_decode_buf(bufp);
        if (ERL_IS_TUPLE(msg) & (ERL_TUPLE_SIZE(msg) == 4)) {

           /* NOTE!!! This is 0-based!! (first item is number 0)
            * Note too that element/2 (in Erlang) and
            * erl_element() are both 1-based.
            */
           pid    = ERL_TUPLE_ELEMENT(msg, 0);
           atom   = ERL_TUPLE_ELEMENT(msg, 1);
           number = ERL_TUPLE_ELEMENT(msg, 2);
           string = ERL_TUPLE_ELEMENT(msg, 3);

           ....etc.....        

        }

        ....etc....

     }

(NB: ERL_TUPLE_ELEMENT(x, i) doesn't seem to be documented.
     You'll find it in the erl_eterm.h header file though...)

> 2) At the time, as I wants to create pid using SELF(sockfd) macros (from
> the 1.2 Erl_Interface library). What is sockfd? Which socket file
> descriptor I should use if I wants to send messages like {sender pid,
> atom, number, string} from C program.

'sockfd' is the filedescriptor you get from erl_connect()
Example:

     int sockfd;
     char *nodename="xyz@REDACTED"; /* An example */
     if ((sockfd = erl_connect(nodename)) < 0)
       erl_err_quit("ERROR: erl_connect failed");

> 3) How to return messages to C program, which invokes erl_receive_msg.

You mean from Erlang ? 
In your C program you create your own Pid using the SELF() macro.
This Pid, has to be included in the message you send to Erlang.
The Erlang side can then use it to return a message.
It may be easier to use erl_rpc() instead. See for example,
how erl_rpc() is used in erl_call.c .

> Which operations I should perform before erl_receive_msg ? 

See the User's Manual. Basically you need to call erl_init()
and erl_connect() before you can receive anything, but again
to receive something asynchronous on your C-side, Erlang needs 
to know the Pid (NB: C-nodes doesn't show up in the result from 
the nodes/0 BIF), so you probably need to send (at least) your Pid 
to Erlang (using erl_send() ) before you can expect to receive 
anything on the C-side.

Good luck ! 

/Tobbe




More information about the erlang-questions mailing list