示例#1
0
 def replyArrived(self, reply):
     if reply.error:
         if callable(self.__error_callback):
             try:
                 self.__error_callback(reply.error)
             except:
                 log.exception("Error while calling error callback (command=%s,spec version=%s)", self.command, self.specVersion)
             self.__error_callback = None
     else:
         if callable(self.__callback):
             try:
                 self.__callback(reply.data)
             except:
                 log.exception("Error while calling reply callback (command=%s,spec version=%s)", self.command, self.specVersion)
             self.__callback = None
示例#2
0
    def replyArrived(self, reply):
        log.log(2, "reply arrived for command")
        self.reply_pending = False
        self.retvalue = reply.get_data()

        if reply.error:
            if callable(self.__error_callback):
                try:
                    self.__error_callback(reply.error)
                except:
                    log.exception("Error while calling error callback (command=%s,spec version=%s)", self.command, self.specapp)
                self.__error_callback = None
        else:
            if callable(self.__callback):
                try:
                    self.__callback(reply.data)
                except:
                    log.exception("Error while calling reply callback (command=%s,spec version=%s)", self.command, self.specapp)
                self.__callback = None
示例#3
0
    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:]]