<commit_before>defmodule SMPPEX.MC do

  alias :ranch, as: Ranch

  @default_transport :ranch_tcp
  @default_acceptor_count 50

  @spec start({module, args :: term}, opts :: Keyword.t) :: {:ok, listener_ref :: Ranch.ref} | {:error, reason :: term}

  def start({_module, _args} = mod_with_args, opts \\ []) do
    acceptor_count = Keyword.get(opts, :acceptor_count, @default_acceptor_count)
    transport = Keyword.get(opts, :transport, @default_transport)
    transport_opts = Keyword.get(opts, :transport_opts, [{:port, 0}])
    mc_opts = Keyword.get(opts, :mc_opts)
    ref = make_ref()

    start_result = Ranch.start_listener(
      ref,
      acceptor_count,
      transport,
      transport_opts,
      SMPPEX.TransportSession,
      {SMPPEX.Session, [mod_with_args, mc_opts]}
    )

    case start_result do
      {:error, _} = error -> error
      {:ok, _, _} -> {:ok, ref}
      {:ok, _} -> {:ok, ref}
    end

  end

  @spec stop(Ranch.ref) :: :ok

  @doc """
  Stops MC listener and all its sessions.
  """

  def stop(listener) do
    Ranch.stop_listener(listener)
  end

end
<commit_msg>Added general MC docs<commit_after>defmodule SMPPEX.MC do
  @moduledoc """
  This is a module for launching a TCP listener (or any other listener supported
  by `ranch`, for example, `ssl`) which handles incoming connections with the
  passed `SMPPEX.Session` implementations.

  To start an MC one generally should do the following.

  1. Implement an `SMPPEX.Session` behaviour.

  ```elixir

  defmodule MyMCSession do
    use SMPPEX.Session

    # ...Callback implementation

  end

  ```

  2. Start a listener passing implemented behaviour as a callback module.

  ```elixir

  {:ok, listener} = SMPPEX.MC.start({MyESMESession, some_args},
                                    transport_opts: [port: 2775])
  ```

  The important things to note are:
  * There is no `start_link` method, since started listener is not a standalone
  `GenServer` but a pool of socket acceptors running under `Ranch` supervisor.
  * Each received connection is served with its own process which uses passed
  callback module (`MyESMESession`) for handling connection events.
  """

  alias :ranch, as: Ranch

  @default_transport :ranch_tcp
  @default_acceptor_count 50

  @spec start({module, args :: term}, opts :: Keyword.t) ::
    {:ok, listener_ref :: Ranch.ref} |
    {:error, reason :: term}

  def start({_module, _args} = mod_with_args, opts \\ []) do
    acceptor_count = Keyword.get(opts, :acceptor_count, @default_acceptor_count)
    transport = Keyword.get(opts, :transport, @default_transport)
    transport_opts = Keyword.get(opts, :transport_opts, [{:port, 0}])
    mc_opts = Keyword.get(opts, :mc_opts)
    ref = make_ref()

    start_result = Ranch.start_listener(
      ref,
      acceptor_count,
      transport,
      transport_opts,
      SMPPEX.TransportSession,
      {SMPPEX.Session, [mod_with_args, mc_opts]}
    )

    case start_result do
      {:error, _} = error -> error
      {:ok, _, _} -> {:ok, ref}
      {:ok, _} -> {:ok, ref}
    end

  end

  @spec stop(Ranch.ref) :: :ok

  @doc """
  Stops MC listener and all its sessions.
  """

  def stop(listener) do
    Ranch.stop_listener(listener)
  end

end

{'fraction_non_alphanumeric': 0.11810291382517049, 'fraction_numerical': 0.0037197768133911966, 'mean_word_length': 3.1638709677419357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4845444', 'n_tokens_mistral': 1118, 'n_tokens_neox': 1056, 'n_words': 299}