<commit_before># Part of the jsonrpc2-zeromq-python project.
# (c) 2012 Wireless Innovation Ltd, All Rights Reserved.
# Please see the LICENSE file in the root of this project for license
# information.

import threading

import zmq

from . import common


def response_from_exception(e, id_=None):
    if not hasattr(e, 'to_response'):
        e = common.ServerError(str(e))
    return e.to_response(id_)


class RPCServer(common.Endpoint, threading.Thread):

    default_socket_type = zmq.REP
    allow_methods = True
    allow_notifications = False

    should_stop = False

    def __init__(self, endpoint, context=None, timeout=1000, socket_type=None,
                 logger=None):
        super(RPCServer, self).__init__(endpoint, socket_type, timeout, context,
                                        logger=logger)
        self.socket = self.context.socket(self.socket_type)
        self.socket.bind(self.endpoint)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)

    def stop(self):
        self.should_stop = True

    def run(self):
        self.logger.info("^_^ Server now listening on %s", self.endpoint)
        while not self.should_stop:
            self._handle_one_message()

    def _handle_one_message(self):
        req = client_id = None

        if not self.poller.poll(self.timeout):
            return

        req_parts = self.socket.recv_multipart()
        try:
            if len(req_parts) > 1:
                client_id, req = req_parts[0], req_parts[1:]
                req = ''.join(req)
            else:
                req = req_parts[0]

            try:
                req = common.json_rpc_loads(req)
            except ValueError:
                raise common.ParseError()

            if not isinstance(req, common.Request):
                raise common.InvalidRequest()<commit_msg>don't crash on interrupts: catch the interrupt and continue the handling loop<commit_after># Part of the jsonrpc2-zeromq-python project.
# (c) 2012 Wireless Innovation Ltd, All Rights Reserved.
# Please see the LICENSE file in the root of this project for license
# information.

import threading
import errno

import zmq

from . import common


def response_from_exception(e, id_=None):
    if not hasattr(e, 'to_response'):
        e = common.ServerError(str(e))
    return e.to_response(id_)


class RPCServer(common.Endpoint, threading.Thread):

    default_socket_type = zmq.REP
    allow_methods = True
    allow_notifications = False

    should_stop = False

    def __init__(self, endpoint, context=None, timeout=1000, socket_type=None,
                 logger=None):
        super(RPCServer, self).__init__(endpoint, socket_type, timeout, context,
                                        logger=logger)
        self.socket = self.context.socket(self.socket_type)
        self.socket.bind(self.endpoint)
        self.poller = zmq.Poller()
        self.poller.register(self.socket, zmq.POLLIN)

    def stop(self):
        self.should_stop = True

    def run(self):
        self.logger.info("^_^ Server now listening on %s", self.endpoint)
        while not self.should_stop:
            try:
                self._handle_one_message()
            except zmq.ZMQError as e:
                if e.errno == errno.EINTR:
                    continue
                else:
                    raise

    def _handle_one_message(self):
        req = client_id = None

        if not self.poller.poll(self.timeout):
            return

        req_parts = self.socket.recv_multipart()
        try:
            if len(req_parts) > 1:
                client_id, req = req_parts[0], req_parts[1:]
                req = ''.join(req)
            else:
                req = req_parts[0]

            try:
                req = common.json_rpc_loads(req)
            except ValueError:
                raise common.ParseError()

            if not isinstance(req, common.Request):
                raise common.InvalidRequest()
{'fraction_non_alphanumeric': 0.09002006018054162, 'fraction_numerical': 0.006519558676028084, 'mean_word_length': 2.052027543993879, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6479292', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1113, 'n_words': 285}