-
-
Save tsnow/1427880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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 recieved: ~p~n", [Nickname, MessageBody]), | |
handle_messages(Nickname); | |
stop -> | |
ok | |
end. | |
print_message(Who, MessageBody) -> | |
io:format("~p received: ~p~n", [Who, MessageBody]). | |
start_router() -> | |
message_router:start(). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(message_router). | |
-define(SERVER, message_router). | |
-compile(export_all). | |
start() -> | |
global:trans({?SERVER,?SERVER}, | |
fun() -> | |
case global:whereis_name(?SERVER) of | |
undefined -> | |
Pid = spawn(message_router, route_messages, [dict:new()]), | |
global:register_name(?SERVER, Pid); | |
_ -> | |
ok | |
end | |
end). | |
stop() -> | |
global:trans({?SERVER,?SERVER}, | |
fun() -> | |
case global:whereis_name(?SERVER) of | |
undefined -> | |
ok; | |
_ -> | |
global:send(?SERVER, shutdown) | |
end | |
end). | |
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}). | |
route_messages(Clients) -> | |
receive | |
{send_chat_msg, ClientName, MessageBody} -> | |
case dict:find(ClientName, Clients) of | |
{ok, ClientPid} -> | |
ClientPid ! {printmsg, MessageBody}; | |
error -> io:format("Error! Unknown Client: ~p~n", [ClientName]) | |
end, | |
route_messages(Clients); | |
{register_nick, ClientName, ClientPid} -> | |
route_messages(dict:store(ClientName, ClientPid,Clients)); | |
{unregister_nick, ClientName} -> | |
case dict:find(ClientName, Clients) of | |
{ok, ClientPid} -> | |
ClientPid ! stop, | |
route_messages(dict:erase(ClientName,Clients)); | |
error -> | |
io:format("Error! Unknown client ~p~n", [ClientName]), | |
route_messages(Clients) | |
end; | |
shutdown -> | |
io:format("Shutting down~n"); | |
Oops -> | |
io:format("Warning! Received: ~p~n", [Oops]), | |
route_messages(Clients) | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c(chat_client). | |
c(message_router). | |
chat_client:start_router(). | |
chat_client:register_nickname("Joe"). | |
chat_client:send_message("Joe", "hello, Joe"). | |
chat_client:send_message("Fred", "Where'd you go?"). % Unknown client. | |
chat_client:unregister_nickname("Joe"). | |
chat_client:send_message("Joe", "hello, Joe").%Unknown client. | |
% net_adm:ping('foo@tim-snowhites-macbook'). | |
% rpc:call('bar@tim-snowhites-macbook', erlang, date, []). | |
% global:registered_names(). %[message_router] | |
% do the above chat_client actions across both servers | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment