Sophie

Sophie

distrib > Fedora > 17 > i386 > by-pkgid > 675c8c8167236dfcf8d66da674f931e8 > files > 1413

erlang-doc-R15B-03.3.fc17.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../../doc/otp_doc.css" type="text/css">
<title>Erlang -- The Erlang I/O-protocol</title>
</head>
<body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container">
<script id="js" type="text/javascript" language="JavaScript" src="../../../../doc/js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../../../../doc/js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript">
            <!--
              function getWinHeight() {
                var myHeight = 0;
                if( typeof( window.innerHeight ) == 'number' ) {
                  //Non-IE
                  myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth ||
                                                         document.documentElement.clientHeight ) ) {
                  //IE 6+ in 'standards compliant mode'
                  myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                  //IE 4 compatible
                  myHeight = document.body.clientHeight;
                }
                return myHeight;
              }

              function setscrollpos() {
                var objf=document.getElementById('loadscrollpos');
                 document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2;
              }

              function addEvent(obj, evType, fn){
                if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
              } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
              } else {
                return false;
              }
             }

             addEvent(window, 'load', setscrollpos);

             //--></script><div id="leftnav"><div class="innertube">
<img alt="Erlang logo" src="../../../../doc/erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="index.html">Reference Manual</a><br><a href="release_notes.html">Release Notes</a><br><a href="../pdf/stdlib-1.18.3.pdf">PDF</a><br><a href="../../../../doc/index.html">Top</a></small><p><strong>STDLIB</strong><br><strong>User's Guide</strong><br><small>Version 1.18.3</small></p>
<br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p>
<ul class="flipMenu" imagepath="../../../../doc/js/flipmenu">
<li id="loadscrollpos" title="The Erlang I/O-protocol" expanded="true">The Erlang I/O-protocol<ul>
<li><a href="io_protocol.html">
              Top of chapter
            </a></li>
<li title="Protocol basics"><a href="io_protocol.html#id59620">Protocol basics</a></li>
<li title="Output requests"><a href="io_protocol.html#id60942">Output requests</a></li>
<li title="Input Requests"><a href="io_protocol.html#id61090">Input Requests</a></li>
<li title="I/O-server modes"><a href="io_protocol.html#id60161">I/O-server modes</a></li>
<li title="Multiple I/O requests"><a href="io_protocol.html#id61124">Multiple I/O requests</a></li>
<li title="Optional I/O-requests"><a href="io_protocol.html#id57061">Optional I/O-requests</a></li>
<li title="Unimplemented request types:"><a href="io_protocol.html#id57117">Unimplemented request types:</a></li>
<li title="An annotated and working example io_server:"><a href="io_protocol.html#id62683">An annotated and working example io_server:</a></li>
</ul>
</li>
<li id="no" title="Using Unicode in Erlang" expanded="false">Using Unicode in Erlang<ul>
<li><a href="unicode_usage.html">
              Top of chapter
            </a></li>
<li title="What Unicode is"><a href="unicode_usage.html#id62432">What Unicode is</a></li>
<li title="Standard Unicode representation in Erlang"><a href="unicode_usage.html#id62530">Standard Unicode representation in Erlang</a></li>
<li title="Basic language support for Unicode"><a href="unicode_usage.html#id61332">Basic language support for Unicode</a></li>
<li title="The interactive shell"><a href="unicode_usage.html#id61505">The interactive shell</a></li>
<li title="Unicode file names"><a href="unicode_usage.html#id62869">Unicode file names</a></li>
<li title="Unicode in environment variables and parameters"><a href="unicode_usage.html#id63164">Unicode in environment variables and parameters</a></li>
<li title="Unicode-aware modules"><a href="unicode_usage.html#id63210">Unicode-aware modules</a></li>
<li title="Unicode recipes"><a href="unicode_usage.html#id63408">Unicode recipes</a></li>
</ul>
</li>
</ul>
</div></div>
<div id="content">
<div class="innertube">
<h1>1 The Erlang I/O-protocol</h1>
  


<p>The I/O-protocol in Erlang specifies a way for a client to communicate
with an io_server and vice versa. The io_server is a process handling the
requests and that performs the requested task on i.e. a device. The
client is any Erlang process wishing to read or write data from/to the
device.</p> 

<p>The common I/O-protocol has been present in OTP since the
beginning, but has been fairly undocumented and has also somewhat
evolved over the years. In an addendum to Robert Virdings rationale
the original I/O-protocol is described. This document describes the
current I/O-protocol.</p>

<p>The original I/O-protocol was simple and flexible. Demands for spacial
and execution time efficiency has triggered extensions to the protocol
over the years, making the protocol larger and somewhat less easy to
implement than the original. It can certainly be argumented that the
current protocol is too complex, but this text describes how it looks
today, not how it should have looked.</p>

<p>The basic ideas from the original protocol still hold. The io_server
and client communicate with one single, rather simplistic protocol and
no server state is ever present in the client. Any io_server can be
used together with any client code and client code need not be aware
of the actual device the io_server communicates with.</p>

<h3><a name="id59620">1.1 
        Protocol basics</a></h3>


<p>As described in Robert's paper, servers and clients communicate using
io_request/io_reply tuples as follows:</p>

<p><strong>{io_request, From, ReplyAs, Request}</strong><br>
<strong>{io_reply, ReplyAs, Reply}</strong></p>

<p>The client sends an io_request to the io_server and the server
eventually sends a corresponding reply.</p>

<ul>
<li>From is the pid() of the client, the process which the io_server
sends the reply to.</li>

<li>ReplyAs can be any datum and is simply returned in the corresponding
io_reply. The io-module in the Erlang standard library simply uses the pid()
of the io_server as the ReplyAs datum, but a more complicated client
could have several outstanding io-requests to the same server and
would then use i.e. a reference() or something else to differentiate among
the incoming io_reply's. The ReplyAs element should be considered
opaque by the io_server. Note that the pid() of the server is not
explicitly present in the io_reply. The reply can be sent from any
process, not necessarily the actual io_server. The ReplyAs element is
the only thing that connects one io_request with an io_reply.</li>

<li>Request and Reply are described below.</li>
</ul>

<p>When an io_server receives an io_request, it acts upon the actual
Request part and eventually sends an io_reply with the corresponding
Reply part.</p> 

<h3><a name="id60942">1.2 
        Output requests</a></h3>


<p>To output characters on a device, the following Requests exist:</p>

<p>
<strong>{put_chars, Encoding, Characters}</strong><br>
<strong>{put_chars, Encoding, Module, Function, Args}</strong>
</p>
<ul>
<li>Encoding is either 'unicode' or 'latin1', meaning that the
  characters are (in case of binaries) encoded as either UTF-8 or
  iso-latin-1 (pure bytes). A well behaved io_server should also
  return error if list elements contain integers &gt; 255 when the
  Encoding is set to latin1. Note that this does not in any way tell
  how characters should be put on the actual device or how the
  io_server should handle them. Different io_servers may handle the
  characters however they want, this simply tells the io_server which
  format the data is expected to have. In the Module/Function/argument
  case, the Encoding tells which format the designated function
  produces. Note that byte-oriented data is simplest sent using latin1
  Encoding</li>

<li>Characters are the data to be put on the device. If Encoding is
  latin1, this is an iolist(). If Encoding is unicode, this is an
  Erlang standard mixed unicode list (one integer in a list per
  character, characters in binaries represented as UTF-8).</li>

<li>Module, Function, Args denotes a function which will be called to
  produce the data (like io_lib:format). Args is a list of arguments
  to the function. The function should produce data in the given
  Encoding. The io_server should call the function as apply(Mod, Func,
  Args) and will put the returned data on the device as if it was sent
  in a {put_chars, Encoding, Characters} request. If the function
  returns anything else than a binary or list or throws an exception,
  an error should be sent back to the client.</li>
</ul>

<p>The server replies to the client with an io_reply where the Reply
element is one of:</p>
<p>
<strong>ok</strong><br>
<strong>{error, Error}</strong>
</p>

<ul>
<li>Error describes the error to the client, which may do whatever it
  wants with it. The Erlang io-module typically returns it as is.</li>
</ul>

<p>For backward compatibility the following Requests should also be
handled by an io_server (these messages should not be present after
R15B of OTP):</p>
<p>
<strong>{put_chars, Characters}</strong><br>
<strong>{put_chars, Module, Function, Args}</strong>
</p>

<p>These should behave as {put_chars, latin1, Characters} and {put_chars,
latin1, Module, Function, Args} respectively. </p>

<h3><a name="id61090">1.3 
        Input Requests</a></h3>


<p>To read characters from a device, the following Requests exist:</p>

<p><strong>{get_until, Encoding, Prompt, Module, Function, ExtraArgs}</strong></p>

<ul>
<li>Encoding denotes how data is to be sent back to the client and
  what data is sent to the function denoted by
  Module/Function/ExtraArgs. If the function supplied returns data as a
  list, the data is converted to this encoding. If however the
  function supplied returns data in some other format, no conversion
  can be done and it's up to the client supplied function to return
  data in a proper way. If Encoding is latin1, lists of integers
  0..255 or binaries containing plain bytes are sent back to the
  client when possible, if Encoding is unicode, lists with integers in
  the whole unicode range or binaries encoded in UTF-8 are sent to the
  client. The user supplied function will always see lists of integers, never
  binaries, but the list may contain numbers &gt; 255 if the Encoding is
  'unicode'.</li>

<li>Prompt is a list of characters (not mixed, no binaries) or an atom()
  to be output as a prompt for input on the device. The Prompt is
  often ignored by the io_server and a Prompt set to '' should always
  be ignored (and result in nothing being written to the device).</li>

<li>
<p>Module, Function, ExtraArgs denotes a function and arguments to
  determine when enough data is written. The function should take two
  additional arguments, the last state, and a list of characters. The
  function should return one of:</p>
<p>
<strong>{done, Result, RestChars}</strong><br>
<strong>{more, Continuation}</strong>
</p>
  <p>The Result can be any Erlang term, but if it is a list(), the
  io_server may convert it to a binary() of appropriate format before
  returning it to the client, if the server is set in binary mode (see
  below).</p>

  <p>The function will be called with the data the io_server finds on
  its device, returning {done, Result, RestChars} when enough data is
  read (in which case Result is sent to the client and RestChars are
  kept in the io_server as a buffer for subsequent input) or {more,
  Continuation}, indicating that more characters are needed to
  complete the request. The Continuation will be sent as the state in
  subsequent calls to the function when more characters are
  available. When no more characters are available, the function
  shall return {done,eof,Rest}. 
  The initial state is the empty list and the data when an
  end of file is reached on the device is the atom 'eof'. An emulation
  of the get_line request could be (inefficiently) implemented using
  the following functions:</p>
<div class="example"><pre>
-module(demo).
-export([until_newline/3, get_line/1]).

until_newline(_ThisFar,eof,_MyStopCharacter) -&gt;
    {done,eof,[]};
until_newline(ThisFar,CharList,MyStopCharacter) -&gt;
    case lists:splitwith(fun(X) -&gt; X =/= MyStopCharacter end,  CharList) of
	{L,[]} -&gt;
            {more,ThisFar++L};
	{L2,[MyStopCharacter|Rest]} -&gt;
	    {done,ThisFar++L2++[MyStopCharacter],Rest}
    end.

get_line(IoServer) -&gt;
    IoServer ! {io_request, self(), IoServer, {get_until, unicode, '',
                                               ?MODULE, until_newline, [$\n]}},
    receive
        {io_reply, IoServer, Data} -&gt;
	    Data
    end.
</pre></div>
 <p>Note especially that the last element in the Request tuple ([$\n])
  is appended to the argument list when the function is called. The
  function should be called like 
  apply(Module, Function, [ State, Data | ExtraArgs ]) by the io_server</p>
</li>
</ul>

<p>A defined number of characters is requested using this Request:</p>
<p>
<strong>{get_chars, Encoding, Prompt, N}</strong>
</p>

<ul>
<li>Encoding and Prompt as for get_until.</li>

<li>N is the number of characters to be read from the device.</li>
</ul>

<p>A single line (like in the example above) is requested with this Request:</p>
<p>
<strong>{get_line, Encoding, Prompt}</strong>
</p>

<ul>
<li>Encoding and prompt as above.</li>
</ul>

<p>Obviously, get_chars and get_line could be implemented with the
get_until request (and indeed was originally), but demands for
efficiency has made these additions necessary.</p>

<p>The server replies to the client with an io_reply where the Reply
element is one of:</p>
<p>
<strong>Data</strong><br>
<strong>eof</strong><br>
<strong>{error, Error}</strong>
</p>

<ul>
<li>Data is the characters read, in either list or binary form
  (depending on the io_server mode, see below).</li>
<li>Error describes the error to the client, which may do whatever it
  wants with it. The Erlang io-module typically returns it as is.</li>
<li>eof is returned when input end is reached and no more data is
available to the client process.</li>
</ul>

<p>For backward compatibility the following Requests should also be
handled by an io_server (these messages should not be present after
R15B of OTP):</p>

<p>
<strong>{get_until, Prompt, Module, Function, ExtraArgs}</strong><br>
<strong>{get_chars, Prompt, N}</strong><br>
<strong>{get_line, Prompt}</strong><br>
</p>

<p>These should behave as {get_until, latin1, Prompt, Module, Function,
ExtraArgs}, {get_chars, latin1, Prompt, N} and {get_line, latin1,
Prompt} respectively.</p>

<h3><a name="id60161">1.4 
        I/O-server modes</a></h3>


<p>Demands for efficiency when reading data from an io_server has not
only lead to the addition of the get_line and get_chars requests, but
has also added the concept of io_server options. No options are
mandatory to implement, but all io_servers in the Erlang standard
libraries honor the 'binary' option, which allows the Data in the
io_reply to be binary instead of in list form <strong>when possible</strong>. 
If the data is sent as a binary, Unicode data will be sent in the 
standard Erlang unicode
format, i.e. UTF-8 (note that the function in get_until still gets
list data regardless of the io_server mode).</p>

<p>Note that i.e. the <span class="code">get_until</span> request allows for a function with the data specified as always being a list. Also the return value data from such a function can be of any type (as is indeed the case when an io:fread request is sent to an io_server). The client has to be prepared for data received as answers to those requests to be in a variety of forms, but the server should convert the results to binaries whenever possible (i.e. when the function supplied to get_until actually returns a list). The example shown later in this text does just that.</p>

<p>An I/O-server in binary mode will affect the data sent to the client,
so that it has to be able to handle binary data. For convenience, it
is possible to set and retrieve the modes of an io_server using the
following I/O-requests:</p>

<p>
<strong>{setopts, Opts}</strong>
</p>


<ul>
<li>Opts is a list of options in the format recognized by proplists (and
  of course by the io_server itself).</li>
</ul>
<p>As an example, the io_server for the interactive shell (in group.erl)
understands the following options:</p>
<p>
<strong>{binary, bool()} (or 'binary'/'list')</strong><br>
<strong>{echo, bool()}</strong><br>
<strong>{expand_fun, fun()}</strong><br>
<strong>{encoding, 'unicode'/'latin1'} (or 'unicode'/'latin1')</strong>
</p>

<p>- of which the 'binary' and 'encoding' options are common for all
io_servers in OTP, while 'echo' and 'expand' is valid only for this
io_server. It's worth noting that the 'unicode' option notifies how
characters are actually put on the physical device, i.e. if the
terminal per se is unicode aware, it does not affect how characters
are sent in the I/O-protocol, where each request contains encoding
information for the provided or returned data.</p>

<p>The server should send one of the following as Reply:</p>
<p>
<strong>ok</strong><br>
<strong>{error, Error}</strong>
</p>

<p>An error (preferably enotsup) is to be expected if the option is
not supported by the io_server (like if an 'echo' option is sent in a
setopt Request to a plain file).</p>

<p>To retrieve options, this message is used:</p>
<p>
<strong>getopts</strong>
</p>

<p>The 'getopts' message requests a complete list of all options
supported by the io_server as well as their current values.</p>

<p>The server replies:</p>
<p>
<strong>OptList</strong><br>
<strong>{error,Error}</strong>
</p>

<ul>
<li>OptList is a list of tuples {Option, Value} where Option is always
  an atom.</li>
</ul>

<h3><a name="id61124">1.5 
        Multiple I/O requests</a></h3>


<p>The Request element can in itself contain several Requests by using
the following format:</p>
<p>
<strong>{requests, Requests}</strong>
</p>
<ul>
<li>Requests is a list of valid Request tuples for the protocol, they
  shall be executed in the order in which they appear in the list and
  the execution should continue until one of the requests result in an
  error or the list is consumed. The result of the last request is
  sent back to the client.</li>
</ul>

<p>The server can for a list of requests send any of the valid results in
the reply:</p>

<p>
<strong>ok</strong><br>
<strong>{ok, Data}</strong><br>
<strong>{ok, Options}</strong><br>
<strong>{error, Error}</strong>
</p>
<p>- depending on the actual requests in the list.</p>

<h3><a name="id57061">1.6 
        Optional I/O-requests</a></h3>


<p>The following I/O request is optional to implement and a client
should be prepared for an error return:</p>
<p>
<strong>{get_geometry, Geometry}</strong>
</p>
<ul>
<li>Geometry is either the atom 'rows' or the atom 'columns'.</li>
</ul>
<p>The server should send the Reply as:</p>
<p>
<strong>{ok, N}</strong><br>
<strong>{error, Error}</strong>
</p>

<ul>
<li>N is the number of character rows or columns the device has, if
  applicable to the device the io_server handles, otherwise {error,
  enotsup} is a good answer.</li>
</ul>

<h3><a name="id57117">1.7 
        Unimplemented request types:</a></h3>


<p>If an io_server encounters a request it does not recognize (i.e. the
io_request tuple is in the expected format, but the actual Request is
unknown), the server should send a valid reply with the error tuple:</p>
<p>
<strong>{error, request}</strong>
</p>

<p>This makes it possible to extend the protocol with optional messages
and for the clients to be somewhat backwards compatible.</p>

<h3><a name="id62683">1.8 
        An annotated and working example io_server:</a></h3>


<p>An io_server is any process capable of handling the protocol. There is
no generic io_server behavior, but could well be. The framework is
simple enough, a process handling incoming requests, usually both
io_requests and other device-specific requests (for i.e. positioning ,
closing etc.).</p>

<p>Our example io_server stores characters in an ets table, making up a
fairly crude ram-file (it is probably not useful, but working).</p>

<p>The module begins with the usual directives, a function to start the
server and a main loop handling the requests:</p> 

<div class="example"><pre>
-module(ets_io_server).

-export([start_link/0, init/0, loop/1, until_newline/3, until_enough/3]).

-define(CHARS_PER_REC, 10).

-record(state, {
	  table,
	  position, % absolute
	  mode % binary | list
	 }).

start_link() -&gt;
    spawn_link(?MODULE,init,[]).

init() -&gt;
    Table = ets:new(noname,[ordered_set]),
    ?MODULE:loop(#state{table = Table, position = 0, mode=list}).

loop(State) -&gt;
    receive
	{io_request, From, ReplyAs, Request} -&gt;
	    case request(Request,State) of
		{Tag, Reply, NewState} when Tag =:= ok; Tag =:= error -&gt;
		    reply(From, ReplyAs, Reply),
		    ?MODULE:loop(NewState);
		{stop, Reply, _NewState} -&gt;
		    reply(From, ReplyAs, Reply),
		    exit(Reply)
	    end;
	%% Private message
	{From, rewind} -&gt;
	    From ! {self(), ok},
	    ?MODULE:loop(State#state{position = 0});
	_Unknown -&gt;
	    ?MODULE:loop(State)
    end.
</pre></div>

<p>The main loop receives messages from the client (which might be using
the io-module to send requests). For each request the function
request/2 is called and a reply is eventually sent using the reply/3
function.</p>

<p>The "private" message {From, rewind} results in the
current position in the pseudo-file to be reset to 0 (the beginning of
the "file"). This is a typical example of device-specific
messages not being part of the I/O-protocol. It is usually a bad idea
to embed such private messages in io_request tuples, as that might be
confusing to the reader.</p>

<p>Let's look at the reply function first...</p>

<div class="example"><pre>

reply(From, ReplyAs, Reply) -&gt;
    From ! {io_reply, ReplyAs, Reply}.

</pre></div>

<p>Simple enough, it sends the io_reply tuple back to the client,
providing the ReplyAs element received in the request along with the
result of the request, as described above.</p>

<p>Now look at the different requests we need to handle. First the
requests for writing characters:</p>

<div class="example"><pre>
request({put_chars, Encoding, Chars}, State) -&gt;
    put_chars(unicode:characters_to_list(Chars,Encoding),State);
request({put_chars, Encoding, Module, Function, Args}, State) -&gt;
    try
	request({put_chars, Encoding, apply(Module, Function, Args)}, State)
    catch
	_:_ -&gt;
	    {error, {error,Function}, State}
    end;
</pre></div>

<p>The Encoding tells us how the characters in the message are
represented. We want to store the characters as lists in the
ets-table, so we convert them to lists using the
unicode:characters_to_list/2 function. The conversion function
conveniently accepts the encoding types unicode or latin1, so we can
use the Encoding parameter directly.</p>

<p>When Module, Function and Arguments are provided, we simply apply it
and do the same thing with the result as if the data was provided
directly.</p>

<p>Let's handle the requests for retrieving data too:</p>

<div class="example"><pre>
request({get_until, Encoding, _Prompt, M, F, As}, State) -&gt;
    get_until(Encoding, M, F, As, State);
request({get_chars, Encoding, _Prompt, N}, State) -&gt;
    %% To simplify the code, get_chars is implemented using get_until
    get_until(Encoding, ?MODULE, until_enough, [N], State);
request({get_line, Encoding, _Prompt}, State) -&gt;
    %% To simplify the code, get_line is implemented using get_until
    get_until(Encoding, ?MODULE, until_newline, [$\n], State);
</pre></div>

<p>Here we have cheated a little by more or less only implementing
get_until and using internal helpers to implement get_chars and
get_line. In production code, this might be to inefficient, but that
of course depends on the frequency of the different requests. Before
we start actually implementing the functions put_chars/2 and
get_until/5, lets look into the few remaining requests:</p>

<div class="example"><pre>
request({get_geometry,_}, State) -&gt;
    {error, {error,enotsup}, State};
request({setopts, Opts}, State) -&gt;
    setopts(Opts, State);
request(getopts, State) -&gt;
    getopts(State);
request({requests, Reqs}, State) -&gt;
     multi_request(Reqs, {ok, ok, State});
</pre></div>

<p>The get_geometry request has no meaning for this io_server, so the
reply will be {error, enotsup}. The only option we handle is the
binary/list option, which is done in separate functions.</p>

<p>The multi-request tag (requests) is handled in a separate loop
function applying the requests in the list one after another,
returning the last result.</p>

<p>What's left is to handle backward compatibility and the file-module
(which uses the old requests until backward compatibility with pre-R13
nodes is no longer needed). Note that the io_server will not work with
a simple file:write if these are not added:</p>

<div class="example"><pre>
request({put_chars,Chars}, State) -&gt;
    request({put_chars,latin1,Chars}, State);
request({put_chars,M,F,As}, State) -&gt;
    request({put_chars,latin1,M,F,As}, State);
request({get_chars,Prompt,N}, State) -&gt;
    request({get_chars,latin1,Prompt,N}, State);
request({get_line,Prompt}, State) -&gt;
    request({get_line,latin1,Prompt}, State);
request({get_until, Prompt,M,F,As}, State) -&gt;
    request({get_until,latin1,Prompt,M,F,As}, State);
</pre></div>

<p>Ok, what's left now is to return {error, request} if the request is
not recognized:</p>

<div class="example"><pre>
request(_Other, State) -&gt;
    {error, {error, request}, State}.
</pre></div>

<p>Let's move further and actually handle the different requests, first
the fairly generic multi-request type:</p>

<div class="example"><pre>
multi_request([R|Rs], {ok, _Res, State}) -&gt;
    multi_request(Rs, request(R, State));
multi_request([_|_], Error) -&gt;
    Error;
multi_request([], Result) -&gt;
    Result.
</pre></div>

<p>We loop through the requests one at the time, stopping when we either
encounter an error or the list is exhausted. The last return value is
sent back to the client (it's first returned to the main loop and then
sent back by the function io_reply).</p>

<p>The getopt and setopt requests are also simple to handle, we just
change or read our state record:</p>

<div class="example"><pre>
setopts(Opts0,State) -&gt;
    Opts = proplists:unfold(
	     proplists:substitute_negations(
	       [{list,binary}], 
	       Opts0)),
    case check_valid_opts(Opts) of
	true -&gt;
	        case proplists:get_value(binary, Opts) of
		    true -&gt;
			{ok,ok,State#state{mode=binary}};
		    false -&gt;
			{ok,ok,State#state{mode=binary}};
		    _ -&gt;
			{ok,ok,State}
		end;
	false -&gt;
	    {error,{error,enotsup},State}
    end.
check_valid_opts([]) -&gt;
    true;
check_valid_opts([{binary,Bool}|T]) when is_boolean(Bool) -&gt;
    check_valid_opts(T);
check_valid_opts(_) -&gt;
    false.

getopts(#state{mode=M} = S) -&gt;
    {ok,[{binary, case M of
		      binary -&gt;
			  true;
		      _ -&gt;
			  false
		  end}],S}.
</pre></div>

<p>As a convention, all io_servers handle both {setopts, [binary]},
{setopts, [list]} and {setopts,[{binary, bool()}]}, hence the trick
with proplists:substitute_negations/2 and proplists:unfold/1. If
invalid options are sent to us, we send {error,enotsup} back to the
client.</p>

<p>The getopts request should return a list of {Option, Value} tuples,
which has the twofold function of providing both the current values
and the available options of this io_server. We have only one option,
and hence return that.</p>

<p>So far our io_server has been fairly generic (except for the rewind
request handled in the main loop and the creation of an ets table). 
Most io_servers contain code similar to what's above.</p>

<p>To make the example runnable, we now start implementing the actual
reading and writing of the data to/from the ets-table. First the
put_chars function:</p>

<div class="example"><pre>
put_chars(Chars, #state{table = T, position = P} = State) -&gt;
    R = P div ?CHARS_PER_REC,
    C = P rem ?CHARS_PER_REC,
    [ apply_update(T,U) || U &lt;- split_data(Chars, R, C) ],
    {ok, ok, State#state{position = (P + length(Chars))}}.
</pre></div>

<p>We already have the data as (Unicode) lists and therefore just split
the list in runs of a predefined size and put each run in the
table at the current position (and forward). The functions
split_data/3 and apply_update/2 are implemented below.</p>

<p>Now we want to read data from the table. The get_until function reads
data and applies the function until it says it's done. The result is
sent back to the client:</p>

<div class="example"><pre>
get_until(Encoding, Mod, Func, As, 
	  #state{position = P, mode = M, table = T} = State) -&gt;
    case get_loop(Mod,Func,As,T,P,[]) of
	{done,Data,_,NewP} when is_binary(Data); is_list(Data) -&gt;
	    if
		M =:= binary -&gt; 
		    {ok, 
		     unicode:characters_to_binary(Data,unicode,Encoding),
		     State#state{position = NewP}};
		true -&gt;
		    case check(Encoding, 
				unicode:characters_to_list(Data, unicode)) of
			{error, _} = E -&gt;
			    {error, E, State};
			List -&gt;
			    {ok, List,
			     State#state{position = NewP}}
		    end
	    end;
	{done,Data,_,NewP} -&gt;
	    {ok, Data, State#state{position = NewP}};
	Error -&gt;
	    {error, Error, State}
    end.

get_loop(M,F,A,T,P,C) -&gt;
    {NewP,L} = get(P,T),
    case catch apply(M,F,[C,L|A]) of
	{done, List, Rest} -&gt;
	    {done, List, [], NewP - length(Rest)};
	{more, NewC} -&gt;
	    get_loop(M,F,A,T,NewP,NewC);
	_ -&gt;
	    {error,F}
    end.
</pre></div>

<p>Here we also handle the mode (binary or list) that can be set by
the setopts request. By default, all OTP io_servers send data back to
the client as lists, but switching mode to binary might increase
efficiency if the server handles it in an appropriate way. The
implementation of get_until is hard to get efficient as the supplied
function is defined to take lists as arguments, but get_chars and
get_line can be optimized for binary mode. This example does not
optimize anything however. It is important though that the returned
data is of the right type depending on the options set, so we convert
the lists to binaries in the correct encoding <strong>if possible</strong>
before returning. The function supplied in the get_until request may,
as its final result return anything, so only functions actually
returning lists can get them converted to binaries. If the request
contained the encoding tag unicode, the lists can contain all unicode
codepoints and the binaries should be in UTF-8, if the encoding tag
was latin1, the client should only get characters in the range
0..255. The function check/2 takes care of not returning arbitrary
unicode codepoints in lists if the encoding was given as latin1. If
the function did not return a list, the check cannot be performed and
the result will be that of the supplied function untouched.</p>

<p>Now we are more or less done. We implement the utility functions below
to actually manipulate the table:</p>

<div class="example"><pre>
check(unicode, List) -&gt;
    List;
check(latin1, List) -&gt;
    try 
	[ throw(not_unicode) || X &lt;- List,
				X &gt; 255 ],
	List
    catch
	throw:_ -&gt;
	    {error,{cannot_convert, unicode, latin1}}
    end.
</pre></div>

<p>The function check takes care of providing an error tuple if unicode
codepoints above 255 is to be returned if the client requested
latin1.</p>

<p>The two functions until_newline/3 and until_enough/3 are helpers used 
together with the get_until function to implement get_chars and
get_line (inefficiently):</p>
	
<div class="example"><pre>
until_newline([],eof,_MyStopCharacter) -&gt;
    {done,eof,[]};
until_newline(ThisFar,eof,_MyStopCharacter) -&gt;
    {done,ThisFar,[]};
until_newline(ThisFar,CharList,MyStopCharacter) -&gt;
    case lists:splitwith(fun(X) -&gt; X =/= MyStopCharacter end,  CharList) of
	{L,[]} -&gt;
            {more,ThisFar++L};
	{L2,[MyStopCharacter|Rest]} -&gt;
	    {done,ThisFar++L2++[MyStopCharacter],Rest}
    end.

until_enough([],eof,_N) -&gt;
    {done,eof,[]};
until_enough(ThisFar,eof,_N) -&gt;
    {done,ThisFar,[]};
until_enough(ThisFar,CharList,N) 
  when length(ThisFar) + length(CharList) &gt;= N -&gt;
    {Res,Rest} = my_split(N,ThisFar ++ CharList, []),
    {done,Res,Rest};
until_enough(ThisFar,CharList,_N) -&gt;
    {more,ThisFar++CharList}. 
</pre></div>

<p>As can be seen, the functions above are just the type of functions
that should be provided in get_until requests.</p>

<p>Now we only need to read and write the table in an appropriate way to
complete the server:</p>

<div class="example"><pre>
get(P,Tab) -&gt;
    R = P div ?CHARS_PER_REC,
    C = P rem ?CHARS_PER_REC,
    case ets:lookup(Tab,R) of
	[] -&gt;
	    {P,eof};
	[{R,List}] -&gt;
	    case my_split(C,List,[]) of
		{_,[]} -&gt;
		    {P+length(List),eof};
		{_,Data} -&gt;
		    {P+length(Data),Data}
	    end
    end.

my_split(0,Left,Acc) -&gt;
    {lists:reverse(Acc),Left};
my_split(_,[],Acc) -&gt;
    {lists:reverse(Acc),[]};
my_split(N,[H|T],Acc) -&gt;
    my_split(N-1,T,[H|Acc]).

split_data([],_,_) -&gt;
    [];
split_data(Chars, Row, Col) -&gt;
    {This,Left} = my_split(?CHARS_PER_REC - Col, Chars, []),
    [ {Row, Col, This} | split_data(Left, Row + 1, 0) ].

apply_update(Table, {Row, Col, List}) -&gt;     
    case ets:lookup(Table,Row) of
	[] -&gt;
	    ets:insert(Table,{Row, lists:duplicate(Col,0) ++ List});
	[{Row, OldData}] -&gt;
	    {Part1,_} = my_split(Col,OldData,[]),
	    {_,Part2} = my_split(Col+length(List),OldData,[]),
	    ets:insert(Table,{Row, Part1 ++ List ++ Part2})
    end.
</pre></div>

<p>The table is read or written in chunks of ?CHARS_PER_REC, overwriting
when necessary. The implementation is obviously not efficient, it is
just working.</p>

<p>This concludes the example. It is fully runnable and you can read or
write to the io_server by using i.e. the io_module or even the file
module. It's as simple as that to implement a fully fledged io_server
in Erlang.</p>

</div>
<div class="footer">
<hr>
<p>Copyright © 1997-2012 Ericsson AB. All Rights Reserved.</p>
</div>
</div>
</div></body>
</html>