Esempio n. 1
0
    def __receive_thread_worker(self):
        waitobj = IOWait()
        waitobj.watch( self._socket, read=True)
        ### BMX poller = select.poll()
        ### BMX poller.register(self._socket, select.POLLIN | select.POLLPRI | select.POLLHUP | select.POLLERR)
        __buffer = b''

        self._is_receiving = True
        self._receiving_callback and self._receiving_callback(self)
        while self._is_connected and self.__running:
            ### BMX events = poller.poll(1000)
            events = waitobj.wait(1000)     ### BMX
            ### BMX for fd, event in events:
            for fileno, read, write in events:  ### BMX
                ### BMX if event & select.POLLHUP:
                ### BMX     self.logger.warning("Client socket closed")
                # Check if POLLIN event triggered
                ### BMX if event & (select.POLLIN | select.POLLPRI):
                if read:
                    msg = self._socket.recv(4096)
                    # Check if incoming message is not empty
                    if msg:
                        # If we transfer in text mode decode message to string
                        if not self._binary:
                            msg = str.rstrip(str(msg, 'utf-8'))
                        # If we work in line mode (with a terminator) slice buffer into single chunks based on terminator
                        if self.terminator:
                            __buffer += msg
                            while True:
                                # terminator = int means fixed size chunks
                                if isinstance(self.terminator, int):
                                    i = self.terminator
                                    if i > len(__buffer):
                                        break
                                # terminator is str or bytes means search for it
                                else:
                                    i = __buffer.find(self.terminator)
                                    if i == -1:
                                        break
                                    i += len(self.terminator)
                                line = __buffer[:i]
                                __buffer = __buffer[i:]
                                if self._data_received_callback is not None:
                                    self._data_received_callback(self, line)
                        # If not in terminator mode just forward what we received
                        else:
                            if self._data_received_callback is not None:
                                self._data_received_callback(self, msg)
                    # If empty peer has closed the connection
                    else:
                        # Peer connection closed
                        self.logger.warning("Connection closed by peer {}".format(self._host))
                        self._is_connected = False
                        ### BMX poller.unregister()
                        waitobj.unwatch(self._socket)
                        self._disconnected_callback and self._disconnected_callback(self)
                        if self._autoreconnect:
                            self.logger.debug("Autoreconnect enabled for {}".format(self._host))
                            self.connect()
        self._is_receiving = False
Esempio n. 2
0
 def __init__(self):
     Base.__init__(self)
     Base._poller = self
     if hasattr(select, 'epoll'):
         self._epoll = select.epoll()
     elif hasattr(select, 'kqueue'):
         self._kqueue = select.kqueue()
     else:
         logger.debug("Init connections using IOWait")
         self._connections_found = 0
         self._waitobj = IOWait()
Esempio n. 3
0
    def __init__(self, streamer, name, callback_time=1., io_loop=None):
        super(SocketStatsCollector, self).__init__(streamer, name,
                                                   callback_time, io_loop)
        # if gevent is installed, we'll use a greenlet,
        # otherwise we'll use a thread
        try:
            import gevent  # NOQA
            self.greenlet = True
        except ImportError:
            self.greenlet = False

        self._rstats = defaultdict(int)
        if self.greenlet:
            self.poller = SelectIOWait()
        else:
            self.poller = IOWait()

        for sock, address, fd in self.streamer.get_sockets():
            self.poller.watch(sock, read=True, write=False)

        self._p = ioloop.PeriodicCallback(self._select,
                                          _LOOP_RES,
                                          io_loop=io_loop)