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 e3d8787..56868a2 100644
--- a/lib/rb/lib/thrift/protocol/tprotocol.rb
+++ b/lib/rb/lib/thrift/protocol/tprotocol.rb
@@ -159,6 +159,6 @@
 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 0000000..c68b70f
--- /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 232f01e..2f128b8 100644
--- a/lib/rb/lib/thrift/transport/tsocket.rb
+++ b/lib/rb/lib/thrift/transport/tsocket.rb
@@ -2,18 +2,22 @@
 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 @@
   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 @@
 
   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 e0369d1..e8c578d 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