コード例 #1
0
ファイル: connection.py プロジェクト: AlessandroZ/pupy
    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
コード例 #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()
コード例 #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)
コード例 #4
0
ファイル: connection.py プロジェクト: AlessandroZ/pupy
 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()
コード例 #5
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
コード例 #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))
コード例 #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
コード例 #8
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
コード例 #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
コード例 #10
0
ファイル: server.py プロジェクト: mwicat/s60rpyc
 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)
コード例 #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
コード例 #12
0
ファイル: server.py プロジェクト: IDA-RE-things/idalink
 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)
コード例 #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)
コード例 #14
0
ファイル: servers.py プロジェクト: zrohdes/pupy
 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)
コード例 #15
0
ファイル: server.py プロジェクト: chris-/rpyc
 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)
コード例 #16
0
ファイル: servers.py プロジェクト: welllima88/pupy
 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)
コード例 #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)
コード例 #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)
コード例 #19
0
ファイル: server.py プロジェクト: J0rdyZ65/script.module.rpyc
 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)
コード例 #20
0
ファイル: servers.py プロジェクト: zero-code/pupy
    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
コード例 #21
0
ファイル: __init__.py プロジェクト: oliverdrake/twisted-rpyc
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)
コード例 #22
0
ファイル: server.py プロジェクト: tws0002/hman
 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)
コード例 #23
0
ファイル: servers.py プロジェクト: zrohdes/pupy
    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
コード例 #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)
コード例 #25
0
ファイル: servers.py プロジェクト: LiuFang816/SALSTM_py_data
 def __init__(self, lock, *args, **kwargs):
     self._sync_events = {}
     self._connection_serve_lock = lock
     self._last_recv = time.time()
     Connection.__init__(self, *args, **kwargs)
コード例 #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
コード例 #27
0
 def close(self, *args):
     try:
         Connection.close(self, *args)
     finally:
         for lock in self._sync_events.itervalues():
             lock.set()
コード例 #28
0
ファイル: __init__.py プロジェクト: oliverdrake/twisted-rpyc
 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()
コード例 #29
0
ファイル: servers.py プロジェクト: dc3l1ne/pupy
 def __init__(self, lock, *args, **kwargs):
     self._sync_events = {}
     self._connection_serve_lock = lock
     self._last_recv = time.time()
     Connection.__init__(self, *args, **kwargs)
コード例 #30
0
ファイル: servers.py プロジェクト: dc3l1ne/pupy
 def close(self, *args):
     try:
         Connection.close(self, *args)
     finally:
         for lock in self._sync_events.itervalues():
             lock.set()