1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2003</year><year>2009</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
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/.
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
24
<title>Records and Macros</title>
29
<file>record_macros.xml</file>
31
<p>Larger programs are usually written as a collection of files with
32
a well defined interface between the various parts.</p>
35
<title>The Larger Example Divided into Several Files</title>
36
<p>To illustrate this, we will divide the messenger example from
37
the previous chapter into five files.</p>
39
<tag><c>mess_config.hrl</c></tag>
40
<item>header file for configuration data</item>
41
<tag><c>mess_interface.hrl</c></tag>
42
<item>interface definitions between the client and the messenger</item>
43
<tag><c>user_interface.erl</c></tag>
44
<item>functions for the user interface</item>
45
<tag><c>mess_client.erl</c></tag>
46
<item>functions for the client side of the messenger</item>
47
<tag><c>mess_server.erl</c></tag>
48
<item>functions for the server side of the messenger</item>
50
<p>While doing this we will also clean up the message passing
51
interface between the shell, the client and the server and define
52
it using <em>records</em>, we will also introduce <em>macros</em>.</p>
54
%%%----FILE mess_config.hrl----
56
%%% Configure the location of the server node,
57
-define(server_node, messenger@super).
59
%%%----END FILE----</code>
61
%%%----FILE mess_interface.hrl----
63
%%% Message interface between client and server and client shell for
66
%%%Messages from Client to server received in server/1 function.
67
-record(logon,{client_pid, username}).
68
-record(message,{client_pid, to_name, message}).
69
%%% {'EXIT', ClientPid, Reason} (client terminated or unreachable.
71
%%% Messages from Server to Client, received in await_result/0 function
72
-record(abort_client,{message}).
73
%%% Messages are: user_exists_at_other_node,
74
%%% you_are_not_logged_on
75
-record(server_reply,{message}).
76
%%% Messages are: logged_on
77
%%% receiver_not_found
78
%%% sent (Message has been sent (no guarantee)
79
%%% Messages from Server to Client received in client/1 function
80
-record(message_from,{from_name, message}).
82
%%% Messages from shell to Client received in client/1 function
83
%%% spawn(mess_client, client, [server_node(), Name])
84
-record(message_to,{to_name, message}).
87
%%%----END FILE----</code>
89
%%%----FILE user_interface.erl----
91
%%% User interface to the messenger program
93
%%% One user at a time can log in from each Erlang node in the
94
%%% system messenger: and choose a suitable Name. If the Name
95
%%% is already logged in at another node or if someone else is
96
%%% already logged in at the same node, login will be rejected
97
%%% with a suitable error message.
100
%%% Logs off anybody at at node
102
%%% message(ToName, Message)
103
%%% sends Message to ToName. Error messages if the user of this
104
%%% function is not logged on or if ToName is not logged on at
107
-module(user_interface).
108
-export([logon/1, logoff/0, message/2]).
109
-include("mess_interface.hrl").
110
-include("mess_config.hrl").
113
case whereis(mess_client) of
115
register(mess_client,
116
spawn(mess_client, client, [?server_node, Name]));
117
_ -> already_logged_on
121
mess_client ! logoff.
123
message(ToName, Message) ->
124
case whereis(mess_client) of % Test if the client is running
127
_ -> mess_client ! #message_to{to_name=ToName, message=Message},
131
%%%----END FILE----</code>
133
%%%----FILE mess_client.erl----
135
%%% The client process which runs on each user node
137
-module(mess_client).
139
-include("mess_interface.hrl").
141
client(Server_Node, Name) ->
142
{messenger, Server_Node} ! #logon{client_pid=self(), username=Name},
146
client(Server_Node) ->
150
#message_to{to_name=ToName, message=Message} ->
151
{messenger, Server_Node} !
152
#message{client_pid=self(), to_name=ToName, message=Message},
154
{message_from, FromName, Message} ->
155
io:format("Message from ~p: ~p~n", [FromName, Message])
159
%%% wait for a response from the server
162
#abort_client{message=Why} ->
163
io:format("~p~n", [Why]),
165
#server_reply{message=What} ->
166
io:format("~p~n", [What])
168
io:format("No response from server~n", []),
172
%%%----END FILE---</code>
174
%%%----FILE mess_server.erl----
176
%%% This is the server process of the messenger service
178
-module(mess_server).
179
-export([start_server/0, server/0]).
180
-include("mess_interface.hrl").
183
process_flag(trap_exit, true),
186
%%% the user list has the format [{ClientPid1, Name1},{ClientPid22, Name2},...]
188
io:format("User list = ~p~n", [User_List]),
190
#logon{client_pid=From, username=Name} ->
191
New_User_List = server_logon(From, Name, User_List),
192
server(New_User_List);
194
New_User_List = server_logoff(From, User_List),
195
server(New_User_List);
196
#message{client_pid=From, to_name=To, message=Message} ->
197
server_transfer(From, To, Message, User_List),
203
register(messenger, spawn(?MODULE, server, [])).
205
%%% Server adds a new user to the user list
206
server_logon(From, Name, User_List) ->
207
%% check if logged on anywhere else
208
case lists:keymember(Name, 2, User_List) of
210
From ! #abort_client{message=user_exists_at_other_node},
213
From ! #server_reply{message=logged_on},
215
[{From, Name} | User_List] %add user to the list
218
%%% Server deletes a user from the user list
219
server_logoff(From, User_List) ->
220
lists:keydelete(From, 1, User_List).
222
%%% Server transfers a message between user
223
server_transfer(From, To, Message, User_List) ->
224
%% check that the user is logged on and who he is
225
case lists:keysearch(From, 1, User_List) of
227
From ! #abort_client{message=you_are_not_logged_on};
228
{value, {_, Name}} ->
229
server_transfer(From, Name, To, Message, User_List)
231
%%% If the user exists, send the message
232
server_transfer(From, Name, To, Message, User_List) ->
233
%% Find the receiver and send the message
234
case lists:keysearch(To, 2, User_List) of
236
From ! #server_reply{message=receiver_not_found};
237
{value, {ToPid, To}} ->
238
ToPid ! #message_from{from_name=Name, message=Message},
239
From ! #server_reply{message=sent}
242
%%%----END FILE---</code>
246
<title>Header Files</title>
247
<p>You will see some files above with extension <c>.hrl</c>. These
248
are header files which are included in the <c>.erl</c> files by:</p>
250
-include("File_Name").</code>
253
-include("mess_interface.hrl").</code>
254
<p>In our case above the file is fetched from the same directory as
255
all the other files in the messenger example. (*manual*).</p>
256
<p>.hrl files can contain any valid Erlang code but are most often
257
used for record and macro definitions.</p>
261
<title>Records</title>
262
<p>A record is defined as:</p>
264
-record(name_of_record,{field_name1, field_name2, field_name3, ......}).</code>
267
-record(message_to,{to_name, message}).</code>
268
<p>This is exactly equivalent to:</p>
270
{message_to, To_Name, Message}</code>
271
<p>Creating record, is best illustrated by an example:</p>
273
#message_to{message="hello", to_name=fred)</code>
274
<p>This will create:</p>
276
{message_to, fred, "hello"}</code>
277
<p>Note that you don't have to worry about the order you assign
278
values to the various parts of the records when you create it.
279
The advantage of using records is that by placing their
280
definitions in header files you can conveniently define
281
interfaces which are easy to change. For example, if you want to
282
add a new field to the record, you will only have to change
283
the code where the new field is used and not at every place
284
the record is referred to. If you leave out a field when creating
285
a record, it will get the value of the atom undefined. (*manual*)</p>
286
<p>Pattern matching with records is very similar to creating
287
records. For example inside a <c>case</c> or <c>receive</c>:</p>
289
#message_to{to_name=ToName, message=Message} -></code>
290
<p>is the same as:</p>
292
{message_to, ToName, Message}</code>
296
<title>Macros</title>
297
<p>The other thing we have added to the messenger is a macro.
298
The file <c>mess_config.hrl</c> contains the definition:</p>
300
%%% Configure the location of the server node,
301
-define(server_node, messenger@super).</code>
302
<p>We include this file in mess_server.erl:</p>
304
-include("mess_config.hrl").</code>
305
<p>Every occurrence of <c>?server_node</c> in <c>mess_server.erl</c>
306
will now be replaced by <c>messenger@super</c>.</p>
307
<p>The other place a macro is used is when we spawn the server
310
spawn(?MODULE, server, [])</code>
311
<p>This is a standard macro (i.e. defined by the system, not
312
the user). <c>?MODULE</c> is always replaced by the name of
313
current module (i.e. the <c>-module</c> definition near the start
314
of the file). There are more advanced ways of using macros with,
315
for example parameters (*manual*).</p>
316
<p>The three Erlang (<c>.erl</c>) files in the messenger example are
317
individually compiled into object code file (<c>.beam</c>).
318
The Erlang system loads and links these files into the system
319
when they are referred to during execution of the code. In our
320
case we simply have put them in the same directory which is our
321
current working directory (i.e. the place we have done "cd" to).
322
There are ways of putting the <c>.beam</c> files in other
324
<p>In the messenger example, no assumptions have been made about
325
what the message being sent is. It could be any valid Erlang term.</p>