示例#1
0
    def _run(self):
        client = self.client
        logger.info(u"Receiver handling connection from {0}".format(client.address))

        last_activity = datetime.now()
        while True:
            if client.server and last_activity + timedelta(minutes=30) < datetime.now():
                client.close('inactivity')
                return

            rlist, wlist, xlist = select([client.socket], [], [], timeout=30.0)

            if len(rlist) == 1:
                # logger.debug(u"Attempting to read an object from {0}".format(self.socket))
                try:
                    obj = BusinessObject.read_from_socket(client.socket)
                    if obj is None:
                        client.close("couldn't read object")
                        return
                    # logger.debug(u"Successfully read object {0}".format(str(obj)))
                    logger.debug(u"<< {0}: {1}".format(client, obj))
                    client.gateway.send(obj, client)
                    last_activity = datetime.now()
                except InvalidObject, ivo:
                    client.close(u"{0}".format(ivo))
                    return
                except socket.error, e:
                    client.close(u"{0}".format(e))
                    return
                except IOError, ioe:
                    client.close(u"{0}".format(e))
                    return
示例#2
0
def reply_for_object(obj, sock, timeout_secs=1.0, select=select):
    """
    Waits for a reply to a sent object (connected by in-reply-to field).

    Returns the object and seconds elapsed as tuple (obj, secs).

    select-module is parameterizable (if not given, Python standard one is used);
    useful for e.g. gevent select.
    """
    started = datetime.now()
    delta = timedelta(seconds=timeout_secs)
    while True:
        rlist, wlist, xlist = select.select([sock], [], [], 0.0001)

        if datetime.now() > started + delta:
            return None, timeout_secs

        if len(rlist) == 0:
            continue

        reply = BusinessObject.read_from_socket(sock)

        if reply is None:
            raise InvalidObject
        elif reply.metadata.get('in-reply-to', None) == obj.id:
            took = datetime.now() - started
            if hasattr(took, 'total_seconds'):
                return reply, took.total_seconds()
            else:
                return reply, _total_seconds(took)
示例#3
0
def reply_for_object(obj, sock, timeout_secs=1.0, select=select):
    """
    Waits for a reply to a sent object (connected by in-reply-to field).

    Returns the object and seconds elapsed as tuple (obj, secs).

    select-module is parameterizable (if not given, Python standard one is used);
    useful for e.g. gevent select.
    """
    started = datetime.now()
    delta = timedelta(seconds=timeout_secs)
    while True:
        rlist, wlist, xlist = select.select([sock], [], [], 0.0001)

        if datetime.now() > started + delta:
            return None, timeout_secs

        if len(rlist) == 0:
            continue

        reply = BusinessObject.read_from_socket(sock)

        if reply is None:
            raise InvalidObject
        elif reply.metadata.get('in-reply-to', None) == obj.id:
            took = datetime.now() - started
            if hasattr(took, 'total_seconds'):
                return reply, took.total_seconds()
            else:
                return reply, _total_seconds(took)
示例#4
0
    def _run(self):
        client = self.client
        logger.info(u"Receiver handling connection from {0}".format(
            client.address))

        last_activity = datetime.now()
        while True:
            if client.server and last_activity + timedelta(
                    minutes=30) < datetime.now():
                client.close('inactivity')
                return

            rlist, wlist, xlist = select([client.socket], [], [], timeout=30.0)

            if len(rlist) == 1:
                # logger.debug(u"Attempting to read an object from {0}".format(self.socket))
                try:
                    obj = BusinessObject.read_from_socket(client.socket)
                    if obj is None:
                        client.close("couldn't read object")
                        return
                    # logger.debug(u"Successfully read object {0}".format(str(obj)))
                    logger.debug(u"<< {0}: {1}".format(client, obj))
                    client.gateway.send(obj, client)
                    last_activity = datetime.now()
                except InvalidObject, ivo:
                    client.close(u"{0}".format(ivo))
                    return
                except socket.error, e:
                    client.close(u"{0}".format(e))
                    return
                except IOError, ioe:
                    client.close(u"{0}".format(e))
                    return
示例#5
0
def read_object_with_timeout(sock, timeout_secs=1.0, select=select):
    rlist, wlist, xlist = select.select([sock], [], [], timeout_secs)

    if len(rlist) > 0:
        return BusinessObject.read_from_socket(sock)
示例#6
0
def read_object_with_timeout(sock, timeout_secs=1.0, select=select):
    rlist, wlist, xlist = select.select([sock], [], [], timeout_secs)

    if len(rlist) > 0:
        return BusinessObject.read_from_socket(sock)