def dataReceived(self, data):
        '''This method accumulates the data received till
        we have a complete log message. Then it pulls the log message 
        out and to logger.handle as a logrecord. This
        method is called by the Protocol parent class when data is
        received from the socket attached to this protocol. This 
        method has to handle possible multiple messages per buffer 
        and partial messages per buffer.

        Parameters:
        data           string of data received by the socket this
                       server is attached to contains the data sent
                       by logging.handlers.SocketHandler
        '''
        logRecord = None

        # get an alias to the LONG_INT_LENGTH
        long_int_length = LoggingProtocol.LONG_INT_LENGTH

        # paste the recieved data onto what we have
        self._buffer += data

        # keep processing the buffer till we need more data
        done = False
        while not done:
            # do we have enough data to pull off the leading big
            # endian long integer?
            if len(self._buffer) >= long_int_length:
                length = struct.unpack(">L", \
                                       self._buffer[:long_int_length])[0]

                # do we have the complete logging message?
                if len(self._buffer) >= long_int_length + length:
                    # get the pickled log message
                    logPickle = self._buffer[long_int_length:long_int_length +
                                             length]
                    logRecord = logging.makeLogRecord(cPickle.loads(logPickle))

                    # do we have a logrecord?, then handle it
                    if logRecord:
                        self._logger.handle(logRecord)
                        model.logRecordHandler(logRecord)

                    # update the class buffer with what we have left
                    self._buffer = self._buffer[long_int_length + length:]

                # otherwise, we don't have a complete message
                else:
                    done = True
            # otherwise, don't have enough data for length value
            else:
                done = True
    def dataReceived(self, data):
        '''This method accumulates the data received till
        we have a complete log message. Then it pulls the log message 
        out and to logger.handle as a logrecord. This
        method is called by the Protocol parent class when data is
        received from the socket attached to this protocol. This 
        method has to handle possible multiple messages per buffer 
        and partial messages per buffer.

        Parameters:
        data           string of data received by the socket this
                       server is attached to contains the data sent
                       by logging.handlers.SocketHandler
        '''
        logRecord = None 
        
        # get an alias to the LONG_INT_LENGTH
        long_int_length = LoggingProtocol.LONG_INT_LENGTH
        
        # paste the recieved data onto what we have
        self._buffer += data

        # keep processing the buffer till we need more data
        done = False
        while not done:
            # do we have enough data to pull off the leading big 
            # endian long integer?
            if len(self._buffer) >= long_int_length:
                length = struct.unpack(">L", \
                                       self._buffer[:long_int_length])[0]
                
                # do we have the complete logging message?
                if len(self._buffer) >= long_int_length + length:
                    # get the pickled log message
                    logPickle = self._buffer[long_int_length : long_int_length + length]
                    logRecord = logging.makeLogRecord(cPickle.loads(logPickle))
                    
                    # do we have a logrecord?, then handle it
                    if logRecord:
                        self._logger.handle(logRecord)
                        model.logRecordHandler(logRecord)
    
                    # update the class buffer with what we have left
                    self._buffer = self._buffer[long_int_length + length:]
                    
                # otherwise, we don't have a complete message
                else:
                    done = True
            # otherwise, don't have enough data for length value
            else:
                done = True
Esempio n. 3
0
    def dataReceived(self, data):
        """Called whenever there's data available in the socket.

        Pulls the newly received data from the socket, adds it to what we've
        already got, then checks to see whether we've got enough to start
        processing a new record.

        If so, we build as much of the log record as we can and, whenever we
        have a complete one, pass it off to Python's logging system.
        """

        ##
        # First, paste the recieved data onto what we have and compute the
        # buffer's length only once rather than every time we need it.
        ##
        self.buffer += data
        self.buffer_len = len(self.buffer)

        ##
        # Keep processing the buffer, peeling off logging records, till we
        # no longer have a complete record, then exit.  We'll get called again
        # as soon as there's more data available.
        ##
        while True:
            ##
            # If we've not yet gotten the record length for the next record,
            # and we have enough data to get it, do so.
            ##
            if not self.rec_len and self.buffer_len >= LONG_INT_LEN:
                self.rec_len = unpack(">L", self.buffer[:LONG_INT_LEN])[0]
                self.full_buffer_len = LONG_INT_LEN + self.rec_len

            ##
            # If we've gotten the length, and there's enough data in the
            # buffer to build our record, do so.
            #
            # Otherwise, we're done (for now).
            ##
            if (self.rec_len and self.buffer_len >= self.full_buffer_len):
                ##
                # get the pickled log message, from the end of the length bytes
                # to the end of full_buffer_len i.e. just the data
                ##
                logPickle = self.buffer[LONG_INT_LEN:self.full_buffer_len]

                ##
                # Create an actual log record from the data
                ##
                logRecord = logging.makeLogRecord(loads(logPickle))

                ##
                # Send the completed log record where it needs to go
                ##
                log.msg("LoggingProtocol: logging new record")
                self.logger.handle(logRecord)
                model.logRecordHandler(logRecord)

                ##
                # Adjust our buffer to point past the end of what we just
                # processed and recompute the length
                ##
                self.buffer = self.buffer[self.full_buffer_len:]
                self.buffer_len = len(self.buffer)

                ##
                # Unset self.rec_len and self.full_buffer_len since we don't
                # yet know the length of the next one.  When we loop around,
                # we'll take care of that if we've got enough data to work on.
                ##
                self.rec_len = self.full_buffer_len = None
            else:
                ##
                # otherwise, we either don't know the length,
                # or don't have a complete record, done for now
                ##
                break
    def dataReceived(self, data):
        """Called whenever there's data available in the socket.

        Pulls the newly received data from the socket, adds it to what we've
        already got, then checks to see whether we've got enough to start
        processing a new record.

        If so, we build as much of the log record as we can and, whenever we
        have a complete one, pass it off to Python's logging system.
        """

        ##
        # First, paste the recieved data onto what we have and compute the
        # buffer's length only once rather than every time we need it.
        ##
        self.buffer += data
        self.buffer_len = len(self.buffer)

        ##
        # Keep processing the buffer, peeling off logging records, till we
        # no longer have a complete record, then exit.  We'll get called again
        # as soon as there's more data available.
        ##
        while True:
            ##
            # If we've not yet gotten the record length for the next record,
            # and we have enough data to get it, do so.
            ##
            if not self.rec_len and self.buffer_len >= LONG_INT_LEN:
                self.rec_len = unpack(">L", self.buffer[:LONG_INT_LEN])[0]
                self.full_buffer_len = LONG_INT_LEN + self.rec_len

            ##
            # If we've gotten the length, and there's enough data in the
            # buffer to build our record, do so.
            #
            # Otherwise, we're done (for now).
            ##
            if (self.rec_len and
                self.buffer_len >= self.full_buffer_len):
                ##
                # get the pickled log message, from the end of the length bytes
                # to the end of full_buffer_len i.e. just the data
                ##
                logPickle = self.buffer[LONG_INT_LEN : self.full_buffer_len]

                ##
                # Create an actual log record from the data
                ##
                logRecord = logging.makeLogRecord(loads(logPickle))

                ##
                # Send the completed log record where it needs to go
                ##
                log.msg("LoggingProtocol: logging new record")
                self.logger.handle(logRecord)
                model.logRecordHandler(logRecord)

                ##
                # Adjust our buffer to point past the end of what we just
                # processed and recompute the length
                ##
                self.buffer = self.buffer[self.full_buffer_len:]
                self.buffer_len = len(self.buffer)

                ##
                # Unset self.rec_len and self.full_buffer_len since we don't
                # yet know the length of the next one.  When we loop around,
                # we'll take care of that if we've got enough data to work on.
                ##
                self.rec_len = self.full_buffer_len = None
            else:
                ##
                # otherwise, we either don't know the length,
                # or don't have a complete record, done for now
                ##
                break