Chat server: Difference between revisions
Content added Content deleted
(Added Prolog Implementation) |
m (quick edit to prompt for a different username instead of disconnecting.) |
||
Line 2,125: | Line 2,125: | ||
% client can be broadcast to. |
% client can be broadcast to. |
||
handle_connection(Str) :- |
handle_connection(Str) :- |
||
send_msg(Str, msg_welcome, []), |
send_msg(Str, msg_welcome, []), |
||
repeat, |
|||
read_line_to_string(Str, Name), |
|||
send_msg(Str, |
send_msg(Str, msg_username, []), |
||
read_line_to_string(Str, Name), |
|||
connect_user(Name, Str), !. |
|||
% connections are stored here |
% connections are stored here |
||
Line 2,135: | Line 2,136: | ||
connect_user(Name, Str) :- |
connect_user(Name, Str) :- |
||
connected(Name, _), |
connected(Name, _), |
||
send_msg(Str, msg_username_taken, []) |
send_msg(Str, msg_username_taken, []), |
||
fail. |
|||
connect_user(Name, Str) :- |
connect_user(Name, Str) :- |
||
\+ connected(Name, _), |
\+ connected(Name, _), |
||
send_msg(Str, msg_welcome_name, Name), |
|||
% make sure that the connection is removed when the client leaves. |
% make sure that the connection is removed when the client leaves. |
||
setup_call_cleanup( |
setup_call_cleanup( |
||
Line 2,171: | Line 2,175: | ||
% constants for the various message types that are sent |
% constants for the various message types that are sent |
||
msg_welcome('Welcome to Chatalot\n\ |
msg_welcome('Welcome to Chatalot\n\r'). |
||
msg_username('Please enter your nickname: '). |
|||
msg_welcome_name('Welcome ~p\n\r'). |
msg_welcome_name('Welcome ~p\n\r'). |
||
msg_joined(' -- "~w" has joined the chat --'). |
msg_joined(' -- "~w" has joined the chat --'). |
||
msg_left(' -- "~w" has left the chat. --'). |
msg_left(' -- "~w" has left the chat. --'). |
||
msg_username_taken('That username is already taken\n\r'). |
msg_username_taken('That username is already taken, choose another\n\r'). |
||
msg_new_line('\n\r'). |
msg_new_line('\n\r'). |
||
msg_by_user('~w> ~w').</lang> |
msg_by_user('~w> ~w').</lang> |