<commit_before>require_relative "./jeromq-0.3.3.jar"
java_import "org.zeromq.ZContext"
java_import "org.zeromq.ZFrame"
java_import "org.zeromq.ZLoop"
java_import "org.zeromq.ZMQ"
java_import "org.zeromq.ZMQException"
java_import "org.zeromq.ZMQQueue"
java_import "org.zeromq.ZMsg"
java_import "org.zeromq.ZThread"

java_import "java.nio.channels.AsynchronousCloseException"
java_import "java.nio.channels.ClosedChannelException"
java_import "java.nio.channels.ClosedSelectorException"

class ZContext
  def create_socket_with_opts(type, opts = {})
    socket = create_socket(type)
    opts.each do |key, value|
      next if key == :bind || key == :connect
      socket.__send__("#{key}=", value)
    end
    socket.connect(opts[:connect]) if opts[:connect]
    socket.bind(opts[:bind]) if opts[:bind]
    socket
  end

  def router(opts = {})
    create_socket_with_opts(ZMQ::ROUTER, opts)
  end

  def dealer(opts = {})
    create_socket_with_opts(ZMQ::DEALER, opts)
  end

  def push(opts = {})
    create_socket_with_opts(ZMQ::PUSH, opts)
  end

  def pull(opts = {})
    create_socket_with_opts(ZMQ::PULL, opts)
  end

  def pub(opts = {})
    create_socket_with_opts(ZMQ::PUB, opts)
  end

  def sub(opts = {})
    create_socket_with_opts(ZMQ::SUB, opts)
  end

  ## global context instance

  def self.instance
    @context ||= new
  end

  def self.create_socket(type)
    instance.create_socket(type)
  end

  def self.destroy_socket(socket)
    instance.destroy_socket(socket)
  end

  def self.destroy
    instance.destroy
  rescue Java::JavaLang::IllegalStateException
    # ignore broken shutdown in zeromq
  end

  def self.router(opts = {})
    instance.router(opts)
  end

  def self.dealer(opts = {})
    instance.dealer(opts)
  end

  def self.push(opts = {})
    instance.push(opts)
  end

  def self.pull(opts = {})
    instance.pull(opts)
  end

  def self.pub(opts = {})
    instance.pub(opts)
  end

  def self.sub(opts = {})
    instance.sub(opts)
  end
end<commit_msg>add exception helper for ZMQ context shutdown<commit_after>require_relative "./jeromq-0.3.3.jar"
java_import "org.zeromq.ZContext"
java_import "org.zeromq.ZFrame"
java_import "org.zeromq.ZLoop"
java_import "org.zeromq.ZMQ"
java_import "org.zeromq.ZMQException"
java_import "org.zeromq.ZMQQueue"
java_import "org.zeromq.ZMsg"
java_import "org.zeromq.ZThread"

class ZContext

  def create_socket_with_opts(type, opts = {})
    socket = create_socket(type)
    opts.each do |key, value|
      next if key == :bind || key == :connect
      socket.__send__("#{key}=", value)
    end
    socket.connect(opts[:connect]) if opts[:connect]
    socket.bind(opts[:bind]) if opts[:bind]
    socket
  end

  def router(opts = {})
    create_socket_with_opts(ZMQ::ROUTER, opts)
  end

  def dealer(opts = {})
    create_socket_with_opts(ZMQ::DEALER, opts)
  end

  def push(opts = {})
    create_socket_with_opts(ZMQ::PUSH, opts)
  end

  def pull(opts = {})
    create_socket_with_opts(ZMQ::PULL, opts)
  end

  def pub(opts = {})
    create_socket_with_opts(ZMQ::PUB, opts)
  end

  def sub(opts = {})
    create_socket_with_opts(ZMQ::SUB, opts)
  end

  ## global context instance

  def self.instance
    @context ||= new
  end

  def self.create_socket(type)
    instance.create_socket(type)
  end

  def self.destroy_socket(socket)
    instance.destroy_socket(socket)
  end

  # really incredible how many exceptions a simple shutdown can throw all over
  # the place. if it's one thing ZMQ did never get right it is the shutdown
  # logic ...
  def self.destroy
    instance.destroy
  rescue Java::JavaLang::IllegalStateException
    # ignore broken shutdown in zeromq
  end

  DestroyExceptions = [
    Java::JavaNioChannels::AsynchronousCloseException,
    Java::JavaNioChannels::ClosedChannelException,
    Java::JavaNioChannels::ClosedSelectorException,
  ]

  Exceptions = DestroyExceptions + [
    Java::OrgZeromq::ZMQException,
    Java::Zmq::ZError::IOException,
  ]

  def self.destroy_exception?(e)
    return true if e.is_a?(Java::OrgZeromq::ZMQException) && ZMQ::Error::ETERM.getCode == e.getErrorCode
    return true if e.is_a?(Java::Zmq::ZError::IOException) && DestroyExceptions.include?(e.cause.class)
    return true if DestroyExceptions.include?(e.class)
    return false
  end

  def self.router(opts = {})
    instance.router(opts)
  end

  def self.dealer(opts = {})
    instance.dealer(opts)
  end

  def self.push(opts = {})
    instance.push(opts)
  end

  def self.pull(opts = {})
    instance.pull(opts)
  end

  def self.pub(opts = {})
    instance.pub(opts)
  end

  def self.sub(opts = {})
    instance.sub(opts)
  end
end
{'fraction_non_alphanumeric': 0.12203536006899526, 'fraction_numerical': 0.00129366106080207, 'mean_word_length': 3.8172377985462096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1308287', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1649, 'n_words': 323}