Author: kclark
Date: Tue Jun 17 17:50:44 2008
New Revision: 668894

URL: http://svn.apache.org/viewvc?rev=668894&view=rev
Log:
add simple Ruby HTTP server and client classes

Using Mongrel for the server.

Added:
    incubator/thrift/trunk/lib/rb/lib/thrift/server/thttpserver.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/thttpclient.rb
Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/ttransport.rb

Added: incubator/thrift/trunk/lib/rb/lib/thrift/server/thttpserver.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/server/thttpserver.rb?rev=668894&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/server/thttpserver.rb (added)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/server/thttpserver.rb Tue Jun 17 
17:50:44 2008
@@ -0,0 +1,43 @@
+#!/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.getProtocol 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

Added: incubator/thrift/trunk/lib/rb/lib/thrift/transport/thttpclient.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/thttpclient.rb?rev=668894&view=auto
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/thttpclient.rb (added)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/thttpclient.rb Tue Jun 
17 17:50:44 2008
@@ -0,0 +1,25 @@
+#!/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 isOpen; 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

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/ttransport.rb
URL: 
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/ttransport.rb?rev=668894&r1=668893&r2=668894&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/ttransport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/ttransport.rb Tue Jun 17 
17:50:44 2008
@@ -269,3 +269,18 @@
   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 isOpen(); true end
+  def read(sz); @input.read(sz) end
+  def write(buf); @output.write(buf) end
+end


Reply via email to