Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. Channel.erl
  2. Client.erl
  3. Server.erl
  4. defs.hrl
  5.  
  6. channel.erl:58: head mismatch
  7. channel.erl:10: function loop/2 undefined
  8. error
  9.  
  10. -module(channel).
  11. -include_lib("./defs.hrl").
  12. -export([main/1, initial_state/1]).
  13.  
  14. %% Receives messages from the server and handles them accordingly.
  15.  
  16. main(State) ->
  17. receive
  18. {request, From, Ref, Request} ->
  19. {Response, NextState} = loop(State, Request),
  20. From ! {result, Ref, Response},
  21. main(NextState)
  22. end.
  23.  
  24. %% Produces the initial state.
  25.  
  26. initial_state(ChannelName) ->
  27. #ch_st{clients=[], name = ChannelName}.
  28.  
  29. %% ---------------------------------------------------------------------------
  30. %% Private functions
  31.  
  32. % connect user to channel
  33.  
  34. loop(St, {join, User}) ->
  35. {Nick, _} = User,
  36. case lists:keymember(Nick, 1, St#ch_st.users) of
  37. false ->
  38. ChangedSt = St#ch_st{clients = [ User | St#ch_st.users ]},
  39. {ok, ChangedSt};
  40. true ->
  41. {{error, user_already_joined}, St}
  42. end;
  43.  
  44. % Disconnet user from channel
  45.  
  46. loop(St, {leave, User}) ->
  47. {Nick, _} = User,
  48. case lists:keymember(Nick, 1, St#ch_st.users) of
  49. true ->
  50. ChangedSt = St#ch_st{clients = lists:delete(User, St#ch_st.users)},
  51. {ok, NewState};
  52. false ->
  53. {{error, user_not_joined}, St}
  54. end;
  55.  
  56. % Send Msg to all channel users
  57.  
  58. loop(St, {msg_from_client, Pid, Nick, Msg}) ->
  59. ToRecvMsg = lists:delete(Pid, St#ch_st.clients),
  60. State = St#ch_st.name,
  61. send_to_cl(ToRecvMsg, State, Nick, Msg),
  62. {ok, St};
  63.  
  64.  
  65. % Send Msg to all users in list.
  66.  
  67. send_to_cl([],State, Nick, Msg) ->
  68. ok;
  69.  
  70. send_to_cl([H|T], State, Nick, Msg) ->
  71. helper:requestAsync(H, {State, Nick, Msg}),
  72. send_to_cl(T, State, Nick, Msg).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement