<commit_before>% Copyright (c) 2009, NorthScale, Inc.
% All rights reserved.

-module(mc_server_ascii).

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

-include("mc_constants.hrl").

-include("mc_entry.hrl").

-compile(export_all).

% Note: the connection just closes on any error.

loop_in(InSock, OutPid, CmdNum, Module, Session) ->
    case recv(InSock) of
        {ok, Cmd, CmdArgs} ->
            {ok, Session2} =
                Module:cmd(Cmd, Session, InSock, {OutPid, CmdNum}, CmdArgs),
            loop_in(InSock, OutPid, CmdNum + 1, Module, Session2);
        {error, closed} ->
            OutPid ! stop,
            ok
    end.

loop_in_prefix(Prefix, InSock, OutPid, CmdNum, Module, Session) ->
    case recv_prefix(Prefix, InSock) of
        {ok, Cmd, CmdArgs} ->
            {ok, Session2} =
                Module:cmd(Cmd, Session, InSock, {OutPid, CmdNum}, CmdArgs),
            loop_in(InSock, OutPid, CmdNum + 1, Module, Session2);
        {error, closed} ->
            OutPid ! stop,
            ok
    end.

loop_out(OutSock) ->
    receive
        {send, _CmdNum, Data} ->
            ok = mc_ascii:send(OutSock, Data),
            loop_out(OutSock);
        {flush, From} -> From ! flushed;
        stop -> ok
    end.

recv(InSock) ->
    case mc_ascii:recv_line(InSock) of
        {ok, <<>>} ->
            {ok, unknown, []};
        {ok, Line} ->
            [CmdName | CmdArgs] = string:tokens(binary_to_list(Line), " "),
            {ok, list_to_existing_atom(CmdName), CmdArgs};
        Err -> Err
    end.

recv_prefix(Prefix, InSock) ->
    case mc_ascii:recv_line(InSock) of
        {ok, LineBody} ->
            Line = <<Prefix/binary, LineBody/binary>>,
            [CmdName | CmdArgs] = string:tokens(binary_to_list(Line), " "),
            {ok, list_to_existing_atom(CmdName), CmdArgs};
        Err -> Err
    end.
<commit_msg>checks around list_to_existing_atom to allow ERROR response<commit_after>% Copyright (c) 2009, NorthScale, Inc.
% All rights reserved.

-module(mc_server_ascii).

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

-include("mc_constants.hrl").

-include("mc_entry.hrl").

-compile(export_all).

% Note: the connection just closes on any error.

loop_in(InSock, OutPid, CmdNum, Module, Session) ->
    case recv(InSock) of
        {ok, Cmd, CmdArgs} ->
            {ok, Session2} =
                Module:cmd(Cmd, Session, InSock, {OutPid, CmdNum}, CmdArgs),
            loop_in(InSock, OutPid, CmdNum + 1, Module, Session2);
        {error, closed} ->
            OutPid ! stop,
            ok
    end.

loop_in_prefix(Prefix, InSock, OutPid, CmdNum, Module, Session) ->
    case recv_prefix(Prefix, InSock) of
        {ok, Cmd, CmdArgs} ->
            {ok, Session2} =
                Module:cmd(Cmd, Session, InSock, {OutPid, CmdNum}, CmdArgs),
            loop_in(InSock, OutPid, CmdNum + 1, Module, Session2);
        {error, closed} ->
            OutPid ! stop,
            ok
    end.

loop_out(OutSock) ->
    receive
        {send, _CmdNum, Data} ->
            ok = mc_ascii:send(OutSock, Data),
            loop_out(OutSock);
        {flush, From} -> From ! flushed;
        stop -> ok
    end.

recv(InSock) ->
    case mc_ascii:recv_line(InSock) of
        {ok, <<>>} ->
            {ok, unknown, []};
        {ok, Line} ->
            [CmdName | CmdArgs] = string:tokens(binary_to_list(Line), " "),
            case catch(list_to_existing_atom(CmdName)) of
                {'EXIT', {badarg, _}} -> {ok, CmdName, CmdArgs};
                CmdAtom               -> {ok, CmdAtom, CmdArgs}
            end;
        Err -> Err
    end.

recv_prefix(Prefix, InSock) ->
    case mc_ascii:recv_line(InSock) of
        {ok, LineBody} ->
            Line = <<Prefix/binary, LineBody/binary>>,
            [CmdName | CmdArgs] = string:tokens(binary_to_list(Line), " "),
            case catch(list_to_existing_atom(CmdName)) of
                {'EXIT', {badarg, _}} -> {ok, CmdName, CmdArgs};
                CmdAtom               -> {ok, CmdAtom, CmdArgs}
            end;
        Err -> Err
    end.

{'fraction_non_alphanumeric': 0.14053254437869822, 'fraction_numerical': 0.004930966469428008, 'mean_word_length': 2.04351087771943, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 49, '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': '6165798', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1396, 'n_words': 304}