def readHeader(self, rawstring):
        self.magic, self.vers, self.size, self.sn, \
                    self.sec, self.usec, self.cmd, \
                    datatype, self.rows, self.cols, \
                    datalen, self.err, self.flags, name  = struct.unpack(self.packedHeaderDataFormat, rawstring)
        if self.magic != MAGIC_NUMBER:
            self.packedHeaderDataFormat = ">" + self.packedHeaderDataFormat[1:]
            self.magic, self.vers, self.size, self.sn, \
                    self.sec, self.usec, self.cmd, \
                    datatype, self.rows, self.cols, \
                    datalen, self.err, self.flags, name  = struct.unpack(self.packedHeaderDataFormat, rawstring)
        self.time = self.sec + float(self.usec) / 1E6

        if is_python3():
            name = name.decode()

        name = name.replace(NULL, '')  #remove padding null bytes
        self.name = name.strip()

        if self.err > 0:
            datatype = ERROR  #change message type to 'ERROR' for further processing

        return (datatype, datalen)
    def handle_read(self):
        """Handle 'read' events on socket

        Messages are built from the read calls on the socket.
        """
        _received = self.recv(32768)

        #if is_python3():
        #_received = _received.decode('utf-8')

        self.receivedStrings.append(_received)

        log.log(DEBUG, "received new data from spec")

        if is_python3():
            s = b''.join(self.receivedStrings)
            sbuffer = memoryview(s)
            #sbuffer = bytes(sbuffer).decode('utf-8')
        else:
            s = ''.join(self.receivedStrings)
            sbuffer = buffer(s)

        consumedBytes = 0
        offset = 0

        while offset < len(sbuffer):
            if self.message is None:
                log.log(DEBUG, "creating a new message")
                self.message = SpecMessage.message(version=self.serverVersion)
            else:
                log.log(DEBUG, "adding data to existing message")

            consumedBytes = self.message.readFromStream(sbuffer[offset:])
            log.log(DEBUG, "  bytes consumed %s" % consumedBytes)

            log.log(DEBUG, " message complete? %s" % self.message.isComplete())

            if consumedBytes == 0:
                log.log(DEBUG, "  no bytes consumed")
                break

            offset += consumedBytes

            if self.message.isComplete():
                log.log(
                    DEBUG, "new message from spec %s / name %s" %
                    (self.message.cmd, self.message.name))
                try:
                    # dispatch incoming message
                    if self.message.cmd == SpecMessage.REPLY:
                        replyID = self.message.sn

                        if replyID > 0:
                            try:
                                reply = self.registeredReplies[replyID]
                            except:
                                log.exception(
                                    "Unexpected error while receiving a message from server"
                                )
                            else:
                                del self.registeredReplies[replyID]

                                reply.update(
                                    self.message.data,
                                    self.message.type == SpecMessage.ERROR,
                                    self.message.err)
                                #SpecEventsDispatcher.emit(self, 'replyFromSpec', (replyID, reply, ))
                    elif self.message.cmd == SpecMessage.EVENT:
                        log.log(
                            2, "got a new event from spec name=%s, data=%s" %
                            (self.message.name, self.message.data))
                        try:
                            self.registeredChannels[self.message.name].update(
                                self.message.data,
                                self.message.flags == SpecMessage.DELETED)
                        except KeyError:
                            import traceback
                            log.log(2, traceback.format_exc())
                        except:
                            import traceback
                            log.log(2, traceback.format_exc())
                        log.log(
                            DEBUG,
                            "AFTER event: %s" % str(self.registeredChannels))
                    elif self.message.cmd == SpecMessage.HELLO_REPLY:
                        if self.checkourversion(self.message.name):
                            self.serverVersion = self.message.vers  #header version
                            #self.state = CONNECTED
                            log.log(DEBUG, "Hello reply. all right. connected")
                            self.specConnected()
                        else:
                            self.serverVersion = None
                            self.connected = False
                            self.close()
                            self.state = DISCONNECTED
                except:
                    self.message = None
                    self.receivedStrings = [s[offset:]]
                    raise
                else:
                    self.message = None

        self.receivedStrings = [s[offset:]]
Exemple #3
0
def SpecArray(data, datatype=ARRAY_CHAR, rows=0, cols=0):

    if isinstance(data, SpecArrayData):
        # create a SpecArrayData from a SpecArrayData ("copy" constructor)
        return SpecArrayData(data.data, data.type, data.shape)

    if datatype == ARRAY_STRING:
        # a list of strings
        newArray = filter(
            None, [x != chr(0) and x or None for x in data.split(chr(0))])
        return newArray
    else:
        newArray = None

    if isinstance(data, numpy.ndarray):
        # convert from a Num* array to a SpecArrayData instance
        # (when you send)
        if len(data.shape) > 2:
            raise SpecArrayError(
                "Spec arrays cannot have more than 2 dimensions")

        try:
            if type(data) == numpy.ndarray:
                numtype = data.dtype.type
                datatype = NUM_TO_SPEC[numtype]
            else:
                numtype = data.typecode()
                datatype = NUM_TO_SPEC[numtype]
        except KeyError:
            data = ''
            datatype = ARRAY_CHAR
            rows = 0
            cols = 0
            log.error("Numerical type '%s' not supported", numtype)
        else:
            if len(data.shape) == 2:
                rows, cols = data.shape
            else:
                rows, cols = 1, data.shape[0]
            data = data.tostring()

        newArray = SpecArrayData(data, datatype, (rows, cols))
    else:
        # return a new Num* array from data
        # (when you receive)
        try:
            numtype = SPEC_TO_NUM[datatype]
        except:
            raise SpecArrayError('Invalid Spec array type')
        else:
            if is_python3():
                newArray = numpy.frombuffer(data, dtype=numtype)
            else:
                newArray = numpy.fromstring(data, dtype=numtype)

            if rows == 1:
                newArray.shape = (cols, )
            else:
                newArray.shape = (rows, cols)

    return newArray