Example #1
0
    def __init__(self, lock, pupy_srv, *args, **kwargs):
        self._sync_events = {}
        self._connection_serve_lock = lock
        self._last_recv = time.time()
        self._ping = False
        self._ping_timeout = 2
        self._serve_timeout = 10

        if 'ping' in kwargs:
            ping = kwargs.get('ping')
            del kwargs['ping']
        else:
            ping = None

        if 'timeout' in kwargs:
            timeout = kwargs.get('timeout')
            del kwargs['timeout']
        else:
            timeout = None

        if ping or timeout:
            self.set_pings(ping, timeout)

        kwargs['_lazy'] = True
        Connection.__init__(self, *args, **kwargs)
        if pupy_srv:
            self._local_root.pupy_srv = pupy_srv
Example #2
0
 def _dispatch_reply(self, seq, raw):
     self._last_recv = time.time()
     sync = seq not in self._async_callbacks
     Connection._dispatch_reply(self, seq, raw)
     if sync:
         if seq in self._sync_events:
             self._sync_events[seq].set()
Example #3
0
    def _dispatch_exception(self, seq, raw):
        if __debug__:
            logger.debug('Dispatch exception(%s): %s', self, seq)

        self._last_recv = time.time()

        is_sync = False
        with self._async_events_lock:
            is_sync = seq not in self._async_callbacks

        if is_sync:
            self._sync_raw_exceptions[seq] = raw
            if __debug__:
                logger.debug(
                    'Dispatch sync exception(%s): %s - pass',
                        self, seq)
            self._sync_events[seq].set()
        else:
            if __debug__:
                logger.debug(
                    'Dispatch async reply(%s): %s - start', self, seq)
            Connection._dispatch_exception(self, seq, raw)
            if __debug__:
                logger.debug(
                    'Dispatch async reply(%s): %s - complete', self, seq)
Example #4
0
 def _dispatch_reply(self, seq, raw):
     self._last_recv = time.time()
     sync = seq not in self._async_callbacks
     Connection._dispatch_reply(self, seq, raw)
     if sync:
         if seq in self._sync_events:
             self._sync_events[seq].set()
    def __init__(self, lock, pupy_srv, *args, **kwargs):
        self._sync_events = {}
        self._connection_serve_lock = lock
        self._last_recv = time.time()
        self._ping = False
        self._ping_timeout = 2
        self._serve_timeout = 10

        if 'ping' in kwargs:
            ping = kwargs.get('ping')
            del kwargs['ping']
        else:
            ping = None

        if 'timeout' in kwargs:
            timeout = kwargs.get('timeout')
            del kwargs['timeout']
        else:
            timeout = None

        if ping or timeout:
            self.set_pings(ping, timeout)

        kwargs['_lazy'] = True
        Connection.__init__(self, *args, **kwargs)
        if pupy_srv:
            self._local_root.pupy_srv = pupy_srv
Example #6
0
    def _dispatch_reply(self, seq, raw):
        if __debug__:
            logger.debug('Dispatch reply: {} - start'.format(seq))

        self._last_recv = time.time()

        is_sync = False
        with self._async_events_lock:
            is_sync = seq not in self._async_callbacks

        if is_sync:
            self._sync_raw_replies[seq] = raw
            if __debug__:
                logger.debug('Dispatch sync reply: {} - pass'.format(seq))
            self._sync_events[seq].set()

        else:
            # We hope here that this request will not block x_x
            if __debug__:
                logger.debug('Dispatch async reply: {} - start'.format(seq))

            Connection._dispatch_reply(self, seq, raw)

            if __debug__:
                logger.debug('Dispatch async reply: {} - complete'.format(seq))
Example #7
0
 def __init__(self, lock, pupy_srv, *args, **kwargs):
     self._sync_events = {}
     self._connection_serve_lock = lock
     self._last_recv = time.time()
     kwargs['_lazy'] = True
     Connection.__init__(self, *args, **kwargs)
     if pupy_srv:
         self._local_root.pupy_srv = pupy_srv
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     try:
         config = dict(self.protocol_config, credentials=credentials,
                       endpoints=(sock.getsockname(), addrinfo), logger=self.logger)
         conn = Connection(self.service, Channel(SocketStream(sock)),
                           config=config)
         conn.serve_all()
     finally:
         pass
Example #9
0
    def sync_request(self, handler, *args):
        seq = self._send_request(handler, args)
        if __debug__:
            synclogger.debug('Sync request wait: {}'.format(seq))

        self._sync_events[seq].wait()

        if __debug__:
            synclogger.debug('Sync request wait: {} - complete'.format(seq))

        del self._sync_events[seq]

        if __debug__:
            synclogger.debug('Sync request process: {}'.format(seq))

        is_response = False
        is_exception = False
        with self._sync_events_lock:
            is_response = seq in self._sync_raw_replies
            is_exception = seq in self._sync_raw_exceptions

        if is_response:
            if __debug__:
                synclogger.debug('Dispatch sync reply: {} - start'.format(seq))

            Connection._dispatch_reply(self, seq,
                                       self._sync_raw_replies.pop(seq))

            if __debug__:
                synclogger.debug(
                    'Dispatch sync reply: {} - complete'.format(seq))

        if is_exception:
            if __debug__:
                synclogger.debug(
                    'Dispatch sync exception: {} - start'.format(seq))

            Connection._dispatch_exception(self, seq,
                                           self._sync_raw_exceptions.pop(seq))

            if __debug__:
                synclogger.debug(
                    'Dispatch sync exception: {} - complete'.format(seq))

        if __debug__:
            synclogger.debug('Sync request: {} - complete'.format(seq))

        if self.closed:
            raise EOFError('Connection was closed, seq: {}'.format(seq))

        isexc, obj = self._sync_replies.pop(seq)
        if isexc:
            raise obj
        else:
            return obj
Example #10
0
 def _serve_client(self, sock, credentials):
     h, p = sock.getpeername()
     self.logger.info("welcome %s:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials = credentials)
         conn = Connection(self.service, Channel(SocketStream(sock)), 
             config = config, _lazy = True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye %s:%s", h, p)
Example #11
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     try:
         config = dict(self.protocol_config,
                       credentials=credentials,
                       endpoints=(sock.getsockname(), addrinfo),
                       logger=self.logger)
         conn = Connection(self.service,
                           Channel(SocketStream(sock)),
                           config=config)
         conn.serve_all()
     finally:
         pass
Example #12
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     if credentials:
         self.logger.info("welcome %s (%r)", addrinfo, credentials)
     else:
         self.logger.info("welcome %s", addrinfo)
     try:
         config = dict(self.protocol_config, credentials = credentials, 
             endpoints = (sock.getsockname(), addrinfo))
         conn = Connection(self.service, Channel(SocketStream(sock)),
             config = config, _lazy = True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye %s", addrinfo)
Example #13
0
    def _authenticate_and_build_connection(self, sock):
        """
        Authenticate a client and if it succees, wraps the socket
        in a connection object.  Note that this code is cut and paste
        from the rpyc internals and may have to be changed if rpyc
        evolves
        """

        # authenticate
        if self.authenticator:
            h, p = sock.getpeername()
            try:
                sock, credentials = self.authenticator(sock)
            except AuthenticationError:
                self.log_message(
                    "%s:%s failed to authenticate, rejecting connection",
                    h, p)
                return None
        else:
            credentials = None

        # build a connection
        h, p = sock.getpeername()
        config = dict(self.protocol_config,
                      credentials = credentials,
                      connid      = "%s:%d" % (h, p))
        return Connection(self.service,
                          Channel(SocketStream(sock)),
                          config = config)
Example #14
0
 def dispatch_data(self, data_received, addr):
     host, port = addr[0], addr[1]
     if addr not in self.clients:
         logging.info("new client connected : %s:%s" % (host, port))
         config = dict(self.protocol_config,
                       credentials=None,
                       connid="%s:%d" % (host, port))
         if self.authenticator:
             try:
                 sock, credentials = self.authenticator(data_received)
                 config["credentials"] = credentials
             except AuthenticationError:
                 logging.info("failed to authenticate, rejecting data")
                 raise
         self.clients[addr] = self.stream_class((self.sock, addr),
                                                self.transport_class,
                                                self.transport_kwargs,
                                                client_side=False)
         conn = Connection(self.service,
                           Channel(self.clients[addr]),
                           config=config,
                           _lazy=True)
         t = multiprocessing.Process(target=self.handle_new_conn,
                                     args=(conn, ))
         t.daemon = True
         t.start()
     with self.clients[addr].downstream_lock:
         self.clients[addr].buf_in.write(data_received)
         self.clients[addr].transport.downstream_recv(
             self.clients[addr].buf_in)
Example #15
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     h = addrinfo[0]
     p = addrinfo[1]
     if credentials:
         self.logger.info("welcome [%s]:%s (%r)", h, p, credentials)
     else:
         self.logger.info("welcome [%s]:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials = credentials)
         conn = Connection(self.service, Channel(SocketStream(sock)),
             config = config, _lazy = True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye [%s]:%s", h, p)
Example #16
0
 def _authenticate_and_build_connection(self, sock):
     '''Authenticate a client and if it succeeds, wraps the socket in a connection object.
     Note that this code is cut and paste from the rpyc internals and may have to be
     changed if rpyc evolves'''
     # authenticate
     if self.authenticator:
         addrinfo = sock.getpeername()
         h = addrinfo[0]
         p = addrinfo[1]
         try:
             sock, credentials = self.authenticator(sock)
         except AuthenticationError:
             self.logger.info(
                 "%s:%s failed to authenticate, rejecting connection", h, p)
             return None
     else:
         credentials = None
     # build a connection
     addrinfo = sock.getpeername()
     h = addrinfo[0]
     p = addrinfo[1]
     config = dict(self.protocol_config,
                   credentials=credentials,
                   connid="%s:%d" % (h, p))
     return Connection(self.service,
                       Channel(
                           self.stream_class(sock, self.transport_class,
                                             self.transport_kwargs)),
                       config=config)
Example #17
0
    def __init__(self, pupy_srv, *args, **kwargs):
        self._close_lock = Lock()
        self._sync_events_lock = Lock()
        self._async_events_lock = Lock()

        self._sync_events = {}
        self._sync_raw_replies = {}
        self._sync_raw_exceptions = {}

        self._last_recv = time.time()
        self._ping = False
        self._ping_timeout = 30
        self._serve_timeout = 10
        self._last_ping = None
        self._default_serve_timeout = 5
        self._queue = SyncRequestDispatchQueue.get_queue()

        self._timer_event = None
        self._timer_event_last = None

        if 'ping' in kwargs:
            ping = kwargs.pop('ping')
        else:
            ping = None

        if 'timeout' in kwargs:
            timeout = kwargs.pop('timeout')
        else:
            timeout = None

        if 'timer_event' in kwargs:
            self._timer_event = kwargs.pop('timer_event')

        if ping or timeout:
            self.set_pings(ping, timeout)

        kwargs['_lazy'] = True
        Connection.__init__(self, *args, **kwargs)
        if pupy_srv:
            self._local_root.pupy_srv = pupy_srv

        if 'config' in kwargs:
            self._config.update(kwargs['config'])

        next(self._seqcounter)

        logger.debug('New PupyConnection: (%s)', self)
Example #18
0
 def _wait(self):
     if not self._listener_poll.poll(0.01):
         return
     sock, addrinfo = self.listener.accept()
     self.clients.add(sock)
     config = dict(self.protocol_config, allow_all_attrs = True, allow_setattr = True)
     self._connection = Connection(self.service, Channel(SocketStream(sock)), config)
     self._connection_callback()
     self._log_message('client connected', addrinfo)
Example #19
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     if credentials:
         self.logger.info("welcome %s (%r)", addrinfo, credentials)
     else:
         self.logger.info("welcome %s", addrinfo)
     try:
         config = dict(self.protocol_config,
                       credentials=credentials,
                       endpoints=(sock.getsockname(), addrinfo),
                       logger=self.logger)
         conn = Connection(self.service,
                           Channel(SocketStream(sock)),
                           config=config,
                           _lazy=True)
         conn._init_service()
         self._handle_connection(conn)
     finally:
         self.logger.info("goodbye %s", addrinfo)
Example #20
0
    def _authenticate_and_build_connection(self, sock):
        '''Authenticate a client and if it succeeds, wraps the socket in a connection object.
        Note that this code is cut and paste from the rpyc internals and may have to be
        changed if rpyc evolves'''
        # authenticate
        if self.authenticator:
            addrinfo = sock.getpeername()
            h = addrinfo[0]
            p = addrinfo[1]
            try:
                sock, credentials = self.authenticator(sock)
            except AuthenticationError:
                self.logger.info(
                    "%s:%s failed to authenticate, rejecting connection", h, p)
                return None
        else:
            credentials = None
        # build a connection
        addrinfo = sock.getpeername()
        h = addrinfo[0]
        p = addrinfo[1]
        config = dict(self.protocol_config,
                      credentials=credentials,
                      connid="%s:%d" % (h, p))

        def check_timeout(event, cb, timeout=10):
            start_time = time.time()
            while True:
                if time.time() - start_time > timeout:
                    if not event.is_set():
                        logging.error("timeout occured !")
                        cb()
                    break
                elif event.is_set():
                    break
                time.sleep(0.5)

        stream = self.stream_class(sock, self.transport_class,
                                   self.transport_kwargs)

        event = threading.Event()
        t = threading.Thread(target=check_timeout, args=(event, stream.close))
        t.daemon = True
        t.start()
        try:
            c = Connection(self.service, Channel(stream), config=config)
        finally:
            event.set()
        return c
Example #21
0
class RPyCProtocol(protocol.Protocol):
    """
    RPyC twisted protocol - feeds data into a TwistedSocketStream instance
    which is read out by RPyC.performing
    """
    def __init__(self, service):
        self.service = service

    def connectionMade(self):
        self._sockstream = TwistedSocketStream(self)
        self._channel = Channel(self._sockstream)
        self._connection = Connection(self.service, self._channel, _lazy=True)
        self._connection._init_service()

    def connectionLost(self, reason):
        if hasattr(self, "_connection"):
            self._connection.close()

    def dataReceived(self, data):
        # Spoof synchronous incoming data:
        self._sockstream.dataReceived(data)
        # Do the rest of our processing in a separate thread so
        # we don't block the reactor:
        threads.deferToThread(self._connection.serve, 1)
Example #22
0
 def _serve_client(self, sock, credentials):
     h, p = sock.getpeername()
     self.logger.info("welcome %s:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials=credentials)
         conn = Connection(self.service,
                           Channel(SocketStream(sock)),
                           config=config,
                           _lazy=True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye %s:%s", h, p)
Example #23
0
    def dispatch_data(self, data_received, host=None, port=None):
        """ receive data, forward it to the stream and send back the stream downstream if any """
        decoded, cookie = self.void_stream.decode_data(data_received)
        if cookie is None:
            logging.debug("failed to retreived cookie, rejecting data %s" %
                          repr(data_received))
            return self.void_stream.encode_data("", None)
        if cookie not in self.clients:
            logging.info("new client connected : %s:%s cookie=%s" %
                         (host, port, cookie))
            config = dict(self.protocol_config,
                          credentials=None,
                          connid="%s:%d" % (host, port))
            if self.authenticator:
                try:
                    sock, credentials = self.authenticator(data_received)
                    config["credentials"] = credentials
                except AuthenticationError:
                    logging.info("failed to authenticate, rejecting data")
                    raise
            self.clients[cookie] = self.stream_class(
                (host, port), self.transport_class, self.transport_kwargs)
            self.clients[cookie].buf_in.cookie = cookie
            self.clients[cookie].buf_out.cookie = cookie
            conn = Connection(self.service,
                              Channel(self.clients[cookie]),
                              config=config,
                              _lazy=True)
            p = multiprocessing.Process(target=self.handle_new_conn,
                                        args=(conn, ))
            p.daemon = True
            p.start()
        resp = None
        with self.clients[cookie].upstream_lock:
            self.clients[cookie].upstream.write(decoded)
            #return self.void_stream.encode_data(self.clients[cookie].downstream.read(), cookie)
            resp = self.clients[cookie].downstream.read()
        if not resp:  # No data to send, so we send the default page with no data
            resp = self.void_stream.encode_data("", cookie)

        return resp
Example #24
0
 def _serve_client(self, sock, credentials):
     addrinfo = sock.getpeername()
     h = addrinfo[0]
     p = addrinfo[1]
     if credentials:
         self.logger.info("welcome [%s]:%s (%r)", h, p, credentials)
     else:
         self.logger.info("welcome [%s]:%s", h, p)
     try:
         config = dict(self.protocol_config, credentials = credentials, 
             endpoints = (sock.getsockname(), addrinfo))
         conn = Connection(self.service, Channel(SocketStream(sock)),
             config = config, _lazy = True)
         conn._init_service()
         conn.serve_all()
     finally:
         self.logger.info("goodbye [%s]:%s", h, p)
Example #25
0
 def __init__(self, lock, *args, **kwargs):
     self._sync_events = {}
     self._connection_serve_lock = lock
     self._last_recv = time.time()
     Connection.__init__(self, *args, **kwargs)
Example #26
0
    def sync_request(self, handler, *args):
        seq = self._send_request(handler, args)
        if __debug__:
            synclogger.debug('Sync request wait(%s): %s / %s:%s %s (%s)', self,
                             seq,
                             *traceback.extract_stack()[-4])

        self._sync_events[seq].wait()

        if __debug__:
            synclogger.debug('Sync request wait(%s): %s - complete', self, seq)

        del self._sync_events[seq]

        if __debug__:
            synclogger.debug('Sync request process(%s): %s', self, seq)

        is_response = False
        is_exception = False

        with self._sync_events_lock:
            is_response = seq in self._sync_raw_replies
            is_exception = seq in self._sync_raw_exceptions

        if is_response:
            if __debug__:
                synclogger.debug('Dispatch sync reply(%s): %s - start', self,
                                 seq)

            Connection._dispatch_reply(self, seq,
                                       self._sync_raw_replies.pop(seq))

            if __debug__:
                synclogger.debug('Dispatch sync reply(%s): %s - complete',
                                 self, seq)

        if is_exception:
            if __debug__:
                synclogger.debug('Dispatch sync exception(%s): %s - start',
                                 self, seq)
                synclogger.debug(
                    'Dispatch sync exception(%s): %s - handler = %s(%s) args = %s',
                    self, seq, self._HANDLERS[handler], handler, repr(args))

            Connection._dispatch_exception(self, seq,
                                           self._sync_raw_exceptions.pop(seq))

            if __debug__:
                synclogger.debug('Dispatch sync exception(%s): %s - complete',
                                 self, seq)

        if __debug__:
            synclogger.debug('Sync request(%s): %s - complete', self, seq)

        if self.closed:
            raise EOFError('Connection was closed, seq({}): {}'.format(
                self, seq))

        isexc, obj = self._sync_replies.pop(seq)
        if isexc:
            raise obj
        else:
            return obj
Example #27
0
 def close(self, *args):
     try:
         Connection.close(self, *args)
     finally:
         for lock in self._sync_events.itervalues():
             lock.set()
Example #28
0
 def connectionMade(self):
     self._sockstream = TwistedSocketStream(self)
     self._channel = Channel(self._sockstream)
     self._connection = Connection(self.service, self._channel, _lazy=True)
     self._connection._init_service()
Example #29
0
 def __init__(self, lock, *args, **kwargs):
     self._sync_events = {}
     self._connection_serve_lock = lock
     self._last_recv = time.time()
     Connection.__init__(self, *args, **kwargs)
Example #30
0
 def close(self, *args):
     try:
         Connection.close(self, *args)
     finally:
         for lock in self._sync_events.itervalues():
             lock.set()