public
Description: Cross language RPC
Homepage: http://developers.facebook.com/thrift/
Clone URL: git://github.com/kevinclark/thrift.git
kevinclark (author)
Mon Apr 28 16:14:15 -0700 2008
commit  86eb40f9944e7fb330760e17d9517b1164582790
tree    b8f776d99022ec7101289fac21c52eff30c51547
parent  993f8d3036b617d8db99a2bf1faf0709af0980e0
thrift / lib / rb / lib / thrift / transport / ttransport.rb
100644 331 lines (253 sloc) 5.358 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env ruby
#
# Copyright (c) 2006- Facebook
# Distributed under the Thrift Software License
#
# See accompanying file LICENSE or visit the Thrift site at:
# http://developers.facebook.com/thrift/
#
# Author: Mark Slee <mcslee@facebook.com>
#
 
require 'thrift/thrift'
 
class TTransportException < TException
 
  UNKNOWN = 0
  NOT_OPEN = 1
  ALREADY_OPEN = 2
  TIMED_OUT = 3
  END_OF_FILE = 4
 
  attr_reader :type
 
  def initialize(type=UNKNOWN, message=nil)
    super(message)
    @type = type
  end
 
end
 
# TTransport is basically an abstract class, but isn't raising NotImplementedError
# TODO: Think about if this is the right thing - Kevin Clark - 3/27/08
class TTransport
  def isOpen; end
 
  def open; end
 
  def close; end
 
  def read(sz); end
 
  def readAll(sz)
    buff = ''
    have = 0
    while (have < sz)
      chunk = read(sz - have)
      have += chunk.length
      buff << chunk
    end
    return buff
  end
 
  def write(buf); end
 
  def flush; end
 
end
 
class TServerTransport
  def listen(); nil; end
 
  def accept(); nil; end
 
  def close(); nil; end
 
end
 
class TTransportFactory
  def getTransport(trans)
    return trans
  end
end
 
class TBufferedTransport < TTransport
  def initialize(transport)
    @transport = transport
    @wbuf = ''
    @rbuf = ''
  end
 
  def isOpen()
    return @transport.isOpen()
  end
 
  def open()
    @transport.open()
  end
 
  def close()
    @transport.close()
  end
 
  DEFAULT_BUFFER = 4096
 
  def read(sz)
    ret = @rbuf.slice!(0...sz)
    if ret.length == 0
      @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
      @rbuf.slice!(0...sz)
    else
      ret
    end
  end
 
  def write(buf)
    @wbuf << buf
  end
 
  def flush()
    @transport.write(@wbuf)
    @transport.flush()
    @wbuf = ''
  end
  
  def borrow(requested_length = 0)
    return @rbuf if requested_length == 0 and @rbuf.length > 0
    
    if @rbuf.length < DEFAULT_BUFFER
      @rbuf << @transport.read([requested_length, DEFAULT_BUFFER].max)
    end
    
    if @rbuf.length < requested_length
      @rbuf << readAll(requested_length - @rbuf.length)
    end
 
    @rbuf
  end
  
  def consume!(size)
    @rbuf.slice!(0...size)
  end
end
 
class TBufferedTransportFactory < TTransportFactory
  def getTransport(transport)
    return TBufferedTransport.new(transport)
  end
end
 
 
class TFramedTransport < TTransport
  def initialize(transport, read=true, write=true)
    @transport = transport
    @rbuf = ''
    @wbuf = ''
    @read = read
    @write = write
  end
 
  def isOpen()
    return @transport.isOpen
  end
 
  def open()
    @transport.open
  end
 
  def close()
    @transport.close
  end
 
  def read(sz)
    if !@read
      return @transport.read(sz)
    end
 
    if (sz <= 0)
      return ''
    end
 
    if (@rbuf.length == 0)
      readFrame
    end
 
    # return full buf
    if (sz > @rbuf.length)
      out = @rbuf
      @rbuf = ''
      return out
    end
 
    # Return substr
    out = @rbuf.slice(0, sz)
    @rbuf = @rbuf.slice(sz, @rbuf.length)
    return out
 
  end
 
  def write(buf,sz=nil)
 
    if !@write
      return @transport.write(buf);
    end
 
    if (sz != nil && sz < buf.length)
      buf = buf.slice(0,sz)
    end
 
    @wbuf << buf
 
  end
 
  #
  # Writes the output buffer to the stream in the format of a 4-byte length
  # followed by the actual data.
  #
  def flush
    if !@write
      return @transport.flush
    end
 
    out = [@wbuf.length].pack('N')
    out << @wbuf
    @transport.write(out)
    @transport.flush
    @wbuf = ''
  end
 
  private
 
  def readFrame
    buf = @transport.readAll(4)
    val = buf.unpack('N')
    sz = val[0]
 
    @rbuf = @transport.readAll(sz)
  end
 
end
 
 
class TFramedTransportFactory < TTransportFactory
  def getTransport(transport)
    return TFramedTransport.new(transport)
  end
end
 
class TMemoryBuffer < TTransport
  def initialize(sz=1024)
    @buf = ''
    @sz = sz
    @wpos = 0
    @rpos = 0
  end
 
  def isOpen
    return 1
  end
 
  def open
  end
 
  def close
  end
 
  def peek
    return @rpos < @wpos
  end
 
  def getBuffer
    return @buf
  end
 
  def resetBuffer(new_buf = '')
     @buf = new_buf
     @sz = new_buf.length
     @wpos = new_buf.length
     @rpos = 0
  end
 
  def available
    return @wpos - @rpos
  end
 
  def read(len)
    avail = available()
 
    return '' if avail == 0
 
    #how much to give
    give = len
    give = avail if avail < len
 
    ret = @buf.slice(@rpos,give)
 
    @rpos += give;
 
    return ret;
  end
 
  def write(wbuf)
    @buf << wbuf
    @wpos += wbuf.length()
  end
 
  def flush
  end
  
  # For fast binary protocol access
  def borrow(size = nil)
    if size.nil?
      @buf[@rpos..-1]
    else
      if size > @buf.length - @rpos
        raise EOFError # Memory buffers only get one shot.
      else
        @buf[@rpos..(@rpos + size)]
      end
    end
  end
  
  def consume!(size)
    @rpos += size
  end
end
 
## Very very simple implementation of wrapping two objects, one with a #read
## method and one with a #write method, into a transport for thrift.
##
## Assumes both objects are open, remain open, don't require flushing, etc.
class TIOStreamTransport < TTransport
  def initialize(input, output)
    @input = input
    @output = output
  end
 
  def isOpen; true end
  def read(sz); @input.read(sz) end
  def write(buf); @output.write(buf) end
end