def getEvent(self):
     event = None
     evJsonStr = self.__comm.getEvent()
     if not evJsonStr is None:
         event = jsonhelpers.decode(servicedefs.Event, evJsonStr)
         if event.event == "syntaxDiag" or event.event == "semanticDiag":
             event = jsonhelpers.decode(servicedefs.DiagnosticEvent, evJsonStr)
     return event
Ejemplo n.º 2
0
 def getEvent(self):
     event = None
     evJsonStr = self.__comm.getEvent()
     if not evJsonStr is None:
         event = jsonhelpers.decode(servicedefs.Event, evJsonStr)
         if event.event == "syntaxDiag" or event.event == "semanticDiag":
             event = jsonhelpers.decode(servicedefs.DiagnosticEvent, evJsonStr)
     return event
Ejemplo n.º 3
0
 def makeTimeoutMsg(self, cmd, seq):
     jsonDict = jsonhelpers.decode(cmd)
     timeoutMsg = {
         "seq": 0,
         "type": "response",
         "success": False,
         "request_seq": seq,
         "command": jsonDict["command"],
         "message": "timeout"
     }
     return timeoutMsg
Ejemplo n.º 4
0
 def makeTimeoutMsg(self, cmd, seq):
    jsonDict = jsonhelpers.decode(cmd)
    timeoutMsg = {
       "seq": 0,
       "type": "response",
       "success": False,
       "request_seq": seq,
       "command": jsonDict["command"],
       "message": "timeout"
    }
    return timeoutMsg
Ejemplo n.º 5
0
 def sendCmdSync(self, cmd, seq):
     """
     Sends the command and wait for the result and returns it
     """
     if self.postCmd(cmd):
         reqSeq = -1
         try:
             while reqSeq < seq:
                 data = self.__msgq.get(True, 1)
                 dict = jsonhelpers.decode(data)
                 reqSeq = dict['request_seq']
             return dict
         except queue.Empty:
             print("queue timeout")
             return self.makeTimeoutMsg(cmd, seq)
     else:
         return self.makeTimeoutMsg(cmd, seq)
Ejemplo n.º 6
0
 def sendCmdSync(self, cmd, seq):
     """
     Sends the command and wait for the result and returns it
     """
     if self.postCmd(cmd):
        reqSeq = -1
        try: 
            while reqSeq < seq:
                data = self.__msgq.get(True,1)
                dict = jsonhelpers.decode(data)
                reqSeq = dict['request_seq']
            return dict
        except queue.Empty:
            print("queue timeout")
            return self.makeTimeoutMsg(cmd, seq)
     else:
         return self.makeTimeoutMsg(cmd, seq)
Ejemplo n.º 7
0
    def __readMsg(stream, msgq, eventq, asyncReq, proc):
        """
        Reader thread helper
        """
        state = "init"
        bodlen = 0
        while state != "body":
            header = stream.readline().strip()
            log.debug('Stream state: "{0}".  Read header: "{1}"'.format(
                state, header if header else 'None'))

            if len(header) == 0:
                if state == 'init':
                    # log.info('0 byte line in stream when expecting header')
                    return proc.poll() != None
                else:
                    state = "body"
            else:
                state = 'header'
                if header.startswith(NodeCommClient.__CONTENT_LENGTH_HEADER):
                    bodlen = int(
                        header[len(NodeCommClient.__CONTENT_LENGTH_HEADER):])

        if bodlen > 0:
            data = stream.read(bodlen)
            log.debug('Read body of length: {0}'.format(bodlen))
            jsonStr = data.decode("utf-8")
            dict = jsonhelpers.decode(jsonStr)
            if dict['type'] == "response":
                request_seq = dict['request_seq']
                log.debug('Body sequence#: {0}'.format(request_seq))
                if request_seq in asyncReq:
                    callback = asyncReq.pop(request_seq, None)
                    if callback:
                        callback(dict)
                        return False
                else:
                    # Only put in the queue if wasn't an async request
                    msgq.put(jsonStr)
            else:
                eventq.put(jsonStr)
        else:
            log.info('Body length of 0 in server stream')
            return False
Ejemplo n.º 8
0
    def __readMsg(stream, msgq, eventq, asyncReq, proc):
        """
        Reader thread helper
        """
        state = "init"
        bodlen = 0
        while state != "body":
            header = stream.readline().strip()
            log.debug('Stream state: "{0}".  Read header: "{1}"'.format(
                                        state, header if header else 'None'))

            if len(header) == 0:
                if state == 'init':
                    # log.info('0 byte line in stream when expecting header')
                    return proc.poll() != None
                else:
                    state = "body"
            else:
                state = 'header'
                if header.startswith(NodeCommClient.__CONTENT_LENGTH_HEADER):
                    bodlen = int(header[len(NodeCommClient.__CONTENT_LENGTH_HEADER):])

        if bodlen > 0:
            data = stream.read(bodlen)
            log.debug('Read body of length: {0}'.format(bodlen))
            jsonStr = data.decode("utf-8")
            dict = jsonhelpers.decode(jsonStr)
            if dict['type'] == "response":
                request_seq = dict['request_seq']
                log.debug('Body sequence#: {0}'.format(request_seq))
                if request_seq in asyncReq:
                    callback = asyncReq.pop(request_seq, None)
                    if callback:
                        callback(dict)
                        return False
                else:
                    # Only put in the queue if wasn't an async request
                    msgq.put(jsonStr)
            else:
                eventq.put(jsonStr)
        else:
            log.info('Body length of 0 in server stream')
            return False
Ejemplo n.º 9
0
 def sendCmd(self, cb, cmd, seq):
     """
     send single-line command string; no sequence number; wait for response
     this assumes stdin/stdout; for TCP, need to add correlation with sequence numbers
     """
     if self.postCmd(cmd):
         reqSeq = -1
         try:
             while reqSeq < seq:
                 data = self.__msgq.get(True, 1)
                 dict = jsonhelpers.decode(data)
                 reqSeq = dict['request_seq']
             if cb:
                 cb(dict)
         except queue.Empty:
             print("queue timeout")
             if (cb):
                 cb(self.makeTimeoutMsg(cmd, seq))
     else:
         if (cb):
             cb(self.makeTimeoutMsg(cmd, seq))
Ejemplo n.º 10
0
 def sendCmd(self, cb, cmd, seq):
     """
     send single-line command string; no sequence number; wait for response
     this assumes stdin/stdout; for TCP, need to add correlation with sequence numbers
     """
     if self.postCmd(cmd):
        reqSeq = -1
        try:
           while reqSeq < seq:
              data = self.__msgq.get(True,1)
              dict = jsonhelpers.decode(data)
              reqSeq = dict['request_seq']
           if cb:
              cb(dict)
        except queue.Empty:
           print("queue timeout")
           if (cb):
              cb(self.makeTimeoutMsg(cmd, seq))
     else:
        if (cb):
           cb(self.makeTimeoutMsg(cmd, seq))
Ejemplo n.º 11
0
 def __readMsg(stream, msgq, eventq):
     """
     Reader thread helper
     """
     state = "headers"
     bodlen = 0
     while state == "headers":
         header = stream.readline().strip()
         if len(header) == 0:
             state = "body"
         elif header.startswith(NodeCommClient.__CONTENT_LENGTH_HEADER):
             bodlen = int(
                 header[len(NodeCommClient.__CONTENT_LENGTH_HEADER):])
     # TODO: signal error if bodlen == 0
     if bodlen > 0:
         data = stream.read(bodlen)
         jsonStr = data.decode("utf-8")
         msg = jsonhelpers.decode(servicedefs.Message, jsonStr)
         if msg.type == "response":
             msgq.put(jsonStr)
         else:
             print("event:")
             print(jsonStr)
             eventq.put(jsonStr)
Ejemplo n.º 12
0
 def getEvent(self):
     event_json_str = self.__comm.getEvent()
     return jsonhelpers.decode(event_json_str) if event_json_str is not None else None