Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
# encoding: ascii-8bit
2
# 
3
# Licensed to the Apache Software Foundation (ASF) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The ASF licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
# 
11
#   http://www.apache.org/licenses/LICENSE-2.0
12
# 
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
#
20
 
21
module Thrift
22
  class BufferedTransport < BaseTransport
23
    DEFAULT_BUFFER = 4096
24
 
25
    def initialize(transport)
26
      @transport = transport
27
      @wbuf = ''
28
      @rbuf = ''
29
      @index = 0
30
    end
31
 
32
    def open?
33
      return @transport.open?
34
    end
35
 
36
    def open
37
      @transport.open
38
    end
39
 
40
    def close
41
      flush
42
      @transport.close
43
    end
44
 
45
    def read(sz)
46
      @index += sz
47
      ret = @rbuf.slice(@index - sz, sz) || ''
48
 
49
      if ret.length == 0
50
        @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
51
        @index = sz
52
        ret = @rbuf.slice(0, sz) || ''
53
      end
54
 
55
      ret
56
    end
57
 
58
    def write(buf)
59
      @wbuf << buf
60
    end
61
 
62
    def flush
63
      if @wbuf != ''
64
        @transport.write(@wbuf)
65
        @wbuf = ''
66
      end
67
 
68
      @transport.flush
69
    end
70
  end
71
 
72
  class BufferedTransportFactory < BaseTransportFactory
73
    def get_transport(transport)
74
      return BufferedTransport.new(transport)
75
    end
76
  end
77
end