Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
View
3  example/config/asir_config.rb
@@ -37,6 +37,9 @@
when :http, :webrick
transport = ASIR::Transport::Webrick.new
transport.uri = "http://localhost:#{30000 + asir.identifier.to_s.to_i}/asir"
+ when :rack
+ transport = ASIR::Transport::Rack.new
+ transport.uri = "http://localhost:#{30000 + asir.identifier.to_s.to_i}/asir"
when :zmq
transport = ASIR::Transport::Zmq.new
transport.one_way = true
View
35 example/ex24.rb
@@ -0,0 +1,35 @@
+# !SLIDE :capture_code_output true
+# Synchronous HTTP service on Rack under WEBrick
+
+gem 'rack'
+require 'example_helper'
+require 'asir/transport/rack'
+require 'asir/coder/base64'
+begin
+ Email.client.transport = t =
+ ASIR::Transport::Rack.new(:uri => "http://localhost:31924/")
+ t.encoder =
+ ASIR::Coder::Chain.new(:encoders =>
+ [ASIR::Coder::Marshal.new,
+ ASIR::Coder::Base64.new, ])
+ server_process do
+ t.prepare_server!
+ t.run_server!
+ end; sleep 2
+ pr Email.client.send_email(:pdf_invoice,
+ :to => "user@email.com",
+ :customer => @customer)
+ sleep 2
+rescue Object => err
+ $stderr.puts "#{err.inspect}\n#{err.backtrace * "\n"}"
+ensure
+ t.close rescue nil; sleep 3
+ server_kill; sleep 2
+end
+
+# !SLIDE END
+# EXPECT: : client process
+# EXPECT: : server process
+# EXPECT: : Email.send_mail :pdf_invoice
+# EXPECT: : pr: :ok
+
View
4 lib/asir/transport/http.rb
@@ -39,8 +39,8 @@ def _send_message message, message_payload
# Recieve the Result payload String from the opaque
# HTTPClient::Request response object returned from #_send_message.
def _receive_result message, http_result_message
- $stderr.puts " ### http_result_message.content.encoding = #{http_result_message.content.encoding.inspect}" rescue nil
- $stderr.puts " ### http_result_message.content = #{http_result_message.content.inspect}" rescue nil
+ # $stderr.puts " ### http_result_message.content.encoding = #{http_result_message.content.encoding.inspect}" rescue nil
+ # $stderr.puts " ### http_result_message.content = #{http_result_message.content.inspect}" rescue nil
http_result_message.content.to_s
end
View
73 lib/asir/transport/rack.rb
@@ -0,0 +1,73 @@
+require 'asir/transport/http'
+require 'rack'
+
+module ASIR
+ class Transport
+ # !SLIDE
+ # Rack Transport
+ class Rack < HTTP
+ # Receive the Message payload String from the Rack::Request object.
+ # Returns the [ Rack::Request, Rack::Response ] as the message_state.
+ def _receive_message rack_req_res, additional_data
+ body = rack_req_res.first.body.read
+ [ body, rack_req_res ]
+ end
+
+ # Send the Result payload String in the Rack::Response object as application/binary.
+ def _send_result message, result, result_payload, rack_rq_rs, message_state
+ rack_response = rack_rq_rs[1]
+ rack_response[CONTENT_TYPE] = APPLICATION_BINARY
+ rack_response.write result_payload
+ end
+
+ # Constructs a Rackable App from this Transport.
+ def rack_app &blk
+ App.new(self, &blk)
+ end
+
+ # Rack Transport Application.
+ class App
+ def initialize transport = nil, &blk
+ @app = transport
+ instance_eval &blk if blk
+ end
+
+ def call env
+ @app.call(env)
+ end
+ end
+
+ # Rack application handler.
+ def call(env)
+ rq = ::Rack::Request.new(env)
+ rs = ::Rack::Response.new
+ rack_rq_rs = [ rq, rs ]
+ serve_message! rack_rq_rs, rack_rq_rs
+ rs.finish # => [ status, header, rbody ]
+ end
+
+ ###############################
+ # Dummy server.
+
+ def prepare_server! opts = { }
+ self
+ end
+
+ # WEBrick under Rack.
+ def run_server!
+ #require 'rack/handler'
+ u = URI.parse(uri); port = u.port # <= REFACTOR
+ ::Rack::Handler::WEBrick.run \
+ ::Rack::ShowExceptions.new(::Rack::Lint.new(self.rack_app)),
+ :Port => port
+ self
+ end
+
+ def stop_server!
+ # NOT IMPLEMENTED
+ self
+ end
+ end
+ # !SLIDE END
+ end
+end
View
5 lib/asir/transport/webrick.rb
@@ -12,18 +12,15 @@ class Webrick < HTTP
# Receive the Message payload String from the HTTP Message object.
# Returns the original http_message as the message_state.
def _receive_message http_message, additional_data
- $stderr.puts " ### http_message.body.encoding = #{http_message.body.encoding.inspect}" rescue nil
[ http_message.body, http_message ]
end
# Send the Result payload String in the HTTP Response object as application/binary.
def _send_result message, result, result_payload, http_result, message_state
- $stderr.puts " ### result_payload.encoding = #{result_payload.encoding.inspect}" rescue nil
http_result[CONTENT_TYPE] = APPLICATION_BINARY
http_result.body = result_payload
end
- # TODO: rename prepare_webrick_server!
def prepare_server! opts = { }
u = URI.parse(uri)
port = u.port
@@ -39,13 +36,11 @@ def prepare_server! opts = { }
end
def run_server!
- $stderr.puts " ### START webrick pid #{$$}"
@server.start
self
end
def stop_server!
- $stderr.puts " ### STOP webrick pid #{$$}"
@server.stop
self
end
View
2  spec/debug_helper.rb
@@ -5,6 +5,8 @@
else
gem 'json'
case RUBY_VERSION
+ when /^2\./
+ gem 'simplecov'
when /^1\.9/
gem 'ruby-debug19'
gem 'simplecov'

No commit comments for this range

Something went wrong with that request. Please try again.