コード例 #1
0
ファイル: rpc.py プロジェクト: hw233/python_3dkserver
    def call(self, func, args=None):
        self.resetTimeout()
        _d = defer.Deferred()

        if self.transport and func and self.deferreds is not None:
            obj = (func, args)
            data = dumps(obj)

            body_length = len(data)
            self.seq = inc_seq(self.seq)
            self.deferreds[self.seq]= _d
 
            _header = struct.pack(self.HEADER_FORMAT_L, TCP_REQ, self.seq, body_length)
            try:
                self.transport.write(_header + data)
                log.debug("[ CALL ]:ar_id:%d, func:%s, body_length:%d, to:%s" % (self.seq, func, body_length, self.transport.getPeer()))
            except:
                self.transport.loseConnection()
                _d.errback(Exception("call failed"))
        else:
            log.warn("[ CALL ]:unknown args client:%s or func:%s or deferreds:%s." % (self.transport.getPeer(), func, self.deferreds))
            _d.errback(Exception("call failed"))

        self.resetTimeout()

        return _d
コード例 #2
0
ファイル: rpc.py プロジェクト: hw233/python_3dkserver
 def ack(self, result, ar_id, state):
     self.resetTimeout()
     if result is None:
         #log.debug('[ ACK ]result is None, nothing need to ack.')
         return
     if isinstance(result, failure.Failure):
         result.printTraceback()
         state = 1, result.getErrorMessage()
         data = dumps((state, None))
         #data = dumps((state, None), ensure_ascii=False)
         ack_data = (state, None) 
     else:
         try:
             ack_data = (state, str(result)[:1000])
             data = dumps((state, result))
             #data = dumps((state, result), ensure_ascii=False)
         except Exception, e:
             traceback.print_exc()
             state = 2, str(e)
             data = dumps((state, None))
             #data = dumps((state, None), ensure_ascii=False)
             ack_data = (state, None) 
コード例 #3
0
def __read_file( path ):
    result = []

    try:
        result = [ line.rstrip('\r\n') for line in file( path ).readlines() ]
    except:
        print_e()

    #print '[ __read_file ]result:', result

    out = StringIO.StringIO()
    f = gzip.GzipFile( fileobj = out, mode = 'w' )
    f.write( dumps( result ) )
    f.close()

    return out.getvalue()
コード例 #4
0
def load_randname():
    global json_randname

    if json_randname: return
    log.info('randname: {0}.'.format( randname ))
    if use_zip:
        import StringIO, gzip

        out = StringIO.StringIO()
        f = gzip.GzipFile(fileobj=out, mode='w')
        f.write(gemsgpack.dumps( randname ))
        f.close()
        json_randname = out.getvalue()
        out.close()
    else:
        json_randname = json.dumps( randname , cls=DecimalEncoder)
コード例 #5
0
ファイル: rpc.py プロジェクト: hw233/python_3dkserver
    def send(self, func, args=None):
        self.resetTimeout()
        if self.transport and func:
            obj = (func, args)
            data = dumps(obj)

            body_length = len(data)
            _header = struct.pack(self.HEADER_FORMAT_L, TCP_REQ, self.seq, body_length)
            try:
                self.transport.write(_header + data)
                log.debug("[ SEND ]:func:%s, body_length:%d, to:%s" % (func, body_length, self.transport.getPeer()))
            except:
                self.transport.loseConnection()
        else:
            log.warn("[ SEND ]:unknown args client:%s or func:%s." % (self.transport.getPeer(), func))

        self.resetTimeout()
コード例 #6
0
def load_keyword():
    global json_keyword

    if json_keyword: return

    if use_zip:
        import StringIO, gzip

        for k, v in all_keyword.iteritems():
            out = StringIO.StringIO()
            f = gzip.GzipFile(fileobj=out, mode='w')
            f.write(gemsgpack.dumps(v))
            #f.write(gemsgpack.dumps(v, cls=DecimalEncoder))
            f.close()
            json_keyword[k] = out.getvalue()
            out.close()
    else:
        for k, v in all_keyword.iteritems():
            json_keyword[k] = json.dumps(v, cls=DecimalEncoder)
コード例 #7
0
def load_sys_config():
    global json_sysconfig

    if json_sysconfig: return

    if use_zip:
        import StringIO, gzip

        out = StringIO.StringIO()
        f = gzip.GzipFile(fileobj=out, mode='w')

        for k, table_value in sysconfig.iteritems():
            f.write(struct.pack('!I%ss' % len(k), len(k), k))
            s = gemsgpack.dumps(table_value)
            f.write(struct.pack('!I%ss' % len(s), len(s), s))

        f.close()
        json_sysconfig = out.getvalue()
        out.close()
    else:
        json_sysconfig = json.dumps(sysconfig, cls=DecimalEncoder)
コード例 #8
0
ファイル: rpc.py プロジェクト: hw233/python_3dkserver
            try:
                ack_data = (state, str(result)[:1000])
                data = dumps((state, result))
                #data = dumps((state, result), ensure_ascii=False)
            except Exception, e:
                traceback.print_exc()
                state = 2, str(e)
                data = dumps((state, None))
                #data = dumps((state, None), ensure_ascii=False)
                ack_data = (state, None) 

        body_length = len(data)
        if body_length >= MAX_BODY_LENGTH:
            log.error("[ ACK ]:ar_id:%d, body_length:%d, body length too huge, to:%s." % (ar_id, body_length, self.transport.getPeer()))
            state = 2, 'msg too hurg. length:{0}.'.format(body_length)
            data = dumps((state, None))
            body_length = len(data)
        else:
            log.debug("[ ACK ]:ar_id:%d, body_length:%d, data:%s, to:%s." % (ar_id, body_length, ack_data, self.transport.getPeer()))

        _header = struct.pack(self.HEADER_FORMAT_L, TCP_ACK, ar_id, body_length)
        self.transport.write(_header + data)

    def connectionLost(self, reason):
        log.debug('[ connectionLost ]lost from:', self.transport.getPeer(), reason.getErrorMessage())
        self.setTimeout(None)

        if hasattr(self.factory, 'onConnectionLost') and self.lose_connect:
            self.factory.onConnectionLost(self)

        self.buff = None