~ubuntu-branches/ubuntu/trusty/erlang/trusty

« back to all changes in this revision

Viewing changes to system/doc/tutorial/c_portdriver.xmlsrc

  • Committer: Bazaar Package Importer
  • Author(s): Clint Byrum
  • Date: 2011-05-05 15:48:43 UTC
  • mfrom: (3.5.13 sid)
  • Revision ID: james.westby@ubuntu.com-20110505154843-0om6ekzg6m7ugj27
Tags: 1:14.b.2-dfsg-3ubuntu1
* Merge from debian unstable.  Remaining changes:
  - Drop libwxgtk2.8-dev build dependency. Wx isn't in main, and not
    supposed to.
  - Drop erlang-wx binary.
  - Drop erlang-wx dependency from -megaco, -common-test, and -reltool, they
    do not really need wx. Also drop it from -debugger; the GUI needs wx,
    but it apparently has CLI bits as well, and is also needed by -megaco,
    so let's keep the package for now.
  - debian/patches/series: Do what I meant, and enable build-options.patch
    instead.
* Additional changes:
  - Drop erlang-wx from -et
* Dropped Changes:
  - patches/pcre-crash.patch: CVE-2008-2371: outer level option with
    alternatives caused crash. (Applied Upstream)
  - fix for ssl certificate verification in newSSL: 
    ssl_cacertfile_fix.patch (Applied Upstream)
  - debian/patches/series: Enable native.patch again, to get stripped beam
    files and reduce the package size again. (build-options is what
    actually accomplished this)
  - Remove build-options.patch on advice from upstream and because it caused
    odd build failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
 
3
 
 
4
<chapter>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2000</year><year>2009</year>
 
8
      <holder>Ericsson AB. All Rights Reserved.</holder>
 
9
    </copyright>
 
10
    <legalnotice>
 
11
      The contents of this file are subject to the Erlang Public License,
 
12
      Version 1.1, (the "License"); you may not use this file except in
 
13
      compliance with the License. You should have received a copy of the
 
14
      Erlang Public License along with this software. If not, it can be
 
15
      retrieved online at http://www.erlang.org/.
 
16
    
 
17
      Software distributed under the License is distributed on an "AS IS"
 
18
      basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
19
      the License for the specific language governing rights and limitations
 
20
      under the License.
 
21
    
 
22
    </legalnotice>
 
23
 
 
24
    <title>Port drivers</title>
 
25
    <prepared></prepared>
 
26
    <docno></docno>
 
27
    <date></date>
 
28
    <rev></rev>
 
29
    <file>c_portdriver.xml</file>
 
30
  </header>
 
31
  <p>This is an example of how to solve the <seealso marker="example">example problem</seealso> by using a linked in port driver.</p>
 
32
  <image file="../tutorial/port_driver.gif">
 
33
    <icaption>Port Driver Communication.</icaption>
 
34
  </image>
 
35
 
 
36
  <section>
 
37
    <title>Port Drivers</title>
 
38
    <p>A port driver is a linked in driver, that is accessible as a
 
39
      port from an Erlang program. It is a shared library (SO in Unix,
 
40
      DLL in Windows), with special entry points. The Erlang runtime
 
41
      calls these entry points, when the driver is started and when
 
42
      data is sent to the port. The port driver can also send data to
 
43
      Erlang.</p>
 
44
    <p>Since a port driver is dynamically linked into the emulator
 
45
      process, this is the fastest way of calling C-code from Erlang.
 
46
      Calling functions in the port driver requires no context
 
47
      switches. But it is also the least safe, because a crash in the
 
48
      port driver brings the emulator down too.</p>
 
49
  </section>
 
50
 
 
51
  <section>
 
52
    <title>Erlang Program</title>
 
53
    <p>Just as with a port program, the port communicates with a Erlang
 
54
      process. All communication goes through one Erlang process that
 
55
      is the <em>connected process</em> of the port
 
56
      driver. Terminating this process closes the port driver.</p>
 
57
    <p>Before the port is created, the driver must be loaded. This is
 
58
      done with the function <c>erl_dll:load_driver/1</c>, with the
 
59
      name of the shared library as argument.</p>
 
60
    <p>The port is then created using the BIF <c>open_port/2</c> with
 
61
      the tuple <c>{spawn, DriverName}</c> as the first argument. The
 
62
      string <c>SharedLib</c> is the name of the port driver. The second
 
63
      argument is a list of options, none in this case.</p>
 
64
    <pre>
 
65
-module(complex5).
 
66
-export([start/1, init/1]).
 
67
 
 
68
start(SharedLib) ->
 
69
    case erl_ddll:load_driver(".", SharedLib) of
 
70
        ok -> ok;
 
71
\011{error, already_loaded} -> ok;
 
72
\011_ -> exit({error, could_not_load_driver})
 
73
    end,
 
74
    spawn(?MODULE, init, [SharedLib]).
 
75
 
 
76
init(SharedLib) ->
 
77
  register(complex, self()),
 
78
  Port = open_port({spawn, SharedLib}, []),
 
79
  loop(Port).</pre>
 
80
    <p>Now it is possible to implement <c>complex5:foo/1</c> and
 
81
      <c>complex5:bar/1</c>. They both send a message to the
 
82
      <c>complex</c> process and receive the reply.</p>
 
83
    <pre>
 
84
foo(X) ->
 
85
    call_port({foo, X}).
 
86
bar(Y) ->
 
87
    call_port({bar, Y}).
 
88
 
 
89
call_port(Msg) ->
 
90
    complex ! {call, self(), Msg},
 
91
    receive
 
92
        {complex, Result} ->
 
93
            Result
 
94
    end.</pre>
 
95
    <p>The <c>complex</c> process encodes the message into a sequence
 
96
      of bytes, sends it to the port, waits for a reply, decodes the
 
97
      reply and sends it back to the caller.
 
98
      </p>
 
99
    <pre>
 
100
loop(Port) ->
 
101
    receive
 
102
        {call, Caller, Msg} ->
 
103
            Port ! {self(), {command, encode(Msg)}},
 
104
            receive
 
105
\011        {Port, {data, Data}} ->
 
106
                    Caller ! {complex, decode(Data)}
 
107
            end,
 
108
            loop(Port)
 
109
    end.</pre>
 
110
    <p>Assuming that both the arguments and the results from the C
 
111
      functions will be less than 256, a very simple encoding/decoding
 
112
      scheme is employed where <c>foo</c> is represented by the byte
 
113
      1, <c>bar</c> is represented by 2, and the argument/result is
 
114
      represented by a single byte as well.
 
115
      </p>
 
116
    <pre>
 
117
encode({foo, X}) -> [1, X];
 
118
encode({bar, Y}) -> [2, Y].
 
119
      
 
120
decode([Int]) -> Int.</pre>
 
121
    <p>The resulting Erlang program, including functionality for
 
122
      stopping the port and detecting port failures is shown below.</p>
 
123
      <codeinclude file="complex5.erl" type="erl"/>
 
124
  </section>
 
125
 
 
126
  <section>
 
127
    <title>C Driver</title>
 
128
    <p>The C driver is a module that is compiled and linked into a
 
129
      shared library. It uses a driver structure, and includes the
 
130
      header file <c>erl_driver.h</c>.</p>
 
131
    <p>The driver structure is filled with the driver name and function
 
132
      pointers. It is returned from the special entry point, declared
 
133
      with the macro <c><![CDATA[DRIVER_INIT(<driver_name>)]]></c>.</p>
 
134
    <p>The functions for receiving and sending data, are combined into
 
135
      a function, pointed out by the driver structure. The data sent
 
136
      into the port is given as arguments, and the data the port
 
137
      sends back is sent with the C-function <c>driver_output</c>.</p>
 
138
    <p>Since the driver is a shared module, not a program, no main
 
139
      function should be present. All function pointers are not used
 
140
      in our example, and the corresponding fields in the
 
141
      <c>driver_entry</c> structure are set to NULL.</p>
 
142
    <p>All functions in the driver, takes a handle (returned from
 
143
      <c>start</c>), that is just passed along by the erlang
 
144
      process. This must in some way refer to the port driver
 
145
      instance.</p>
 
146
    <p>The example_drv_start, is the only function that is called with
 
147
      a handle to the port instance, so we must save this. It is
 
148
      customary to use a allocated driver-defined structure for this
 
149
      one, and pass a pointer back as a reference.</p>
 
150
    <p>It is not a good idea to use a global variable; since the port
 
151
      driver can be spawned by multiple Erlang processes, this
 
152
      driver-structure should be instantiated multiple times.
 
153
      </p>
 
154
    <codeinclude file="port_driver.c" tag="" type="none"></codeinclude>
 
155
  </section>
 
156
 
 
157
  <section>
 
158
    <title>Running the Example</title>
 
159
    <p>1. Compile the C code.</p>
 
160
    <pre>
 
161
unix> <input>gcc -o exampledrv -fpic -shared complex.c port_driver.c</input>
 
162
windows> <input>cl -LD -MD -Fe exampledrv.dll complex.c port_driver.c</input></pre>
 
163
    <p>2. Start Erlang and compile the Erlang code.</p>
 
164
    <pre>
 
165
> <input>erl</input>
 
166
Erlang (BEAM) emulator version 5.1
 
167
 
 
168
Eshell V5.1 (abort with ^G)
 
169
1> <input>c(complex5).</input>
 
170
{ok,complex5}</pre>
 
171
    <p>3. Run the example.</p>
 
172
    <pre>
 
173
2> <input>complex5:start("example_drv").</input>
 
174
&lt;0.34.0>
 
175
3> <input>complex5:foo(3).</input>
 
176
4
 
177
4> <input>complex5:bar(5).</input>
 
178
10
 
179
5> <input>complex5:stop().</input>
 
180
stop</pre>
 
181
  </section>
 
182
</chapter>
 
183