Esempio n. 1
0
def test_tcpsocket(serversocket):
    sock = None

    sockargs = [('localhost:65432', 1), ('localhost', 65432),
                (('localhost', 65432), 1, dict(timeout=5))]
    for args in sockargs:
        try:
            if len(args) == 3:
                kwds = args[2]
                args = args[:2]
                sock = tcpSocket(*args, **kwds)
            else:
                sock = tcpSocket(*args)
        finally:
            if sock:
                closeSocket(sock)
Esempio n. 2
0
    def connect_events(self, conndata):
        # connect to event port
        try:
            self.event_sock = tcpSocket(conndata.host, conndata.port)
        except OSError as err:
            msg = err.args[1]
            self.signal('failed', 'Event connection failed: %s.' % msg, err)
            return

        # write client id to ensure we get registered as event connection
        self.event_sock.sendall(self.client_id)
Esempio n. 3
0
 def cache_wait_cb():
     if wait:
         start = time.time()
         while time.time() < start + wait:
             try:
                 s = tcpSocket(hostport, 0)
             except socket.error:
                 time.sleep(0.02)
             except Exception as e:
                 sys.stderr.write('%r' % e)
                 raise
             else:
                 s.close()
                 break
         else:
             raise Exception('cache failed to start within %s sec' % wait)
Esempio n. 4
0
def daemon_wait_cb():
    start = time.time()
    wait = 10
    s = None
    while time.time() < start + wait:

        try:
            s = tcpSocket(daemon_addr, 0)
        except OSError:
            time.sleep(0.02)
        else:
            s.close()
            break
        finally:
            if s:
                s.close()
    else:
        raise Exception('daemon failed to start within %s sec' % wait)
Esempio n. 5
0
 def _connect(self):
     self._do_callbacks = False
     self._startup_done.clear()
     self.log.debug('connecting to %s', self.cache)
     try:
         self._socket = tcpSocket(self.cache,
                                  DEFAULT_CACHE_PORT,
                                  timeout=5,
                                  keepalive=10)
     except Exception as err:
         self._disconnect('unable to connect to %s: %s' % (self.cache, err))
     else:
         self.log.info('now connected to %s', self.cache)
         self._connected = True
         self._disconnect_warnings = 0
         try:
             self._connect_action()
         except Exception as err:
             self._disconnect('unable to init connection to %s: %s' %
                              (self.cache, err))
     self._startup_done.set()
     self._do_callbacks = self.remote_callbacks
Esempio n. 6
0
    def _single_request(self, tosend, sentinel=b'\n', retry=2, sync=False):
        """Communicate over the secondary socket."""
        if not self._socket:
            self._disconnect('single request: no socket')
            if not self._socket:
                raise CacheError('cache not connected')
        if sync:
            # sync has to be false for lock requests, as these occur during startup
            self._queue.join()
        with self._sec_lock:
            if not self._secsocket:
                try:
                    self._secsocket = tcpSocket(self.cache, DEFAULT_CACHE_PORT)
                except Exception as err:
                    self.log.warning(
                        'unable to connect secondary socket '
                        'to %s: %s', self.cache, err)
                    self._secsocket = None
                    self._disconnect('secondary socket: could not connect')
                    raise CacheError('secondary socket could not be created')

            try:
                # write request
                # self.log.debug("get_explicit: sending %r", tosend)
                self._secsocket.sendall(to_utf8(tosend))

                # give 10 seconds time to get the whole reply
                timeout = currenttime() + 10
                # read response
                data = b''
                while not data.endswith(sentinel):
                    newdata = self._secsocket.recv(BUFSIZE)  # blocking read
                    if not newdata:
                        raise socket.error('cache closed connection')
                    if currenttime() > timeout:
                        # do not just break, we need to reopen the socket
                        raise socket.error('getting response took too long')
                    data += newdata
            except socket.error:
                self.log.warning('error during cache query', exc=1)
                closeSocket(self._secsocket)
                self._secsocket = None
                if retry:
                    for m in self._single_request(tosend, sentinel, retry - 1):
                        yield m
                    return
                raise

        lmatch = line_pattern.match
        mmatch = msg_pattern.match
        i = 0
        # self.log.debug("get_explicit: data =%r", data)
        match = lmatch(data, i)
        while match:
            line = match.group(1)
            i = match.end()
            msgmatch = mmatch(from_utf8(line))
            if not msgmatch:
                # ignore invalid lines
                continue
            # self.log.debug('line processed: %r', line)
            yield msgmatch
            match = lmatch(data, i)
Esempio n. 7
0
 def _open_cached_socket(self, *args, **kwds):
     if not TestCacheClient._use_cache:
         return tcpSocket(*args, **kwds)
     if TestCacheClient._cached_socket is None:
         TestCacheClient._cached_socket = tcpSocket(*args, **kwds)
     return TestCacheClient._cached_socket
Esempio n. 8
0
 def connect(self, conndata):
     self.client_id = uuid.uuid1().bytes
     self.sock = tcpSocket(conndata.host, conndata.port, timeout=30.0)
     # write client identification: we are a new client
     self.sock.sendall(self.client_id)