1
votes

Ive been trying to write a simple concurrency exercise in Erlang that invlolves communicating across different terminals/shells.

However this error appears everytime I run init_chat() and entering my name.

=ERROR REPORT==== 15-Nov-2021::08:13:11.849169 === Error in process <0.91.0> on node kei@osboxes with exit value: {undef,[{chat,chat_handle,"Jeano",[]}]}

I have no idea what I'm doing wrong.

The whole program:

-module(chat).
-compile(export_all).

init_chat() ->
    In = io:get_line("Name please: "),
    Name = string:trim(In),
    register(chat_handle, spawn(chat, chat_handle, [Name])).

chat_handle(Name) ->
    spawn(chat, message_handle, [Name]),
    receive
        {message, Sender, Message} ->
            io:format("~p: ~p", [Sender, Message]),
            chat_handle(Name);
        {dc, Sender} ->
            io:format("~p Has disconnected. ~n", [Sender]),
            chat_handle(Name);
        quit ->
            io:format("Disconnecting... ~n"),
            erlang:halt()
    end.

message_handle(Name) ->
    Message = io:get_line("You: "),
    if
        Message == "bye/n" ->
            disconnect(nodes(), Name);
        true ->
            send_message(nodes(), Name, Message)
    end.

send_message([Head | Tail], Name, Message) ->
    {chat_handle, Head} ! {message, Name, Message},
    send_message(Tail, Name, Message);
send_message([], Name, Message) ->
    message_handle(Name).

disconnect([Head | Tail], Name) ->
    {chat_handle, Head} ! {dc, Name},
    disconnect(Tail, Name);
disconnect([], Name) ->
    {chat_handle, node()} ! quit.

1
I think Richard probably hit the exact problem you have in this case, but more generally introduction to chat systems in Erlang are really good to explore when you're new, and to that end you may find this series (and related repo) useful to explore: rumble.com/ve8h9r - zxq9

1 Answers

2
votes

The error message says {chat,chat_handle,"Jeano", ... (which is missing list brackets around the string) but your code actually says spawn(chat, chat_handle, [Name]), which looks correct, so I think you just haven't reloaded the recompiled version of the module.