<commit_before>-module(shirasu_websocket).
-author('<NAME> <<EMAIL>>').
-export([handle_websocket/2, wsManager/0]).

-include_lib("eunit/include/eunit.hrl").

handle_websocket(Ws) ->
    receive
            {browser, Data} ->
                    %?debugVal({"ws:browser", Data}),
                    wsManager ! {send, Ws:get(path), Data},
                    handle_websocket(Ws);
            closed ->
                    wsManager ! {del, Ws:get(path), self()},
                    %?debugVal("ws:close"),
                    exit(self(), kill);
            {send, Data} ->
                    %?debugVal({"ws:send", Data}),
                    Ws:send(Data),
                    handle_websocket(Ws);
            _Ignore ->
                    handle_websocket(Ws)
    after 5000 ->
            %Ws:send(["pushing!"]),
            handle_websocket(Ws)
    end.

handle_websocket(new, Ws) ->
    %?debugVal({"handle_websocket:new:", Ws:get(path)}),
    wsManager ! {add, Ws:get(path), self()},
    handle_websocket(Ws).

wsManager() ->
    register(wsManager, self()),
    process_flag(trap_exit, true),
    wsManager([]).
wsManager(ChannelList) ->
    receive
        {add, Channel, Pid} ->
            %?debugVal({add, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    case lists:member(Pid, PidList) of
                        true ->
                            wsManager(ChannelList);
                        false ->
                            NewPidList = lists:append([Pid], PidList),
                            NewChannelList = lists:keyreplace(Channel, 1, ChannelList, {Channel, NewPidList}),
                            %?debugVal(NewChannelList),
                            wsManager(NewChannelList)
                    end;
                false ->
                    wsManager(lists:append([{Channel, [Pid]}], ChannelList))
            end;
        {del, Channel, Pid} ->
            %?debugVal({del, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    case lists:member(Pid, PidList) of
                        true ->
                            NewPidList = lists:delete(Pid, PidList),
                            NewChannelList = lists:keyreplace(Channel, 1, ChannelList, {Channel, NewPidList}),
                            %?debugVal(NewChannelList),
                            wsManager(NewChannelList);
                        false ->
                            wsManager(ChannelList)
                    end;
                false ->
                    wsManager(ChannelList)
            end;
        {get, Channel, Pid} ->
            %?debugVal({get, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    Pid ! PidList;
                false ->
                    Pid ! false
            end,
            wsManager(ChannelList);
        {send, Channel, Data} ->
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {_Channel, PidList}} ->
                    lists:map(fun(PID) -> PID ! {send, Data} end, PidList);
                false ->
                    pass
            end,
            wsManager(ChannelList);
        {stat, Pid} ->
            Pid ! {stat, ChannelList},
            wsManager(ChannelList);
        stop -><commit_msg>Added debugVal for debug.<commit_after>-module(shirasu_websocket).
-author('<NAME> <<EMAIL>>').
-export([handle_websocket/2, wsManager/0]).

-include_lib("eunit/include/eunit.hrl").

handle_websocket(Ws) ->
    receive
            {browser, Data} ->
                    %?debugVal({"ws:browser", Data}),
                    wsManager ! {send, Ws:get(path), Data},
                    handle_websocket(Ws);
            closed ->
                    wsManager ! {del, Ws:get(path), self()},
                    %?debugVal("ws:close"),
                    exit(self(), kill);
            {send, Data} ->
                    %?debugVal({"ws:send", Data}),
                    Ws:send(Data),
                    handle_websocket(Ws);
            _Ignore ->
                    handle_websocket(Ws)
    after 5000 ->
            %Ws:send(["pushing!"]),
            handle_websocket(Ws)
    end.

handle_websocket(new, Ws) ->
    %?debugVal({"handle_websocket:new:", Ws:get(path)}),
    wsManager ! {add, Ws:get(path), self()},
    handle_websocket(Ws).

wsManager() ->
    register(wsManager, self()),
    process_flag(trap_exit, true),
    wsManager([]).
wsManager(ChannelList) ->
    receive
        {add, Channel, Pid} ->
            %?debugVal({add, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    case lists:member(Pid, PidList) of
                        true ->
                            wsManager(ChannelList);
                        false ->
                            NewPidList = lists:append([Pid], PidList),
                            NewChannelList = lists:keyreplace(Channel, 1, ChannelList, {Channel, NewPidList}),
                            %?debugVal(NewChannelList),
                            wsManager(NewChannelList)
                    end;
                false ->
                    wsManager(lists:append([{Channel, [Pid]}], ChannelList))
            end;
        {del, Channel, Pid} ->
            %?debugVal({del, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    case lists:member(Pid, PidList) of
                        true ->
                            NewPidList = lists:delete(Pid, PidList),
                            NewChannelList = lists:keyreplace(Channel, 1, ChannelList, {Channel, NewPidList}),
                            %?debugVal(NewChannelList),
                            wsManager(NewChannelList);
                        false ->
                            wsManager(ChannelList)
                    end;
                false ->
                    wsManager(ChannelList)
            end;
        {get, Channel, Pid} ->
            %?debugVal({get, Channel, Pid}),
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {Channel, PidList}} ->
                    Pid ! PidList;
                false ->
                    Pid ! false
            end,
            wsManager(ChannelList);
        {send, Channel, Data} ->
            case lists:keysearch(Channel, 1, ChannelList) of
                {value, {_Channel, PidList}} ->
                    %?debugVal(io:format("~p", [{Channel, PidList, Data}])),
                    lists:map(fun(PID) -> PID ! {send, Data} end, PidList);
                false ->
                    pass
            end,
            wsManager(ChannelList);
        {stat, Pid} ->
            Pid ! {stat, ChannelList},
            wsManager(ChannelList);
        stop ->
{'fraction_non_alphanumeric': 0.1261503610363868, 'fraction_numerical': 0.0033979895228656376, 'mean_word_length': 1.3198686371100163, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6406245', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1863, 'n_words': 363}