Skip to content

Instantly share code, notes, and snippets.

@baburdick
Created December 10, 2010 01:51
Show Gist options
  • Save baburdick/735639 to your computer and use it in GitHub Desktop.
Save baburdick/735639 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
client = ::HTTPClient.new
client.get request
end
# Should extract the content from the HTTPClient::Message
def _receive_response opaque
HTTPClient::Message.content opaque
end
# Server-side: WEBrick
# Extract the body from the request.
def _receive_request rq
request = WEBrick::HTTPRequest.new rq
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 server_request! with the HTTP request and response objects.
def setup_server!
@server = WEBrick::HTTPServer.new :Port => 8902
@server.mount_proc Dir.pwd do |req,resp|
server_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