1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- This document was generated using DocBuilder 3.3.3 -->
<HTML>
<HEAD>
<TITLE>Erl_Interface</TITLE>
<SCRIPT type="text/javascript" src="../../doc/erlresolvelinks.js">
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#FF00FF"
ALINK="#FF0000">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Ericsson AB]" SRC="min_head.gif"></A>
</CENTER>
<A NAME="5"><!-- Empty --></A>
<H2>5 Erl_Interface</H2>
<P>This is an example of how to solve the <A HREF="example.html">example problem</A> by using a port and <CODE>erl_interface</CODE>. It is necessary to read the <A HREF="c_port.html">port example</A> before reading this chapter.<A NAME="5.1"><!-- Empty --></A>
<H3>5.1 Erlang Program</H3>
<P>Compared to the Erlang module
<A TARGET="_top" HREF="complex1.erl">complex1.erl</A>
used for the plain port, there are two differences when using Erl_Interface on the C side: Since Erl_Interface operates on the Erlang external term format the port must be set to use binaries and, instead of inventing an encoding/decoding scheme, the BIFs <CODE>term_to_binary/1</CODE> and <CODE>binary_to_term/1</CODE> should be used. That is:
<PRE>
open_port({spawn, ExtPrg}, [{packet, 2}])
</PRE>
<P>is replaced with:
<PRE>
open_port({spawn, ExtPrg}, [{packet, 2}, binary])
</PRE>
<P>And:
<PRE>
Port ! {self(), {command, encode(Msg)}},
receive
{Port, {data, Data}} ->
Caller ! {complex, decode(Data)}
end
</PRE>
<P>is replaced with:
<PRE>
Port ! {self(), {command, term_to_binary(Msg)}},
receive
{Port, {data, Data}} ->
Caller ! {complex, binary_to_term(Data)}
end
</PRE>
<P>The resulting Erlang program can be found in
<A TARGET="_top" HREF="complex2.erl">complex2.erl</A>
. Note that calling <CODE>complex2:foo/1</CODE> and <CODE>complex2:bar/1</CODE> will result in the tuple <CODE>{foo,X}</CODE> or <CODE>{bar,Y}</CODE> being sent to the <CODE>complex</CODE> process, which will code them as binaries and send them to the port. This means that the C program must be able to handle these two tuples.<A NAME="5.2"><!-- Empty --></A>
<H3>5.2 C Program</H3>
<P>Compared to the C program
<A TARGET="_top" HREF="port.c">port.c</A>
used for the plain port the <CODE>while</CODE>-loop must be rewritten. Messages coming from the port will be on the Erlang external term format. They should be converted into an <CODE>ETERM</CODE> struct, a C struct similar to an Erlang term. The result of calling <CODE>foo()</CODE> or <CODE>bar()</CODE> must be converted to the Erlang external term format before being sent back to the port. But before calling any other <CODE>erl_interface</CODE> function, the memory handling must be initiated.
<PRE>
erl_init(NULL, 0);
</PRE>
<P>For reading from and writing to the port the functions <CODE>read_cmd()</CODE> and <CODE>write_cmd()</CODE> from
<A TARGET="_top" HREF="erl_comm.c">erl_comm.c</A>
can still be used. The function <CODE>erl_decode()</CODE> from <CODE>erl_marshal</CODE> will convert the binary into an <CODE>ETERM</CODE> struct.
<PRE>
int main() {
ETERM *tuplep;
while (read_cmd(buf) > 0) {
tuplep = erl_decode(buf);
</PRE>
<P>In this case <CODE>tuplep</CODE> now points to an <CODE>ETERM</CODE> struct representing a tuple with two elements; the function name (atom) and the argument (integer). By using the function <CODE>erl_element()</CODE> from <CODE>erl_eterm</CODE> it is possible to extract these elements, which also must be declared as pointers to an <CODE>ETERM</CODE> struct.
<PRE>
fnp = erl_element(1, tuplep);
argp = erl_element(2, tuplep);
</PRE>
<P>The macros <CODE>ERL_ATOM_PTR</CODE> and <CODE>ERL_INT_VALUE</CODE> from <CODE>erl_eterm</CODE> can be used to obtain the actual values of the atom and the integer. The atom value is represented as a string. By comparing this value with the strings "foo" and "bar" it can be decided which function to call.
<PRE>
if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
res = foo(ERL_INT_VALUE(argp));
} else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) {
res = bar(ERL_INT_VALUE(argp));
}
</PRE>
<P>Now an <CODE>ETERM</CODE> struct representing the integer result can be constructed using the function <CODE>erl_mk_int()</CODE> from <CODE>erl_eterm</CODE>. It is also possible to use the function <CODE>erl_format()</CODE> from the module <CODE>erl_format</CODE>.
<PRE>
intp = erl_mk_int(res);
</PRE>
<P>The resulting <CODE>ETERM</CODE> struct is converted into the Erlang external term format using the function <CODE>erl_encode()</CODE> from <CODE>erl_marshal</CODE> and sent to Erlang using <CODE>write_cmd()</CODE>.
<PRE>
erl_encode(intp, buf);
write_cmd(buf, erl_eterm_len(intp));
</PRE>
<P>Last, the memory allocated by the <CODE>ETERM</CODE> creating functions must be freed.
<PRE>
erl_free_compound(tuplep);
erl_free_term(fnp);
erl_free_term(argp);
erl_free_term(intp);
</PRE>
<P>The resulting C program can be found in
<A TARGET="_top" HREF="ei.c">ei.c</A>
.<A NAME="5.3"><!-- Empty --></A>
<H3>5.3 Running the Example</H3>
<P>1. Compile the C code, providing the paths to the include files <CODE>erl_interface.h</CODE> and <CODE>ei.h</CODE>, and to the libraries <CODE>erl_interface</CODE> and <CODE>ei</CODE>.
<PRE>
unix> gcc -o extprg -I/usr/local/otp/lib/erl_interface-3.2.1/include \
-L/usr/local/otp/lib/erl_interface-3.2.1/lib \
complex.c erl_comm.c ei.c -lerl_interface -lei
</PRE>
<P>In R5B and later versions of OTP, the <CODE>include</CODE> and <CODE>lib</CODE> directories are situated under <CODE>OTPROOT/lib/erl_interface-VSN</CODE>, where <CODE>OTPROOT</CODE> is the root directory of the OTP installation (<CODE>/usr/local/otp</CODE> in the example above) and <CODE>VSN</CODE> is the version of the <CODE>erl_interface</CODE> application (3.2.1 in the example above).<BR>
In R4B and earlier versions of OTP, <CODE>include</CODE> and <CODE>lib</CODE> are situated under <CODE>OTPROOT/usr</CODE>.
<P>2. Start Erlang and compile the Erlang code.
<PRE>
unix> erl
Erlang (BEAM) emulator version 4.9.1.2
Eshell V4.9.1.2 (abort with ^G)
1> c(complex2).
{ok,complex2}
</PRE>
<P>3. Run the example.
<PRE>
2> complex2:start("extprg").
<0.34.0>
3> complex2:foo(3).
4
4> complex2:bar(5).
10
5> complex2:bar(352).
704
6> complex2:stop().
stop
</PRE>
<CENTER>
<HR>
<SMALL>
Copyright © 1991-2006
<A HREF="http://www.erlang.se">Ericsson AB</A><BR>
</SMALL>
</CENTER>
</BODY>
</HTML>
|