From: Mark Slee Date: Wed, 7 Feb 2007 06:05:28 +0000 (+0000) Subject: Implement TSimpleServer in Ruby X-Git-Tag: 0.2.0~1480 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=e1165d4b1837726dba8c144719cbde6f47389478;p=common%2Fthrift.git Implement TSimpleServer in Ruby Summary: It Works! Reviewed By: tbr-doug git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664992 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/protocol/tprotocol.rb b/lib/rb/lib/thrift/protocol/tprotocol.rb index e3d87874..56868a21 100644 --- a/lib/rb/lib/thrift/protocol/tprotocol.rb +++ b/lib/rb/lib/thrift/protocol/tprotocol.rb @@ -159,6 +159,6 @@ class TProtocol end class TProtocolFactory - def getProtocol(trans); nil end + def getProtocol(trans); nil; end end diff --git a/lib/rb/lib/thrift/server/tserver.rb b/lib/rb/lib/thrift/server/tserver.rb new file mode 100644 index 00000000..c68b70f7 --- /dev/null +++ b/lib/rb/lib/thrift/server/tserver.rb @@ -0,0 +1,43 @@ +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, procotolFactory=nil) + super(processor, serverTransport, transportFactory, procotolFactory) + end + + def serve() + @serverTransport.listen() + while (true) + client = @serverTransport.accept() + trans = @transportFactory.getTransport(client) + prot = @protocolFactory.getProtocol(trans) + begin + while (true) + @processor.process(prot, prot) + end + rescue TTransportException => ttx + print ttx,"\n" + end + trans.close() + end + end + +end + + diff --git a/lib/rb/lib/thrift/transport/tsocket.rb b/lib/rb/lib/thrift/transport/tsocket.rb index 232f01eb..2f128b87 100644 --- a/lib/rb/lib/thrift/transport/tsocket.rb +++ b/lib/rb/lib/thrift/transport/tsocket.rb @@ -2,18 +2,22 @@ require 'thrift/transport/ttransport' require 'socket' class TSocket < TTransport - def initialize(host, port) + def initialize(host='localhost', port=9090) @host = host @port = port @handle = nil end + def setHandle(handle) + @handle = handle + end + def open() @handle = TCPSocket.new(@host, @port) end def isOpen() - return @handle != nil + return !@handle.nil? end def write(str) @@ -21,11 +25,16 @@ class TSocket < TTransport end def read(sz) - return @handle.recv(sz) + data = @handle.recv(sz) + if (data.length == 0) + raise TTransportException.new("TSocket: Could not read #{sz} bytes from #{@host}:#{@port}") + end + return data end def close() @handle.close() unless @handle.nil? + @handle = nil end end @@ -42,7 +51,10 @@ class TServerSocket < TServerTransport def accept() if (@handle != nil) - return @handle.accept() + sock = @handle.accept() + trans = TSocket.new() + trans.setHandle(sock) + return trans end return nil end diff --git a/lib/rb/lib/thrift/transport/ttransport.rb b/lib/rb/lib/thrift/transport/ttransport.rb index e0369d16..e8c578db 100644 --- a/lib/rb/lib/thrift/transport/ttransport.rb +++ b/lib/rb/lib/thrift/transport/ttransport.rb @@ -1,3 +1,9 @@ +class TTransportException < StandardError + def initialize(message) + super(message) + end +end + class TTransport def isOpen(); nil; end