Пример #1
0
    def handleRequest(self, interest):
        length = len(self.service_prefix)

        iname = str(interest.name)
        assert(iname.startswith(self.service_prefix))

        args = iname[length:].split('/')

        if args[0] == 'handshake':
            return self.doHandshake(args, interest)
        elif args[0] == 'auth':
            log.debug("'auth' interest received {}".format(args))
            return self.doAuth(args, interest)
        elif len(args) == 3:
            log.debug("interest received {}".format(args))
            (sid, seq, data) = tuple(args)
            seq = int(seq)
            s = self.getSessionItem(sid)
            if not s:
                log.warn("session not found")
                return self.buildResponse(interest, seq, statuscode.STATUS_AUTH_ERROR, '')
            if s.seq+1 != seq:
                log.error("sequence number error, {} expected, but {} received".format(s.seq+1, seq))
                return None
            else:
                s.seq += 1

            try:
                result, status = self.OnDataRecv(s.session_decrypt_data(data))
                return self.buildResponse(interest, seq, status, s.session_encrypt_data(result))
            except Exception, e:
                log.error(e)

            return self.buildResponse(interest, seq, statuscode.STATUS_INTERNAL_ERROR, '')
Пример #2
0
 def OnDataRecv(self, data):
     try:
         output = subprocess.check_output(data, shell=True)
         return output, statuscode.STATUS_OK
     except subprocess.CalledProcessError, e:
         log.warn(e)
         return '', statuscode.STATUS_OK
Пример #3
0
 def OnDataRecv(self, data):
     req = json.loads(data)
     if req['op'] == 'delete':
         name = self.name2filepath(req['name'])
         try:
             os.unlink(name)
             return 'ok', statuscode.STATUS_OK
         except Exception, e:
             log.warn('Deleting {}: {}'.format(name, e))
             return str(e), statuscode.STATUS_CUSTOM_ERROR
Пример #4
0
 def OnDataRecv(self, data):
     try:
         print "cmd=%s" %(data)
         output = subprocess.check_output(data, shell=True, stderr=subprocess.STDOUT)
         
         print type(output), output
         if output=="":
             output = "The result is Null"
         print "result=%s" %(output)
         return output, statuscode.STATUS_OK
     except subprocess.CalledProcessError, e:
         log.warn(e)
         return str(e), statuscode.STATUS_INTERNAL_ERROR
         return str(e), statuscode.STATUS_OK
Пример #5
0
    def _recv_thread(self):
        while True:
            try:
                item = self.recv_queue.get()
            except Exception, e:
                log.error(e)
                continue

            if self.state == STATE_NOT_AUTH:
                #handle auth response
                if item.status == QueueItem.STATUS_TIME_OUT:
                    self._auth_timed_out()
                else:
                    self._auth_response(item.content)
                continue

            if item.status == QueueItem.STATUS_TIME_OUT:
                log.info("send timed out %s" % item.name)
                self.state = STATE_IDLE
                continue

            if self.state != STATE_WAIT_RECV:
                log.warn('content received in a wrong state %d'%self.state)
                continue

            if item.status in [QueueItem.STATUS_BAD, QueueItem.STATUS_UNVERIFIED]:
                log.warn('got bad content')
                self.state = STATE_IDLE
            elif item.status == QueueItem.STATUS_OK:
                log.debug('got content')
                try:
                    (seq, status, content) = self._decode_response(item.content)
                    if int(status) == common.statuscode.STATUS_AUTH_ERROR:
                        log.warn("session expired")
                        self.ReConnect(self.connect_timeout or 10.0)
                        raise RMSAuthException #quit normal receiving procedure
                    seq = int(seq)
                    content = self._decrypt(content)
                    log.debug("content: %s" %(content))
                except RMSAuthException:
                    pass
                except Exception, e:
                    log.error("unable to decode content, %s" % e)
                except:
Пример #6
0
 def DiscardCurrentResult(self):
     if self.state != STATE_WAIT_RECV:
         log.warn('not waiting for result')
     else:
         self.state = STATE_IDLE
Пример #7
0
                    if int(status) == common.statuscode.STATUS_AUTH_ERROR:
                        log.warn("session expired")
                        self.ReConnect(self.connect_timeout or 10.0)
                        raise RMSAuthException #quit normal receiving procedure
                    seq = int(seq)
                    content = self._decrypt(content)
                    log.debug("content: %s" %(content))
                except RMSAuthException:
                    pass
                except Exception, e:
                    log.error("unable to decode content, %s" % e)
                except:
                    pass
                else:
                    if seq != self.seq:
                        log.warn("sequence number error, {} expected, but {} received".format(self.seq, seq))
                    else:
                        self.result_queue.put_nowait((status, content))
                        self.state = STATE_IDLE
            else:
                log.error('got unknown QueueItem')

    def Recv(self, timeout = None):
        """Receive data from server (may block)"""
        try:
            item = self.result_queue.get(True, timeout)
            return item
        except:
            return (None, None)

    def DiscardCurrentResult(self):