<commit_before>%%%-------------------------------------------------------------------
%% @copyright <NAME>
%% @author <NAME> <<EMAIL>>
%% @version {@vsn}, {@date} {@time}
%% @doc Enet Interface process
%% @end
%%%-------------------------------------------------------------------
-module(enet_iface).

-behaviour(gen_server).

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

%% API
-export([start_link/2
         ,start/2
         ,event_manager/1
         ,send/2
        ]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {dev, port, ev_pid, osifconfig}).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link(Device, OSIfconfig) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end
%%--------------------------------------------------------------------
start_link(Device, IfConfig) when is_list(Device), is_list(IfConfig) ->
    gen_server:start_link(?MODULE, [#state{dev=Device}, IfConfig], []).

start(Device, IfConfig) when is_list(Device), is_list(IfConfig) ->
    gen_server:start(?MODULE, [#state{dev=Device}, IfConfig], []).

event_manager(Interface) ->
    gen_server:call(Interface, get_event_manager).

send(Interface, Data) ->
    gen_server:cast(Interface, {send, Data}).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @private
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore               |
%%                     {stop, Reason}
%% @doc Initialises the server's state
%% @end
%%--------------------------------------------------------------------
init([S = #state{dev=Device}, IfConfig]) ->
    Port = enet_tap:open(Device),
    {ok, EvPid} = gen_event:start_link(),
    {ok, S#state{port=Port, ev_pid=EvPid, osifconfig=IfConfig}}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Call message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_call(get_event_manager, _From, S = #state{ev_pid=Pid}) ->
    {reply, Pid, S};
handle_call(Call, _From, State) ->
    ?WARN("Unexpected call ~p.", [Call]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_cast(Msg, State) -> {noreply, State} |
%%                            {noreply, State, Timeout} |
%%                            {stop, Reason, State}
%% @doc Cast message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_cast({send, Data}, S = #state{port=P}) when is_port(P) ->
    case Data of
        Packet when is_tuple(Packet) ->
            Frame = enet_codec:encode(ethernet, Packet),
            gen_event:notify(S#state.ev_pid, {out, Frame, Packet}),
            port_command(P, Frame);
        Frame when is_binary(Frame) ->
            gen_event:notify(S#state.ev_pid, {out, Frame, raw}),
            port_command(P, Frame)
    end,
    {noreply, S};
handle_cast(Msg, State) ->
    ?WARN("Unexpected cast ~p", [Msg]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_info(Info, State) -> {noreply, State} |
%%                             {noreply, State, Timeout} |
%%                             {stop, Reason, State}
%% @doc Non gen-server message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_info({Port, {exit_status, N}}, S = #state{port=Port}) ->
    {stop, {interface_shutdown, N}, S = #state{port=undefined}};
handle_info({Port, {data, PortPacket}}, S = #state{port=Port}) -><commit_msg>Check for back packets before sending from enet_iface, log.<commit_after>%%%-------------------------------------------------------------------
%% @copyright <NAME>
%% @author <NAME> <<EMAIL>>
%% @version {@vsn}, {@date} {@time}
%% @doc Enet Interface process
%% @end
%%%-------------------------------------------------------------------
-module(enet_iface).

-behaviour(gen_server).

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

%% API
-export([start_link/2
         ,start/2
         ,event_manager/1
         ,send/2
        ]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

-record(state, {dev, port, ev_pid, osifconfig}).

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% @spec start_link(Device, OSIfconfig) -> {ok,Pid} | ignore | {error,Error}
%% @doc Starts the server
%% @end
%%--------------------------------------------------------------------
start_link(Device, IfConfig) when is_list(Device), is_list(IfConfig) ->
    gen_server:start_link(?MODULE, [#state{dev=Device}, IfConfig], []).

start(Device, IfConfig) when is_list(Device), is_list(IfConfig) ->
    gen_server:start(?MODULE, [#state{dev=Device}, IfConfig], []).

event_manager(Interface) ->
    gen_server:call(Interface, get_event_manager).

send(Interface, Data) ->
    gen_server:cast(Interface, {send, Data}).

%%====================================================================
%% gen_server callbacks
%%====================================================================

%%--------------------------------------------------------------------
%% @private
%% @spec init(Args) -> {ok, State} |
%%                     {ok, State, Timeout} |
%%                     ignore               |
%%                     {stop, Reason}
%% @doc Initialises the server's state
%% @end
%%--------------------------------------------------------------------
init([S = #state{dev=Device}, IfConfig]) ->
    Port = enet_tap:open(Device),
    {ok, EvPid} = gen_event:start_link(),
    {ok, S#state{port=Port, ev_pid=EvPid, osifconfig=IfConfig}}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_call(Request, From, State) -> {reply, Reply, State} |
%%                                      {reply, Reply, State, Timeout} |
%%                                      {noreply, State} |
%%                                      {noreply, State, Timeout} |
%%                                      {stop, Reason, Reply, State} |
%%                                      {stop, Reason, State}
%% @doc Call message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_call(get_event_manager, _From, S = #state{ev_pid=Pid}) ->
    {reply, Pid, S};
handle_call(Call, _From, State) ->
    ?WARN("Unexpected call ~p.", [Call]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_cast(Msg, State) -> {noreply, State} |
%%                            {noreply, State, Timeout} |
%%                            {stop, Reason, State}
%% @doc Cast message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_cast({send, Data}, S = #state{port=P}) when is_port(P) ->
    case Data of
        Packet = #eth{} ->
            Frame = enet_codec:encode(ethernet, Packet),
            gen_event:notify(S#state.ev_pid, {out, Frame, Packet}),
            port_command(P, Frame);
        Frame when is_binary(Frame) ->
            gen_event:notify(S#state.ev_pid, {out, Frame, raw}),
            port_command(P, Frame);
        BadPacket ->
            ?WARN("Couldn't send bad packet: ~p", [BadPacket])
    end,
    {noreply, S};
handle_cast(Msg, State) ->
    ?WARN("Unexpected cast ~p", [Msg]),
    {noreply, State}.

%%--------------------------------------------------------------------
%% @private
%% @spec 
%% handle_info(Info, State) -> {noreply, State} |
%%                             {noreply, State, Timeout} |
%%                             {stop, Reason, State}
%% @doc Non gen-server message handler callbacks
%% @end
%%--------------------------------------------------------------------
handle_info({Port, {exit_status, N}}, S = #state{port=Port}) ->
    {stop, {interface_shutdown, N}, S = #state{port=undefined}};
handle_info({Port, {data, PortPacket}}, S = #state{port=Port}) ->
{'fraction_non_alphanumeric': 0.3711742999782939, 'fraction_numerical': 0.0021706099413935317, 'mean_word_length': 1.5476914570085707, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 48, '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': '3580079', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2334, 'n_words': 502}