-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.erl
59 lines (50 loc) · 1.79 KB
/
channel.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
-module(channel).
-export([start/2]).
-record(channelState, {
name,
users
}).
start(Name, InitialUser) ->
genserver:start(list_to_atom(Name), #channelState{ name=Name, users=[ InitialUser ]}, fun handle_channel/2) .
% Functions for handling requests to a channel-process.
% JOIN
handle_channel(State = #channelState{users = UsersInChannel}, {join, NewPid}) ->
% Joining a channel, if the user is already joined an error is returned.
IsMember = lists:member(NewPid, UsersInChannel),
if IsMember ->
{reply, error, State};
true ->
{reply, join, State#channelState{users = [ NewPid | UsersInChannel ]}}
end;
% LEAVE
handle_channel(State = #channelState{users = UsersInChannel}, {leave, Sender}) ->
% User wants to leave the channel. Respond with an error if user is not in channel.
IsMember = lists:member(Sender, UsersInChannel),
if IsMember ->
NewUsers = lists:delete(Sender, UsersInChannel),
{reply, leave, State#channelState{users = NewUsers}};
true ->
{reply, error, State}
end;
% SEND MESSAGE
handle_channel(State = #channelState{name = ChannelName, users = UsersInChannel}, {message_send, Nick, Msg, Sender}) ->
% Sending a message to all clients connected to the channel.
% As above, if a user is not a member of channel, an error is sent.
IsMember = lists:member(Sender, UsersInChannel),
case IsMember of
true ->
spawn(
fun() ->
[ genserver:request(
Receiver,
{message_receive, ChannelName, Nick, Msg}
) || Receiver <- UsersInChannel, Receiver =/= Sender]
end
),
{reply, message_send, State};
false ->
{reply, error, State}
end ;
% Catch-all for any unhandled requests
handle_channel(State, _) ->
{reply, error, State} .