From 74df0bf429a29d85a2f252600f28ca652487953b Mon Sep 17 00:00:00 2001 From: Kevin Clark Date: Wed, 18 Jun 2008 01:00:33 +0000 Subject: [PATCH] Finish renaming/deprecating classes. Change a few more filenames, like thrift/transport/ttransport -> thrift/transport git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@668924 13f79535-47bb-0310-9956-ffa450edef68 --- lib/rb/lib/thrift.rb | 8 +- lib/rb/lib/thrift/protocol.rb | 9 +- lib/rb/lib/thrift/protocol/binaryprotocol.rb | 10 +- lib/rb/lib/thrift/protocol/tbinaryprotocol.rb | 1 + lib/rb/lib/thrift/protocol/tprotocol.rb | 1 + lib/rb/lib/thrift/server.rb | 141 +++++++++ lib/rb/lib/thrift/server/httpserver.rb | 44 +++ lib/rb/lib/thrift/server/thttpserver.rb | 45 +-- lib/rb/lib/thrift/server/tserver.rb | 141 +-------- lib/rb/lib/thrift/transport.rb | 290 +++++++++++++++++ lib/rb/lib/thrift/transport/httpclient.rb | 26 ++ lib/rb/lib/thrift/transport/socket.rb | 90 ++++++ lib/rb/lib/thrift/transport/thttpclient.rb | 27 +- lib/rb/lib/thrift/transport/tsocket.rb | 90 +----- lib/rb/lib/thrift/transport/ttransport.rb | 291 +----------------- lib/rb/lib/thrift/types.rb | 2 +- 16 files changed, 618 insertions(+), 598 deletions(-) create mode 100644 lib/rb/lib/thrift/server.rb create mode 100644 lib/rb/lib/thrift/server/httpserver.rb create mode 100644 lib/rb/lib/thrift/transport.rb create mode 100644 lib/rb/lib/thrift/transport/httpclient.rb create mode 100644 lib/rb/lib/thrift/transport/socket.rb diff --git a/lib/rb/lib/thrift.rb b/lib/rb/lib/thrift.rb index 9c9c4b26..2e747d49 100644 --- a/lib/rb/lib/thrift.rb +++ b/lib/rb/lib/thrift.rb @@ -21,7 +21,7 @@ require 'thrift/types' require 'thrift/processor' require 'thrift/client' require 'thrift/struct' -require 'thrift/protocol/tprotocol' -require 'thrift/transport/tsocket' -require 'thrift/transport/ttransport' -require 'thrift/server/tserver' +require 'thrift/protocol' +require 'thrift/transport/socket' +require 'thrift/transport' +require 'thrift/server' diff --git a/lib/rb/lib/thrift/protocol.rb b/lib/rb/lib/thrift/protocol.rb index e8a6cb7e..23433bec 100644 --- a/lib/rb/lib/thrift/protocol.rb +++ b/lib/rb/lib/thrift/protocol.rb @@ -255,9 +255,10 @@ module Thrift end deprecate_class! :TProtocol => Protocol -end -class TProtocolFactory - def get_protocol(trans); nil; end - deprecate! :getProtocol => :get_protocol + class ProtocolFactory + def get_protocol(trans); nil; end + deprecate! :getProtocol => :get_protocol + end + deprecate_class! :TProtocolFactory => ProtocolFactory end diff --git a/lib/rb/lib/thrift/protocol/binaryprotocol.rb b/lib/rb/lib/thrift/protocol/binaryprotocol.rb index 682ecbf8..b344faf9 100644 --- a/lib/rb/lib/thrift/protocol/binaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/binaryprotocol.rb @@ -181,11 +181,11 @@ module Thrift end deprecate_class! :TBinaryProtocol => BinaryProtocol -end -class TBinaryProtocolFactory < TProtocolFactory - def get_protocol(trans) - return TBinaryProtocol.new(trans) + class BinaryProtocolFactory < ProtocolFactory + def get_protocol(trans) + return TBinaryProtocol.new(trans) + end end + deprecate_class! :TBinaryProtocolFactory => BinaryProtocolFactory end - diff --git a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb index 038e1903..bde3169a 100644 --- a/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tbinaryprotocol.rb @@ -1 +1,2 @@ +require 'thrift/deprecation' require 'thrift/protocol/binaryprotocol' diff --git a/lib/rb/lib/thrift/protocol/tprotocol.rb b/lib/rb/lib/thrift/protocol/tprotocol.rb index 48bf2616..235a34b7 100644 --- a/lib/rb/lib/thrift/protocol/tprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tprotocol.rb @@ -1 +1,2 @@ +require 'thrift/deprecation' require 'thrift/protocol' diff --git a/lib/rb/lib/thrift/server.rb b/lib/rb/lib/thrift/server.rb new file mode 100644 index 00000000..d19a5b24 --- /dev/null +++ b/lib/rb/lib/thrift/server.rb @@ -0,0 +1,141 @@ +# 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 +# +require 'thrift/protocol' +require 'thrift/protocol/binaryprotocol' +require 'thrift/transport' + +module Thrift + class Server + def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil) + @processor = processor + @serverTransport = serverTransport + @transportFactory = transportFactory ? transportFactory : Thrift::TransportFactory.new() + @protocolFactory = protocolFactory ? protocolFactory : Thrift::BinaryProtocolFactory.new() + end + + def serve(); nil; end + end + deprecate_class! :TServer => Server + + class SimpleServer < Server + def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil) + super(processor, serverTransport, transportFactory, protocolFactory) + end + + def serve() + begin + @serverTransport.listen() + while (true) + client = @serverTransport.accept() + trans = @transportFactory.get_transport(client) + prot = @protocolFactory.get_protocol(trans) + begin + while (true) + @processor.process(prot, prot) + end + rescue TTransportException, Thrift::ProtocolException => ttx + #print ttx,"\n" + ensure + trans.close() + end + end + ensure + @serverTransport.close() + end + end + end + deprecate_class! :TSimpleServer => SimpleServer +end + +begin + require 'fastthread' +rescue LoadError + require 'thread' +end + +module Thrift + class ThreadedServer < Server + def serve() + begin + @serverTransport.listen() + while (true) + client = @serverTransport.accept() + trans = @transportFactory.get_transport(client) + prot = @protocolFactory.get_protocol(trans) + Thread.new(prot, trans) do |p, t| + begin + while (true) + @processor.process(p, p) + end + rescue TTransportException, Thrift::ProtocolException => e + ensure + t.close() + end + end + end + ensure + @serverTransport.close() + end + end + end + deprecate_class! :TThreadedServer => ThreadedServer + + class ThreadPoolServer < Server + def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil, num=20) + super(processor, serverTransport, transportFactory, protocolFactory) + @thread_q = SizedQueue.new(num) + @exception_q = Queue.new + @running = false + end + + ## exceptions that happen in worker threads will be relayed here and + ## must be caught. 'retry' can be used to continue. (threads will + ## continue to run while the exception is being handled.) + def rescuable_serve + Thread.new { serve } unless @running + raise @exception_q.pop + end + + ## exceptions that happen in worker threads simply cause that thread + ## to die and another to be spawned in its place. + def serve + @serverTransport.listen() + + begin + while (true) + @thread_q.push(:token) + Thread.new do + begin + while (true) + client = @serverTransport.accept() + trans = @transportFactory.get_transport(client) + prot = @protocolFactory.get_protocol(trans) + begin + while (true) + @processor.process(prot, prot) + end + rescue TTransportException, Thrift::ProtocolException => e + ensure + trans.close() + end + end + rescue Exception => e + @exception_q.push(e) + ensure + @thread_q.pop() # thread died! + end + end + end + ensure + @serverTransport.close() + end + end + end + deprecate_class! :TThreadPoolServer => ThreadPoolServer +end diff --git a/lib/rb/lib/thrift/server/httpserver.rb b/lib/rb/lib/thrift/server/httpserver.rb new file mode 100644 index 00000000..c92e0cad --- /dev/null +++ b/lib/rb/lib/thrift/server/httpserver.rb @@ -0,0 +1,44 @@ +require 'thrift/protocol' +require 'thrift/protocol/binaryprotocol' +require 'thrift/transport' + +require 'mongrel' + +## Sticks a service on a URL, using mongrel to do the HTTP work +module Thrift + class SimpleMongrelHTTPServer + class Handler < Mongrel::HttpHandler + def initialize(processor, protocol_factory) + @processor = processor + @protocol_factory = protocol_factory + end + + def process(request, response) + unless request.params["REQUEST_METHOD"] == "POST" + response.start(404) { } # better way? + return + end + response.start(200) do |head, out| + head["Content-Type"] = "application/x-thrift" + transport = TIOStreamTransport.new request.body, out + protocol = @protocol_factory.get_protocol transport + @processor.process protocol, protocol + end + end + end + + def initialize(processor, opts={}) + port = opts[:port] || 80 + ip = opts[:ip] || "0.0.0.0" + path = opts[:path] || "" + protocol_factory = opts[:protocol_factory] || TBinaryProtocolFactory.new + @server = Mongrel::HttpServer.new ip, port + @server.register "/#{path}", Handler.new(processor, protocol_factory) + end + + def serve + @server.run.join + end + end + deprecate_class! :TSimpleMongrelHTTPServer => SimpleMongrelHTTPServer +end diff --git a/lib/rb/lib/thrift/server/thttpserver.rb b/lib/rb/lib/thrift/server/thttpserver.rb index d6e85a16..0eb7ea60 100644 --- a/lib/rb/lib/thrift/server/thttpserver.rb +++ b/lib/rb/lib/thrift/server/thttpserver.rb @@ -1,43 +1,2 @@ -#!/usr/bin/env ruby - -require 'thrift/protocol/tprotocol' -require 'thrift/protocol/tbinaryprotocol' -require 'thrift/transport/ttransport' - -require 'mongrel' - -## Sticks a service on a URL, using mongrel to do the HTTP work -class TSimpleMongrelHTTPServer - class Handler < Mongrel::HttpHandler - def initialize(processor, protocol_factory) - @processor = processor - @protocol_factory = protocol_factory - end - - def process(request, response) - unless request.params["REQUEST_METHOD"] == "POST" - response.start(404) { } # better way? - return - end - response.start(200) do |head, out| - head["Content-Type"] = "application/x-thrift" - transport = TIOStreamTransport.new request.body, out - protocol = @protocol_factory.get_protocol transport - @processor.process protocol, protocol - end - end - end - - def initialize(processor, opts={}) - port = opts[:port] || 80 - ip = opts[:ip] || "0.0.0.0" - path = opts[:path] || "" - protocol_factory = opts[:protocol_factory] || TBinaryProtocolFactory.new - @server = Mongrel::HttpServer.new ip, port - @server.register "/#{path}", Handler.new(processor, protocol_factory) - end - - def serve - @server.run.join - end -end +require 'thrift/deprecation' +require 'thrift/server/httpserver' diff --git a/lib/rb/lib/thrift/server/tserver.rb b/lib/rb/lib/thrift/server/tserver.rb index 6e3830a2..5903bce9 100644 --- a/lib/rb/lib/thrift/server/tserver.rb +++ b/lib/rb/lib/thrift/server/tserver.rb @@ -1,139 +1,2 @@ -#!/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 -# -require 'thrift/protocol/tprotocol' -require 'thrift/protocol/tbinaryprotocol' -require 'thrift/transport/ttransport' - -class TServer - - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil) - @processor = processor - @serverTransport = serverTransport - @transportFactory = transportFactory ? transportFactory : TTransportFactory.new() - @protocolFactory = protocolFactory ? protocolFactory : TBinaryProtocolFactory.new() - end - - def serve(); nil; end - -end - -class TSimpleServer < TServer - - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil) - super(processor, serverTransport, transportFactory, protocolFactory) - end - - def serve() - begin - @serverTransport.listen() - while (true) - client = @serverTransport.accept() - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - begin - while (true) - @processor.process(prot, prot) - end - rescue TTransportException, Thrift::ProtocolException => ttx - #print ttx,"\n" - ensure - trans.close() - end - end - ensure - @serverTransport.close() - end - end -end - -begin - require 'fastthread' -rescue LoadError - require 'thread' -end - -class TThreadedServer < TServer - def serve() - begin - @serverTransport.listen() - while (true) - client = @serverTransport.accept() - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - Thread.new(prot, trans) do |p, t| - begin - while (true) - @processor.process(p, p) - end - rescue TTransportException, Thrift::ProtocolException => e - ensure - t.close() - end - end - end - ensure - @serverTransport.close() - end - end -end - -class TThreadPoolServer < TServer - def initialize(processor, serverTransport, transportFactory=nil, protocolFactory=nil, num=20) - super(processor, serverTransport, transportFactory, protocolFactory) - @thread_q = SizedQueue.new(num) - @exception_q = Queue.new - @running = false - end - - ## exceptions that happen in worker threads will be relayed here and - ## must be caught. 'retry' can be used to continue. (threads will - ## continue to run while the exception is being handled.) - def rescuable_serve - Thread.new { serve } unless @running - raise @exception_q.pop - end - - ## exceptions that happen in worker threads simply cause that thread - ## to die and another to be spawned in its place. - def serve - @serverTransport.listen() - - begin - while (true) - @thread_q.push(:token) - Thread.new do - begin - while (true) - client = @serverTransport.accept() - trans = @transportFactory.get_transport(client) - prot = @protocolFactory.get_protocol(trans) - begin - while (true) - @processor.process(prot, prot) - end - rescue TTransportException, Thrift::ProtocolException => e - ensure - trans.close() - end - end - rescue Exception => e - @exception_q.push(e) - ensure - @thread_q.pop() # thread died! - end - end - end - ensure - @serverTransport.close() - end - end -end - +require 'thrift/deprecation' +require 'thrift/server' diff --git a/lib/rb/lib/thrift/transport.rb b/lib/rb/lib/thrift/transport.rb new file mode 100644 index 00000000..cc45fac2 --- /dev/null +++ b/lib/rb/lib/thrift/transport.rb @@ -0,0 +1,290 @@ +# 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 +# + +module Thrift + class TransportException < Exception + 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 + deprecate_class! :TTransportException => TransportException + +# Transport 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 Transport + def open?; end + deprecate! :isOpen => :open? + deprecate! :is_open? => :open? + + def open; end + + def close; end + + def read(sz); end + + def read_all(size) + buff = '' + have = 0 + + while (have < size) + chunk = read(size - have) + have += chunk.length + buff << chunk + end + + buff + end + deprecate! :readAll => :read_all + + def write(buf); end + + def flush; end + end + deprecate_class! :TTransport => Transport + + class ServerTransport + def listen(); nil; end + + def accept(); nil; end + + def close(); nil; end + end + deprecate_class! :TServerTransport => ServerTransport + + class TransportFactory + def get_transport(trans) + return trans + end + deprecate! :getTransport => :get_transport + end + deprecate_class! :TTransportFactory => TransportFactory + + class BufferedTransport < Transport + def initialize(transport) + @transport = transport + @wbuf = '' + end + + def open?() + return @transport.open?() + end + + def open() + @transport.open() + end + + def close() + @transport.close() + end + + def read(sz) + return @transport.read(sz) + end + + def write(buf) + @wbuf << buf + end + + def flush() + @transport.write(@wbuf) + @transport.flush() + @wbuf = '' + end + end + deprecate_class! :TBufferedTransport => BufferedTransport + + class BufferedTransportFactory < TransportFactory + def get_transport(transport) + return BufferedTransport.new(transport) + end + end + deprecate_class! :TBufferedTransportFactory => BufferedTransportFactory + + class FramedTransport < Transport + def initialize(transport, read=true, write=true) + @transport = transport + @rbuf = '' + @wbuf = '' + @read = read + @write = write + end + + def open?() + return @transport.open? + 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) + read_frame + 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 read_frame + buf = @transport.read_all(4) + val = buf.unpack('N') + sz = val[0] + + @rbuf = @transport.read_all(sz) + end + end + deprecate_class! :TFramedTransport => FramedTransport + + class FramedTransportFactory < TransportFactory + def get_transport(transport) + return FramedTransport.new(transport) + end + end + deprecate_class! :TFramedTransportFactory => FramedTransportFactory + + class MemoryBuffer < Transport + def initialize(sz=1024) + @buf = '' + @sz = sz + @wpos = 0 + @rpos = 0 + end + + def open? + return 1 + end + + def open + end + + def close + end + + def peek + return @rpos < @wpos + end + + def get_buffer + return @buf + end + + def reset_buffer(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 + end + deprecate_class! :TMemoryBuffer => MemoryBuffer + + ## 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 IOStreamTransport < Transport + def initialize(input, output) + @input = input + @output = output + end + + def open?; true end + def read(sz); @input.read(sz) end + def write(buf); @output.write(buf) end + end + deprecate_class! :TIOStreamTransport => IOStreamTransport +end diff --git a/lib/rb/lib/thrift/transport/httpclient.rb b/lib/rb/lib/thrift/transport/httpclient.rb new file mode 100644 index 00000000..2c4adfcd --- /dev/null +++ b/lib/rb/lib/thrift/transport/httpclient.rb @@ -0,0 +1,26 @@ +require 'thrift/transport' + +require 'net/http' +require 'uri' +require 'stringio' + +## Very simple HTTP client +module Thrift + class HTTPClient < Transport + def initialize(url) + @url = URI url + @outbuf = "" + end + + def open?; true end + def read(sz); @inbuf.read sz end + def write(buf); @outbuf << buf end + def flush + http = Net::HTTP.new @url.host, @url.port + resp, data = http.post(@url.path, @outbuf) + @inbuf = StringIO.new data + @outbuf = "" + end + end + deprecate_class! :THttpClient => HTTPClient +end diff --git a/lib/rb/lib/thrift/transport/socket.rb b/lib/rb/lib/thrift/transport/socket.rb new file mode 100644 index 00000000..762b8d3d --- /dev/null +++ b/lib/rb/lib/thrift/transport/socket.rb @@ -0,0 +1,90 @@ +#!/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 +# +require 'thrift/transport' +require 'socket' + +module Thrift + class Socket < Transport + def initialize(host='localhost', port=9090) + @host = host + @port = port + @handle = nil + end + + def set_handle(handle) + @handle = handle + end + + def open() + begin + @handle = TCPSocket.new(@host, @port) + rescue StandardError + raise TransportException.new(TransportException::NOT_OPEN, "Could not connect to #{@host}:#{@port}") + end + end + + def open?() + return !@handle.nil? + end + + def write(str) + begin + @handle.write(str) + rescue StandardError + raise TransportException.new(TransportException::NOT_OPEN) + end + end + + def read(sz) + begin + data = @handle.recv(sz) + if (data.length == 0) + raise TransportException.new("TSocket: Could not read #{sz} bytes from #{@host}:#{@port}") + end + return data + rescue StandardError + raise TransportException.new(TransportException::NOT_OPEN) + end + end + + def close() + @handle.close() unless @handle.nil? + @handle = nil + end + end + deprecate_class! :TSocket => Socket + + class ServerSocket < ServerTransport + def initialize(port) + @port = port + @handle = nil + end + + def listen() + @handle = TCPserver.new(nil, @port) + end + + def accept() + if (@handle != nil) + sock = @handle.accept() + trans = Socket.new() + trans.set_handle(sock) + return trans + end + return nil + end + + def close() + @handle.close() unless @handle.nil? + end + end + deprecate_class! :TServerSocket => ServerSocket +end diff --git a/lib/rb/lib/thrift/transport/thttpclient.rb b/lib/rb/lib/thrift/transport/thttpclient.rb index 2fe3c574..ffca0cad 100644 --- a/lib/rb/lib/thrift/transport/thttpclient.rb +++ b/lib/rb/lib/thrift/transport/thttpclient.rb @@ -1,25 +1,2 @@ -#!/usr/bin/env ruby - -require 'thrift/transport/ttransport' - -require 'net/http' -require 'uri' -require 'stringio' - -## Very simple HTTP client -class THttpClient < TTransport - def initialize(url) - @url = URI url - @outbuf = "" - end - - def open?; true end - def read(sz); @inbuf.read sz end - def write(buf); @outbuf << buf end - def flush - http = Net::HTTP.new @url.host, @url.port - resp, data = http.post(@url.path, @outbuf) - @inbuf = StringIO.new data - @outbuf = "" - end -end +require 'thrift/deprecation' +require 'thrift/transport/httpclient' diff --git a/lib/rb/lib/thrift/transport/tsocket.rb b/lib/rb/lib/thrift/transport/tsocket.rb index e6ce9a97..fec6edf2 100644 --- a/lib/rb/lib/thrift/transport/tsocket.rb +++ b/lib/rb/lib/thrift/transport/tsocket.rb @@ -1,88 +1,2 @@ -#!/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 -# -require 'thrift/transport/ttransport' -require 'socket' - -class TSocket < TTransport - def initialize(host='localhost', port=9090) - @host = host - @port = port - @handle = nil - end - - def set_handle(handle) - @handle = handle - end - - def open() - begin - @handle = TCPSocket.new(@host, @port) - rescue StandardError - raise TTransportException.new(TTransportException::NOT_OPEN, "Could not connect to #{@host}:#{@port}") - end - end - - def open?() - return !@handle.nil? - end - - def write(str) - begin - @handle.write(str) - rescue StandardError - raise TTransportException.new(TTransportException::NOT_OPEN) - end - end - - def read(sz) - begin - data = @handle.recv(sz) - if (data.length == 0) - raise TTransportException.new("TSocket: Could not read #{sz} bytes from #{@host}:#{@port}") - end - return data - rescue StandardError - raise TTransportException.new(TTransportException::NOT_OPEN) - end - end - - def close() - @handle.close() unless @handle.nil? - @handle = nil - end - -end - -class TServerSocket < TServerTransport - def initialize(port) - @port = port - @handle = nil - end - - def listen() - @handle = TCPserver.new(nil, @port) - end - - def accept() - if (@handle != nil) - sock = @handle.accept() - trans = TSocket.new() - trans.set_handle(sock) - return trans - end - return nil - end - - def close() - @handle.close() unless @handle.nil? - end - -end +require 'thrift/deprecation' +require 'thrift/transport/socket' diff --git a/lib/rb/lib/thrift/transport/ttransport.rb b/lib/rb/lib/thrift/transport/ttransport.rb index 3d475b92..bae60885 100644 --- a/lib/rb/lib/thrift/transport/ttransport.rb +++ b/lib/rb/lib/thrift/transport/ttransport.rb @@ -1,289 +1,2 @@ -#!/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 -# - -class TTransportException < Thrift::Exception - - 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 open?; end - deprecate! :isOpen => :open? - deprecate! :is_open? => :open? - - def open; end - - def close; end - - def read(sz); end - - def read_all(size) - buff = '' - have = 0 - - while (have < size) - chunk = read(size - have) - have += chunk.length - buff << chunk - end - - buff - end - deprecate! :readAll => :read_all - - 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 get_transport(trans) - return trans - end - deprecate! :getTransport => :get_transport -end - -class TBufferedTransport < TTransport - def initialize(transport) - @transport = transport - @wbuf = '' - end - - def open?() - return @transport.open?() - end - - def open() - @transport.open() - end - - def close() - @transport.close() - end - - def read(sz) - return @transport.read(sz) - end - - def write(buf) - @wbuf << buf - end - - def flush() - @transport.write(@wbuf) - @transport.flush() - @wbuf = '' - end -end - -class TBufferedTransportFactory < TTransportFactory - def get_transport(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 open?() - return @transport.open? - 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) - read_frame - 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 read_frame - buf = @transport.read_all(4) - val = buf.unpack('N') - sz = val[0] - - @rbuf = @transport.read_all(sz) - end - -end - - -class TFramedTransportFactory < TTransportFactory - def get_transport(transport) - return TFramedTransport.new(transport) - end -end - -class TMemoryBuffer < TTransport - def initialize(sz=1024) - @buf = '' - @sz = sz - @wpos = 0 - @rpos = 0 - end - - def open? - return 1 - end - - def open - end - - def close - end - - def peek - return @rpos < @wpos - end - - def get_buffer - return @buf - end - - def reset_buffer(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 -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 open?; true end - def read(sz); @input.read(sz) end - def write(buf); @output.write(buf) end -end +require 'thrift/deprecation' +require 'thrift/transport' diff --git a/lib/rb/lib/thrift/types.rb b/lib/rb/lib/thrift/types.rb index b1fbb87c..2831748b 100644 --- a/lib/rb/lib/thrift/types.rb +++ b/lib/rb/lib/thrift/types.rb @@ -22,4 +22,4 @@ module Thrift EXCEPTION = 3 end deprecate_class! :TMessageType => MessageTypes -end \ No newline at end of file +end -- 2.17.1