Skip to content

Instantly share code, notes, and snippets.

@baburdick
Created December 10, 2010 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baburdick/735653 to your computer and use it in GitHub Desktop.
Save baburdick/735653 to your computer and use it in GitHub Desktop.
# Write a ASIR::Transport::HTTP class
# Using HTTP::Client for transport send_request and receive_response.
# Using WEBrick for transport on the receive_request and send_response.
# Use the Marshal Coder for the Transport.
require 'rubygems'
require 'webrick'
gem 'httpclient'
require 'httpclient'
require 'uri'
$: << File.expand_path("../../../lib", __FILE__)
require 'asir'
require 'math_service'
module ASIR
class Transport
class HTTP < self
attr_accessor :uri, :server
# Client-side: HTTPClient
def _send_request request
#$stderr.puts "request.inspect: #{request.inspect}"
client = ::HTTPClient.new
client.put uri, request
end
# Should extract the content from the HTTPClient::Message
def _receive_response opaque
$stderr.puts "opaque.inspect: #{opaque.inspect}"
HTTPClient::Message.content opaque
end
# Server-side: WEBrick
# Extract the body from the request.
def _receive_request rq
#request = WEBrick::HTTPRequest.new rq
$stderr.puts "rq.inspect: #{rq.inspect}"
request.body
end
# Set the Content-Type and body of the response.
def _send_response result, rs
# ???
end
# Parse the port and path of the #uri
# Create a @server = WEBrick::HTTPServer on the port
# Mount the path with a proc that calls serve_request! with the HTTP request and response objects.
def setup_server!
@server = WEBrick::HTTPServer.new :Port => 8902
@server.mount_proc "/" do |req,resp|
serve_request! req, resp
end
self
end
# Start the WEBbrick @server
def start_server!
@server.start
self
end
end
end
end
port = 3001
begin
t = ASIR::Transport::HTTP.new
t._log_enabled = true
t.logger = $stderr
c = t.encoder = ASIR::Coder::Marshal.new
c._log_enabled = true
c.logger = $stderr
# Setup and run the server in a child process.
server_pid = Process.fork do
t.setup_server!
t.start_server!
end
# system("curl http://localhost:#{port}/")
MathService.client.transport = t
MathService.client.sum([1, 2, 3])
rescue Exception => err
puts err
puts err.backtrace.join "\n"
ensure
# Kill the server.
Process.kill(9, server_pid)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment