<commit_before>-module(ubf_plugin_handler).

%% The plugin handler handles request for the instance of the plugin

-export([start_handler/0, start_manager/2, manager/2]).

%%----------------------------------------------------------------------
%% Handler stuff

start_handler() ->
    proc_utils:spawn_link_debug(fun() -> wait() end, ?MODULE).

wait() ->
    receive
        {start, Contract, Server, Mod} ->
            loop(Contract, start, [], Server, Mod);
        stop ->
            exit({serverPluginHandler, stop})
    end.

loop(Client, State1, Data, Manager, Mod) ->
    receive
        {_Pid, {rpc, Q}} ->
            if Manager /= undefined ->
                    case (catch Mod:handlerRpc(State1, Q, Data, Manager)) of
                        {Reply, State2, Data2} ->
                            Client ! {self(), {rpcReply, Reply, State2, same}},
                            loop(Client, State2, Data2, Manager, Mod);
                        {changeContract, Reply, State1,
                         HandlerMod, State2, Data2, ManPid} ->
                            Client ! {self(), {rpcReply, Reply, State1,
                                               {new, HandlerMod, State2}}},
                            loop(Client, State2, Data2, ManPid, HandlerMod);
                        {'EXIT', Why} ->
                            exit({serverPluginHandler, Why})
                    end;
               true ->
                    DispatchArgs =
                        if is_tuple(Q) ->
                                erlang:tuple_to_list(Q);
                           is_atom(Q) ->
                                [Q];
                           true ->
                                Why1 = {bad_dispatch_args, Q},
                                exit({serverPluginHandler, Why1})
                        end,
                    case (catch Mod:handlerDispatch(hd(DispatchArgs), Data)) of
                        {DispatchMod, DispatchFun, Data2} ->
                            case (catch erlang:apply(DispatchMod, DispatchFun, tl(DispatchArgs))) of
                                {'EXIT', Why2} ->
                                    exit({serverPluginHandler, Why2});
                                Reply ->
                                    Client ! {self(), {rpcReply, Reply, State1, same}},
                                    loop(Client, State1, Data2, Manager, Mod)
                            end;
                        {'EXIT', Why3} ->
                            exit({serverPluginHandler, Why3});
                        Data2 ->
                            %% special case - hard-coded not_implemented reply
                            Reply = not_implemented,
                            Client ! {self(), {rpcReply, Reply, State1, same}},
                            loop(Client, State1, Data2, Manager, Mod)
                    end
            end;
        {event, X} ->
            Client ! {event, X},
            loop(Client, State1, Data, Manager, Mod);
        stop ->
            if Manager /= undefined ->
                    Manager ! {client_has_stopped, self()};
               true ->
                    case (catch Mod:handlerStop(undefined, normal, Data)) of
                        {'EXIT', OOps} ->
                            io:format("plug in error:~p~n",[OOps]);
                        _ ->
                            noop
                    end
            end;
        Other ->
            io:format("**** OOOPYikes ...~p (Client=~p)~n",[Other,Client]),<commit_msg>change request - convert not_implemented to serverBrokeContract response<commit_after>-module(ubf_plugin_handler).

%% The plugin handler handles request for the instance of the plugin

-export([start_handler/0, start_manager/2, manager/2]).

%%----------------------------------------------------------------------
%% Handler stuff

start_handler() ->
    proc_utils:spawn_link_debug(fun() -> wait() end, ?MODULE).

wait() ->
    receive
        {start, Contract, Server, Mod} ->
            loop(Contract, start, [], Server, Mod);
        stop ->
            exit({serverPluginHandler, stop})
    end.

loop(Client, State1, Data, Manager, Mod) ->
    receive
        {_Pid, {rpc, Q}} ->
            if Manager /= undefined ->
                    case (catch Mod:handlerRpc(State1, Q, Data, Manager)) of
                        {Reply, State2, Data2} ->
                            Client ! {self(), {rpcReply, Reply, State2, same}},
                            loop(Client, State2, Data2, Manager, Mod);
                        {changeContract, Reply, State1,
                         HandlerMod, State2, Data2, ManPid} ->
                            Client ! {self(), {rpcReply, Reply, State1,
                                               {new, HandlerMod, State2}}},
                            loop(Client, State2, Data2, ManPid, HandlerMod);
                        {'EXIT', Why} ->
                            exit({serverPluginHandler, Why})
                    end;
               true ->
                    DispatchArgs =
                        if is_tuple(Q) ->
                                erlang:tuple_to_list(Q);
                           is_atom(Q) ->
                                [Q];
                           true ->
                                Why1 = {bad_dispatch_args, Q},
                                exit({serverPluginHandler, Why1})
                        end,
                    case (catch Mod:handlerDispatch(hd(DispatchArgs), Data)) of
                        {DispatchMod, DispatchFun, Data2} ->
                            case (catch erlang:apply(DispatchMod, DispatchFun, tl(DispatchArgs))) of
                                {'EXIT', Why2} ->
                                    exit({serverPluginHandler, Why2});
                                Reply ->
                                    Client ! {self(), {rpcReply, Reply, State1, same}},
                                    loop(Client, State1, Data2, Manager, Mod)
                            end;
                        {'EXIT', Why3} ->
                            exit({serverPluginHandler, Why3});
                        Data2 ->
                            %% special case - hard-coded not_implemented reply
                            Reply = {serverBrokeContract, {Q, not_implemented}, undefined},
                            Client ! {self(), {rpcReply, Reply, State1, same}},
                            loop(Client, State1, Data2, Manager, Mod)
                    end
            end;
        {event, X} ->
            Client ! {event, X},
            loop(Client, State1, Data, Manager, Mod);
        stop ->
            if Manager /= undefined ->
                    Manager ! {client_has_stopped, self()};
               true ->
                    case (catch Mod:handlerStop(undefined, normal, Data)) of
                        {'EXIT', OOps} ->
                            io:format("plug in error:~p~n",[OOps]);
                        _ ->
                            noop
                    end
            end;
        Other ->
            io:format("**** OOOPYikes ...~p (Client=~p)~n",[Other,Client]),
{'fraction_non_alphanumeric': 0.12681159420289856, 'fraction_numerical': 0.008918617614269788, 'mean_word_length': 1.0047486033519553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5419133', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1646, 'n_words': 410}