Search code examples
erlangmessage-passing

Erlang Undefined Function Global


This is my first time diving into Erlang, but I've come from from the world of Prolog.

I can't seem to understand why after loading the module, and telling it to execute a function, it seems to run off to another separate function.

The question Erlang - undefined function seems related, but doesn't look as if it applies in my case. I can compile and load the module(s).

Here is my Eshell output:

Erlang/OTP 20 [erts-9.1] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:10]

Eshell V9.1  (abort with ^G)
1> m(chat_client).
Module: chat_client
MD5: 32e278745489222df841e93620154031
Compiled: No compile time info available
Object file: h:/Program Files/erl9.1/bin/chat_client.beam
Compiler options:  []
Exports: 
         handle_messages/1
         module_info/0
         module_info/1
         register_nickname/1
         send_message/2
         start_router/0
         unregister_nickname/1
ok
2> chat_client:start_router().
yes
3> chat_client:register_nickname(myatom).
** exception error: undefined function global:send_message/2
4> l(chat_client).
{module,chat_client}
5> chat_client:start_router().           
ok
6> chat_client:register_nickname(myatom).
** exception error: undefined function global:send_message/2
7> l(message_router).                    
{module,message_router}
8> chat_client:start_router().           
ok
9> chat_client:register_nickname(myatom).
** exception error: undefined function global:send_message/2
10> 

In the chat_client.erl file, there is no match to send_message/2:

-module(chat_client).

-compile(export_all).

register_nickname(Nickname) ->
    Pid = spawn(chat_client, handle_messages, [Nickname]),
    message_router:register_nick(Nickname, Pid).

unregister_nickname(Nickname) ->
    message_router:unregister_nick(Nickname).

send_message(Addressee, MessageBody) ->
    message_router:send_chat_message(Addressee, MessageBody).

handle_messages(Nickname) ->
    receive
        { printmsg, MessageBody } ->
            io:format("~p received: ~p~n", [Nickname, MessageBody]),
            handle_messages(Nickname);
        stop ->
            ok
    end.

start_router() ->
message_router:start().

From the related file message_router.erl I can see a global function make a match (or so I think):

send_chat_message(Addressee, MessageBody) ->
    global:send_message(?SERVER, { send_chat_msg, Addressee, MessageBody }).

register_nick(ClientName, ClientPid) ->
    global:send_message(?SERVER, { register_nick, ClientName, ClientPid }).

unregister_nick(ClientName) ->
    global:send_message(?SERVER, { unregister_nick, ClientName }).

What am I doing wrong in this case?


Solution

  • In the module global, there is a function send/2, but no send_message/2. change your message_router to:

    send_chat_message(Addressee, MessageBody) ->
        global:send(?SERVER, { send_chat_msg, Addressee, MessageBody }).
    
    register_nick(ClientName, ClientPid) ->
        global:send(?SERVER, { register_nick, ClientName, ClientPid }).
    
    unregister_nick(ClientName) ->
        global:send(?SERVER, { unregister_nick, ClientName }).