5 记录和宏
较大的程序通常被编写为包含多个文件,这些文件之间具有明确定义的接口。
5.1 将较大示例划分为多个文件
为了说明这一点,上一节中的信使示例被划分为以下五个文件
-
mess_config.hrl
配置数据的头文件
-
mess_interface.hrl
客户端和信使之间的接口定义
-
user_interface.erl
用户界面的函数
-
mess_client.erl
信使客户端的函数
-
mess_server.erl
信使服务器端的函数
在执行此操作时,Shell、客户端和服务器之间消息传递接口已清理,并使用**记录**进行定义。此外,还引入了**宏**
%%%----FILE mess_config.hrl---- %%% Configure the location of the server node, -define(server_node, messenger@super). %%%----END FILE----
%%%----FILE mess_interface.hrl---- %%% Message interface between client and server and client shell for %%% messenger program %%%Messages from Client to server received in server/1 function. -record(logon,{client_pid, username}). -record(message,{client_pid, to_name, message}). %%% {'EXIT', ClientPid, Reason} (client terminated or unreachable. %%% Messages from Server to Client, received in await_result/0 function -record(abort_client,{message}). %%% Messages are: user_exists_at_other_node, %%% you_are_not_logged_on -record(server_reply,{message}). %%% Messages are: logged_on %%% receiver_not_found %%% sent (Message has been sent (no guarantee) %%% Messages from Server to Client received in client/1 function -record(message_from,{from_name, message}). %%% Messages from shell to Client received in client/1 function %%% spawn(mess_client, client, [server_node(), Name]) -record(message_to,{to_name, message}). %%% logoff %%%----END FILE----
%%%----FILE user_interface.erl---- %%% User interface to the messenger program %%% login(Name) %%% One user at a time can log in from each Erlang node in the %%% system messenger: and choose a suitable Name. If the Name %%% is already logged in at another node or if someone else is %%% already logged in at the same node, login will be rejected %%% with a suitable error message. %%% logoff() %%% Logs off anybody at that node %%% message(ToName, Message) %%% sends Message to ToName. Error messages if the user of this %%% function is not logged on or if ToName is not logged on at %%% any node. -module(user_interface). -export([logon/1, logoff/0, message/2]). -include("mess_interface.hrl"). -include("mess_config.hrl"). logon(Name) -> case whereis(mess_client) of undefined -> register(mess_client, spawn(mess_client, client, [?server_node, Name])); _ -> already_logged_on end. logoff() -> mess_client ! logoff. message(ToName, Message) -> case whereis(mess_client) of % Test if the client is running undefined -> not_logged_on; _ -> mess_client ! #message_to{to_name=ToName, message=Message}, ok end. %%%----END FILE----
%%%----FILE mess_client.erl---- %%% The client process which runs on each user node -module(mess_client). -export([client/2]). -include("mess_interface.hrl"). client(Server_Node, Name) -> {messenger, Server_Node} ! #logon{client_pid=self(), username=Name}, await_result(), client(Server_Node). client(Server_Node) -> receive logoff -> exit(normal); #message_to{to_name=ToName, message=Message} -> {messenger, Server_Node} ! #message{client_pid=self(), to_name=ToName, message=Message}, await_result(); {message_from, FromName, Message} -> io:format("Message from ~p: ~p~n", [FromName, Message]) end, client(Server_Node). %%% wait for a response from the server await_result() -> receive #abort_client{message=Why} -> io:format("~p~n", [Why]), exit(normal); #server_reply{message=What} -> io:format("~p~n", [What]) after 5000 -> io:format("No response from server~n", []), exit(timeout) end. %%%----END FILE---
%%%----FILE mess_server.erl---- %%% This is the server process of the messenger service -module(mess_server). -export([start_server/0, server/0]). -include("mess_interface.hrl"). server() -> process_flag(trap_exit, true), server([]). %%% the user list has the format [{ClientPid1, Name1},{ClientPid22, Name2},...] server(User_List) -> io:format("User list = ~p~n", [User_List]), receive #logon{client_pid=From, username=Name} -> New_User_List = server_logon(From, Name, User_List), server(New_User_List); {'EXIT', From, _} -> New_User_List = server_logoff(From, User_List), server(New_User_List); #message{client_pid=From, to_name=To, message=Message} -> server_transfer(From, To, Message, User_List), server(User_List) end. %%% Start the server start_server() -> register(messenger, spawn(?MODULE, server, [])). %%% Server adds a new user to the user list server_logon(From, Name, User_List) -> %% check if logged on anywhere else case lists:keymember(Name, 2, User_List) of true -> From ! #abort_client{message=user_exists_at_other_node}, User_List; false -> From ! #server_reply{message=logged_on}, link(From), [{From, Name} | User_List] %add user to the list end. %%% Server deletes a user from the user list server_logoff(From, User_List) -> lists:keydelete(From, 1, User_List). %%% Server transfers a message between user server_transfer(From, To, Message, User_List) -> %% check that the user is logged on and who he is case lists:keysearch(From, 1, User_List) of false -> From ! #abort_client{message=you_are_not_logged_on}; {value, {_, Name}} -> server_transfer(From, Name, To, Message, User_List) end. %%% If the user exists, send the message server_transfer(From, Name, To, Message, User_List) -> %% Find the receiver and send the message case lists:keysearch(To, 2, User_List) of false -> From ! #server_reply{message=receiver_not_found}; {value, {ToPid, To}} -> ToPid ! #message_from{from_name=Name, message=Message}, From ! #server_reply{message=sent} end. %%%----END FILE---
5.2 头文件
如上所示,一些文件扩展名为 .hrl。这些是头文件,通过以下方式包含在 .erl 文件中:
-include("File_Name").
例如
-include("mess_interface.hrl").
在上面的情况下,该文件是从与信使示例中所有其他文件相同的目录中获取的。(*手册*)。
.hrl 文件可以包含任何有效的 Erlang 代码,但最常用于记录和宏定义。
5.3 记录
记录被定义为
-record(name_of_record,{field_name1, field_name2, field_name3, ......}).
例如
-record(message_to,{to_name, message}).
这等效于
{message_to, To_Name, Message}
创建记录最好通过示例说明
#message_to{message="hello", to_name=fred)
这将创建
{message_to, fred, "hello"}
请注意,您不必担心在创建记录时为记录的各个部分分配值的顺序。使用记录的优势在于,通过将它们的定义放在头文件中,您可以方便地定义易于更改的接口。例如,如果您想在记录中添加一个新字段,您只需要更改使用新字段的代码,而不需要在引用记录的每个地方都更改。如果您在创建记录时省略了一个字段,它将获得原子 undefined 的值。(*手册*)
使用记录进行模式匹配与创建记录非常相似。例如,在 case 或 receive 内部
#message_to{to_name=ToName, message=Message} ->
这与以下相同
{message_to, ToName, Message}
5.4 宏
添加到信使中的另一项是宏。文件 mess_config.hrl 包含以下定义:
%%% Configure the location of the server node, -define(server_node, messenger@super).
此文件包含在 mess_server.erl 中
-include("mess_config.hrl").
现在,mess_server.erl 中的每个 ?server_node 都会被替换为 messenger@super。
在生成服务器进程时也使用宏
spawn(?MODULE, server, [])
这是一个标准宏(即,由系统定义,而不是由用户定义)。?MODULE 始终被替换为当前模块的名称(即,文件开头附近的 -module 定义)。还有一些更高级的使用宏的方法,例如使用参数(*手册*)。
信使示例中的三个 Erlang (.erl) 文件分别编译成目标代码文件 (.beam)。Erlang 系统在执行代码时引用这些文件时,会将这些文件加载并链接到系统中。在这种情况下,它们只是被放在我们的当前工作目录中(即,您执行了 "cd" 的位置)。还有一些方法可以将 .beam 文件放在其他目录中。
在信使示例中,没有关于正在发送的消息的任何假设。它可以是任何有效的 Erlang 项。