Erlang节点配置

时间:2014-05-04 18:29:21

标签: concurrency erlang

我正在研究Erlang网站上关于并发的示例。我目前正在研究3.5节,它实现了一个简单的聊天信使。 这是代码:

    -module(messenger).
    -export([start_server/0, server/1, goOnline/1, goOffline/0, message/2, client/2]).
    server_node() ->
        messenger@bill.
    server(User_List) ->
        receive
            {From, logon, Name} ->
                New_User = server_goOnline(From, Name, User_List),
                server(New_User);
            {From, logoff} ->
                New_User = server_goOffline(From, User_List),
                server(New_User);
            {From, message_to, To, Message} ->
                server_transfer(From, To, Message, User_List),
                io:format("list is now: ~p~n", [User_List]),
                server(User_List)
        end.

    start_server() ->
        register(messenger, spawn(messager, server, [[]])).

    server_goOnline(From, Name, User_List) ->
        case lists:keymember(Name, 2, User_List) of
            true ->
                From ! {messenger, stop, user_exists_at_other_node},
                User_List;
            false ->
                From ! {messenger, logged_on},
                [{From, Name} | User_List]
        end.

    server_goOffline(From, User_List) ->
        lists:keydelete(From, 1, User_List).

    server_transfer(From, To, Message, User_List) ->
        case lists:keysearch(From, 1, User_List) of
            false ->
                From ! {messenger, stop, you_are_not_logged_on};
            {value, {From, Name}} ->
                server_transfer(From, Name, To, Message, User_List)
        end.

server_transfer(From, Name, To, Message, User_List) ->
    case lists:keysearch(To, 2, User_List) of
        false ->
            From ! {messenger, receiver_not_found};
        {value, {ToPid, To}} ->
            ToPid ! {message_from, Name, Message},
            From ! {messenger, sent}
   end.

goOnline(Name) ->
    case whereis(mess_client) of
        undefined ->
            register(mess_client,
                     spawn(messenger, client, [server_node(), Name]));
        _ -> already_logged_on
    end.

goOffline() ->
    mess_client ! logoff.

message(ToName, Message) ->
    case whereis(mess_client) of
        undefined ->
            not_logged_on;
        _ -> mess_client ! {message_to, ToName, Message},
            ok
end.

client(Server_Node, Name) ->
    {messenger, Server_Node} ! {self(), goOnline, Name},
    await_result(),
    client(Server_Node).

client(Server_Node) ->
    receive
        logoff ->
            {messenger, Server_Node} ! {self(), logoff},
            exit(normal);
        {message_to, ToName, Message} ->
            {messenger, Server_Node} ! {self(), message_to, ToName, Message},
            await_result();
        {message_from, FromName, Message} ->
            io:format("Message From ~p: ~p~n", [FromName, Message])
        end,
       client(Server_Node).

await_result() ->
    receive
        {messenger, stop, Why} ->
            io:format("~p~n", [Why]),
            exit(normal);
        {messneger, What} ->
            io:format("~p~n", [What])
    end.

教程说配置server_node() - >到我的messenger服务器运行的节点。如何配置该功能?我不知道这意味着什么,我的问题是如何配置server_node来为messenger注册我的服务器位置。

1 个答案:

答案 0 :(得分:1)

如果使用命令erl -sname somename启动erlang,则应该提示表单:

(somename@pcname)1>

只需替换代码

server_node() ->
        messenger@bill.

通过

server_node() ->
        somename@pcname.

并重新编译代码。