Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Channel.erl
- Client.erl
- Server.erl
- defs.hrl
- channel.erl:58: head mismatch
- channel.erl:10: function loop/2 undefined
- error
- -module(channel).
- -include_lib("./defs.hrl").
- -export([main/1, initial_state/1]).
- %% Receives messages from the server and handles them accordingly.
- main(State) ->
- receive
- {request, From, Ref, Request} ->
- {Response, NextState} = loop(State, Request),
- From ! {result, Ref, Response},
- main(NextState)
- end.
- %% Produces the initial state.
- initial_state(ChannelName) ->
- #ch_st{clients=[], name = ChannelName}.
- %% ---------------------------------------------------------------------------
- %% Private functions
- % connect user to channel
- loop(St, {join, User}) ->
- {Nick, _} = User,
- case lists:keymember(Nick, 1, St#ch_st.users) of
- false ->
- ChangedSt = St#ch_st{clients = [ User | St#ch_st.users ]},
- {ok, ChangedSt};
- true ->
- {{error, user_already_joined}, St}
- end;
- % Disconnet user from channel
- loop(St, {leave, User}) ->
- {Nick, _} = User,
- case lists:keymember(Nick, 1, St#ch_st.users) of
- true ->
- ChangedSt = St#ch_st{clients = lists:delete(User, St#ch_st.users)},
- {ok, NewState};
- false ->
- {{error, user_not_joined}, St}
- end;
- % Send Msg to all channel users
- loop(St, {msg_from_client, Pid, Nick, Msg}) ->
- ToRecvMsg = lists:delete(Pid, St#ch_st.clients),
- State = St#ch_st.name,
- send_to_cl(ToRecvMsg, State, Nick, Msg),
- {ok, St};
- % Send Msg to all users in list.
- send_to_cl([],State, Nick, Msg) ->
- ok;
- send_to_cl([H|T], State, Nick, Msg) ->
- helper:requestAsync(H, {State, Nick, Msg}),
- send_to_cl(T, State, Nick, Msg).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement