This is my client code: -module(client).…
- User
This is my client code: -module(client). -export([start/1,add_remote/1,send_msg/4,stop_client/1]).
start(Client) -> register(Client,spawn(fun() -> loop() end)).
% c(client). % client:start(elsa). % client:add_remote('server1@LAPTOP-FELLF17T'). %pong %client:send_msg(elsa,central,'server1@LAPTOP-FELLF17T',ola).
% c(client). % client:start(sofia). % client:add_remote('server1@LAPTOP-FELLF17T'). %pong %client:send_msg(sofia,central,'server1@LAPTOP-FELLF17T',ola).
%%%%%%%%%%%%%%%%%%%%%%%%% % Interface to client % %%%%%%%%%%%%%%%%%%%%%%%%% %Add remote machine to known ones. add_remote(RemoteMachine) -> net_adm:ping(RemoteMachine). % Sends Message send_msg(Client,Server,RemoteMachine,Message)-> Client ! {send,Server,RemoteMachine,Message}. % Stop client stop_client(Client) -> Client ! {stop_client}.
%%%%%%%%%%%%%%%%%%%%%%%%% % Main loop % %%%%%%%%%%%%%%%%%%%%%%%%% loop() -> receive {send,Server,RemoteMachine,Message} -> {Server,RemoteMachine} ! {self(),Message}, receive {_,Reply} -> io:format("Received from server:
pn",[Reply]) end, loop(); {stop_client} -> io:format("Cliente exiting...") end.and this is my server code:
-module(server). -export([start/1]).
% c(server). % server:start(central).
start(Server) -> register(Server,spawn(fun() -> loop() end)).
loop() -> receive {From, stop} -> io:format("Received from ~p message to stop!~n",[From]), From ! {self(),server_disconnect}; {From, Msg} -> io:format("Received ~p:
pn",[From,Msg]), io:format("Sending reply...~n"), From ! {self(),happy_to_receive_your_message}, loop() end. - Assistant
This code contains a basic Erlang implementation for a client and server communicating with each other. Let's go step by step through the code to understand what it does.
Client module:
- User
make it so that the server forwards the sent message to all the users that already sent a message.
- Assistant
To forward the sent messages to all the users, we need to maintain a list of users that have already sent a message and then forward the received message to all those users. Here's how you can modify the server code to achieve this functionality:
- User
make it so that the client is also actively listening to messages from the server so that when the servers forwards the message all the clients that receive print it right way
- Assistant
To make the client actively listen for messages from the server, you can add a separate process in the client module that listens for incoming messages. Here's how you can modify the client code to achieve this functionality:
-module(client). -export([start/1,add_remote/1,send_msg/4,stop_client/1]). start(Client) -> register(Client,spawn(fun() -> loop() end)), spawn(fun() -> message_listener(Client) end). % c(client). % client:start(elsa). % client:add_remote('server1@LAPTOP-FELLF17T'). %pong %client:send_msg(elsa,central,'server1@LAPTOP-FELLF17T',ola). % c(client). % client:start(sofia). % client:add_remote('server1@LAPTOP-FELLF17T'). %pong %client:send_msg(sofia,central,'server1@LAPTOP-FELLF17T',ola). %%%%%%%%%%%%%%%%%%%%%%%%% % Interface to client % %%%%%%%%%%%%%%%%%%%%%%%%% %Add remote machine to known ones. add_remote(RemoteMachine) -> net_adm:ping(RemoteMachine). % Sends Message send_msg(Client,Server,RemoteMachine,Message)-> Client ! {send,Server,RemoteMachine,Message}. % Stop client stop_client(Client) -> Client ! {stop_client}. %%%%%%%%%%%%%%%%%%%%%%%%% % Main loop % %%%%%%%%%%%%%%%%%%%%%%%%% loop() -> receive {send,Server,RemoteMachine,Message} -> {Server,RemoteMachine} ! {self(),Message}, loop(); {stop_client} -> io:format("Cliente exiting...~n"), exit(normal) end. message_listener(Client) -> receive {Sender, Message} -> io:format("~p received from ~p~n", [Message, Sender]), message_listener(Client) end. - User
It's not printing any message. Any help? Do I need to change anything on the server side?
- Assistant
Apologies for that oversight. You are right; there is a need to make a modification in the server code to include the registered client name instead of the process ID. Modify the
forward_message_to_users/3function in the server code as follows:forward_message_to_users(_, [], _) -> ok; forward_message_to_users(Message, [User | Rest], Sender) -> case User /= Sender of true -> User ! {registered_name(Sender), Message}; false -> ok end, forward_message_to_users(Message, Rest, Sender). registered_name(Pid) -> case erlang:process_info(Pid, registered_name) of {registered_name, Name} -> Name; _ -> Pid end.In the
forward_message_to_users/3function, replace the lineUser ! {Sender, Message};withUser ! {registered_name(Sender), Message};. This modification ensures that the registered client name is included in the message.Also, add a new function
registered_name/1that checks for the registered name associated with the process ID of the sender. If a registered name is present, it will return the name; otherwise, it will return the process ID itself. - User
(server1@Zes-MacBook-Air)4> =ERROR REPORT==== 1-Jun-2023::03:52:04.821769 === Error in process <0.99.0> on node 'server1@Zes-MacBook-Air' with exit value: {badarg,[{erlang,process_info, [<13906.95.0>,registered_name], [{error_info,#{module => erl_erts_errors}}]}, {server,registered_name,1,[{file,"server.erl"},{line,40}]}, {server,forward_message_to_users,3,[{file,"server.erl"},{line,33}]}, {server,loop,1,[{file,"server.erl"},{line,23}]}]}
gave this error the server
- Assistant
Apologies for the error. The issue is that we are trying to call
erlang:process_info/2on a remote process, which is not allowed.To avoid this error, we can store both client Pid and Registered name in our Users list. Here's the updated server code:
-module(server). -export([start/1]). % c(server). % server:start(central). start(Server) -> register(Server,spawn(fun() -> loop([]) end)). loop(Users) -> receive {From, stop} -> NewUsers = lists:keydelete(From, 1, Users), io:format("Received from ~p message to stop! Removing from user's list~n",[From]), From ! {self(), server_disconnect}, loop(NewUsers); {From, Msg} -> io:format("Received ~p: ~p~n",[From,Msg]), io:format("Sending reply and forwarding message to other users...~n"), {NewUsers, UpdateOccurred} = update_users_list(Users, From), case UpdateOccurred of true -> From ! {self(), server_message_received}; false -> ok end, forward_message_to_users(Msg, NewUsers, From), loop(NewUsers) end. update_users_list(Users, Pid) -> case lists:keyfind(Pid, 1, Users) of {Pid, _} -> {Users, false}; false -> {[(Pid, registered_name(Pid)) | Users], true} end. registered_name(Pid) -> case erlang:process_info(Pid, registered_name) of {registered_name, Name} -> Name; _ -> Pid end. forward_message_to_users(_, [], _) -> ok; forward_message_to_users(Message, [{UserPid, UserName} | Rest], Sender) -> case UserPid /= Sender of true -> UserPid ! {registered_name(Sender), Message}; false -> ok end, forward_message_to_users(Message, Rest, Sender).