<commit_before>%% @doc Ssl Socket listening server.  Clients are expected to first contact
%% this listeer before attempting to reach the others.  Uses an accept 
%% pool for extra awesome.
-module(pre_ssl_listener).
-behavior(gen_server).

-include("log.hrl").

-export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,
	code_change/3]).
-export([start/0,start/1,start_link/0,start_link/1]).
% exported only for internal use
-export([spawn_acceptor/1]).

-record(state, {
	listener,
	acceptors,
	poolsize = 5
}).

%% =====
%% Types
%% =====

-type(port_opt() :: {port, pos_integer()}).
-type(certfile_opt() :: {certfile, string()}).
-type(keyfile_opt() :: {keyfile, string()}).
-type(poolsize_opt() :: {poolsize, pos_integer()}).
-type(start_opt() :: port_opt() | certfile_opt() | keyfile_opt() |
		poolsize_opt()).
-type(start_opts() :: [start_opt()]).

%% =====
%% API
%% =====

%% @doc Starts an unlinked ssl listener with default options.
-spec(start/0 :: () -> {'ok', pid()}).
start() -> start([]).

%% @doc Starts an unlinked ssl listener with the given options.
-spec(start/1 :: (Args :: start_opts()) -> {'ok', pid()}).
start(Args) -> gen_server:start(?MODULE, Args, []).

%% @doc Starts an ssl listener with default options.
-spec(start_link/0 :: () -> {'ok', pid()}).
start_link() -> start_link([]).

%% @doc Starts an ssl listener with the given options.
-spec(start_link/1 :: (Args :: start_opts()) -> {'ok', pid()}).
start_link(Args) -> gen_server:start_link(?MODULE, Args, []).

%% =====
%% init
%% =====

init(Args) ->
	process_flag(trap_exit, true),
	Port = proplists:get_value(port, Args, 6006),
	PrivDir = pre_server_app:priv_dir(),
	DefaultCertfile = filename:join(PrivDir, "precursors.crt"),
	DefaultKeyfile = filename:join(PrivDir, "key.pub"),
	Certfile = proplists:get_value(certfile, Args, DefaultCertfile),
	Keyfile = proplists:get_value(keyfile, Args, DefaultKeyfile),
	SimpleOpts = [list, {packet, line}, {reuseaddr, true},
		{certfile, Certfile}, {keyfile, Keyfile}, {keepalive, true},
		{backlog, 30}, {active, false}],
	Poolsize = proplists:get_value(poolsize, Args, 5),
	case ssl:listen(Port, SimpleOpts) of
		{ok, Listen_socket} ->
			Acceptors = spawn_acceptors(Listen_socket, Poolsize),
			%%Create first accepting process
			%Acceptor = spawn_link(?MODULE, spawn_acceptor, [Listen_socket]),
			?info("Started on port ~p", [Port]),
			{ok, #state{poolsize = Poolsize, listener = Listen_socket, acceptors= Acceptors}};
		{error, Reason} ->
			?warning("Could not start ssl:  ~p", [Reason]),
			{stop, Reason}
	end.

%% =====
%% handle_call
%% =====

handle_call(Req, _From, State) ->
	?debug("Unhandled call:  ~p", [Req]),
	{reply, unknown, State}.<commit_msg>Corrected which keyfile it uses.<commit_after>%% @doc Ssl Socket listening server.  Clients are expected to first contact
%% this listeer before attempting to reach the others.  Uses an accept 
%% pool for extra awesome.
-module(pre_ssl_listener).
-behavior(gen_server).

-include("log.hrl").

-export([init/1,handle_call/3,handle_cast/2,handle_info/2,terminate/2,
	code_change/3]).
-export([start/0,start/1,start_link/0,start_link/1]).
% exported only for internal use
-export([spawn_acceptor/1]).

-record(state, {
	listener,
	acceptors,
	poolsize = 5
}).

%% =====
%% Types
%% =====

-type(port_opt() :: {port, pos_integer()}).
-type(certfile_opt() :: {certfile, string()}).
-type(keyfile_opt() :: {keyfile, string()}).
-type(poolsize_opt() :: {poolsize, pos_integer()}).
-type(start_opt() :: port_opt() | certfile_opt() | keyfile_opt() |
		poolsize_opt()).
-type(start_opts() :: [start_opt()]).

%% =====
%% API
%% =====

%% @doc Starts an unlinked ssl listener with default options.
-spec(start/0 :: () -> {'ok', pid()}).
start() -> start([]).

%% @doc Starts an unlinked ssl listener with the given options.
-spec(start/1 :: (Args :: start_opts()) -> {'ok', pid()}).
start(Args) -> gen_server:start(?MODULE, Args, []).

%% @doc Starts an ssl listener with default options.
-spec(start_link/0 :: () -> {'ok', pid()}).
start_link() -> start_link([]).

%% @doc Starts an ssl listener with the given options.
-spec(start_link/1 :: (Args :: start_opts()) -> {'ok', pid()}).
start_link(Args) -> gen_server:start_link(?MODULE, Args, []).

%% =====
%% init
%% =====

init(Args) ->
	process_flag(trap_exit, true),
	Port = proplists:get_value(port, Args, 6006),
	PrivDir = pre_server_app:priv_dir(),
	DefaultCertfile = filename:join(PrivDir, "precursors.crt"),
	DefaultKeyfile = filename:join(PrivDir, "key"),
	Certfile = proplists:get_value(certfile, Args, DefaultCertfile),
	Keyfile = proplists:get_value(keyfile, Args, DefaultKeyfile),
	SimpleOpts = [list, {packet, line}, {reuseaddr, true},
		{certfile, Certfile}, {keyfile, Keyfile}, {keepalive, true},
		{backlog, 30}, {active, false}],
	Poolsize = proplists:get_value(poolsize, Args, 5),
	case ssl:listen(Port, SimpleOpts) of
		{ok, Listen_socket} ->
			Acceptors = spawn_acceptors(Listen_socket, Poolsize),
			%%Create first accepting process
			%Acceptor = spawn_link(?MODULE, spawn_acceptor, [Listen_socket]),
			?info("Started on port ~p", [Port]),
			{ok, #state{poolsize = Poolsize, listener = Listen_socket, acceptors= Acceptors}};
		{error, Reason} ->
			?warning("Could not start ssl:  ~p", [Reason]),
			{stop, Reason}
	end.

%% =====
%% handle_call
%% =====

handle_call(Req, _From, State) ->
	?debug("Unhandled call:  ~p", [Req]),
	{reply, unknown, State}.
{'fraction_non_alphanumeric': 0.20047908605122536, 'fraction_numerical': 0.00847613782937166, 'mean_word_length': 5.161180476730988, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '1747020', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1946, 'n_words': 447}