mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-07 02:45:22 +00:00
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
This commit is contained in:
parent
157d9f794e
commit
e1165d4b18
@ -159,6 +159,6 @@ class TProtocol
|
||||
end
|
||||
|
||||
class TProtocolFactory
|
||||
def getProtocol(trans); nil end
|
||||
def getProtocol(trans); nil; end
|
||||
end
|
||||
|
||||
|
43
lib/rb/lib/thrift/server/tserver.rb
Normal file
43
lib/rb/lib/thrift/server/tserver.rb
Normal file
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -1,3 +1,9 @@
|
||||
class TTransportException < StandardError
|
||||
def initialize(message)
|
||||
super(message)
|
||||
end
|
||||
end
|
||||
|
||||
class TTransport
|
||||
def isOpen(); nil; end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user