<commit_before>defmodule IElixir.Socket.IOPub do
  @moduledoc """
  "IOPub: this socket is the ‘broadcast channel’ where the kernel publishes all
  side effects (stdout, stderr, etc.) as well as the requests coming from any
  client over the shell socket and its own requests on the stdin socket."
  From https://ipython.org/ipython-doc/dev/development/messaging.html
  """

  use GenServer
  require Logger
  alias IElixir.Message

  @doc false
  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: IOPub)
  end

  def init(opts) do
    Process.flag(:trap_exit, true)
    sock = IElixir.Utils.make_socket(opts, "iopub", :pub)
    {:ok, sock}
  end

  def send_status(status, message) do
    GenServer.cast(IOPub, {:send_status, status, message})
  end

  def send_execute_input(message, execution_count) do
    GenServer.cast(IOPub, {:send_execute_input, message, execution_count})
  end

  def send_stream(message, text) do
    GenServer.cast(IOPub, {:send_stream, message, text})
  end

  def send_execute_result(message, text) do
    GenServer.cast(IOPub, {:send_execute_result, message, text})
  end

  def send_error(message, execution_count, exception_name, traceback) do
    GenServer.cast(IOPub, {:send_error, message, execution_count, exception_name, traceback})
  end

  def terminate(_reason, sock) do
    Logger.debug("Shutdown IOPub")
    :erlzmq.close(sock)
  end

  def handle_cast({:send_execute_input, message, execution_count}, sock) do
    content = %{
      "execution_count": execution_count,
      "code": message.content["code"]
    }
    Message.send_message(sock, message, "execute_input", content)
    {:noreply, sock}
  end
  def handle_cast({:send_stream, message, text}, sock) do<commit_msg>Add short documentation to IOPub socket<commit_after>defmodule IElixir.Socket.IOPub do
  @moduledoc """
  "IOPub: this socket is the ‘broadcast channel’ where the kernel publishes all
  side effects (stdout, stderr, etc.) as well as the requests coming from any
  client over the shell socket and its own requests on the stdin socket."
  From https://ipython.org/ipython-doc/dev/development/messaging.html
  """

  use GenServer
  require Logger
  alias IElixir.Message

  @doc false
  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts, name: IOPub)
  end

  def init(opts) do
    Process.flag(:trap_exit, true)
    sock = IElixir.Utils.make_socket(opts, "iopub", :pub)
    {:ok, sock}
  end

  @doc """
  Send status of sandbox: 'ok' | 'error' | 'abort'.
  """
  def send_status(status, message) do
    GenServer.cast(IOPub, {:send_status, status, message})
  end

  @doc """
  Send execute_input message. Send information about 'execution_count'.
  """
  def send_execute_input(message, execution_count) do
    GenServer.cast(IOPub, {:send_execute_input, message, execution_count})
  end

  @doc """
  Send execute_result message. This is used for sending what executed code
  returned.
  """
  def send_execute_result(message, text) do
    GenServer.cast(IOPub, {:send_execute_result, message, text})
  end

  @doc """
  Send stream message. This is used for sending output of code execution.
  """
  def send_stream(message, text) do
    GenServer.cast(IOPub, {:send_stream, message, text})
  end

  @doc """
  Send error message. Send traceback so client can have information about what
  went wrong.
  """
  def send_error(message, execution_count, exception_name, traceback) do
    GenServer.cast(IOPub, {:send_error, message, execution_count, exception_name, traceback})
  end

  def terminate(_reason, sock) do
    Logger.debug("Shutdown IOPub")
    :erlzmq.close(sock)
  end

  def handle_cast({:send_execute_input, message, execution_count}, sock) do
    content = %{
      "execution_count": execution_count,
      "code": message.content["code"]
    }
    Message.send_message(sock, message, "execute_input", content)
    {:noreply, sock}
  end
  def handle_cast({:send_stream, message, text}, sock) do
{'fraction_non_alphanumeric': 0.10646866347847973, 'fraction_numerical': 0.0, 'mean_word_length': 4.174479166666667, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '1851203', 'n_tokens_mistral': 1329, 'n_tokens_neox': 1274, 'n_words': 367}