From 6d6d6bad525cfad62e9d6f08ef9300a495dc6ec0 Mon Sep 17 00:00:00 2001 From: Mark Imbriaco Date: Sat, 19 Sep 2009 19:15:25 -0400 Subject: [PATCH] Replace DOS line endings with Unix line endings. --- lib/em-proxy.rb | 16 ++-- lib/em-proxy/backend.rb | 86 +++++++++---------- lib/em-proxy/connection.rb | 164 ++++++++++++++++++------------------- lib/em-proxy/proxy.rb | 38 ++++----- 4 files changed, 152 insertions(+), 152 deletions(-) diff --git a/lib/em-proxy.rb b/lib/em-proxy.rb index d40016e..ad13c1f 100644 --- a/lib/em-proxy.rb +++ b/lib/em-proxy.rb @@ -1,8 +1,8 @@ -$:.unshift(File.dirname(__FILE__) + '/../lib') - -require "rubygems" -require "eventmachine" - -%w[ backend proxy connection ].each do |file| - require "em-proxy/#{file}" -end +$:.unshift(File.dirname(__FILE__) + '/../lib') + +require "rubygems" +require "eventmachine" + +%w[ backend proxy connection ].each do |file| + require "em-proxy/#{file}" +end diff --git a/lib/em-proxy/backend.rb b/lib/em-proxy/backend.rb index b97e673..79beb41 100644 --- a/lib/em-proxy/backend.rb +++ b/lib/em-proxy/backend.rb @@ -1,43 +1,43 @@ -module EventMachine - module ProxyServer - class Backend < EventMachine::Connection - attr_accessor :plexer, :data, :name, :debug - - def initialize(debug = false) - @debug = debug - @connected = EM::DefaultDeferrable.new - @data = [] - end - - def connection_completed - debug [@name, :conn_complete] - @connected.succeed - end - - def receive_data(data) - debug [@name, data] - @data.push data - @plexer.relay_from_backend(@name, data) - end - - # Buffer data until the connection to the backend server - # is established and is ready for use - def send(data) - @connected.callback { send_data data } - end - - # Notify upstream plexer that the backend server is done - # processing the request - def unbind - debug [@name, :unbind] - @plexer.unbind_backend(@name) - end - - private - - def debug(*data) - p data if @debug - end - end - end -end +module EventMachine + module ProxyServer + class Backend < EventMachine::Connection + attr_accessor :plexer, :data, :name, :debug + + def initialize(debug = false) + @debug = debug + @connected = EM::DefaultDeferrable.new + @data = [] + end + + def connection_completed + debug [@name, :conn_complete] + @connected.succeed + end + + def receive_data(data) + debug [@name, data] + @data.push data + @plexer.relay_from_backend(@name, data) + end + + # Buffer data until the connection to the backend server + # is established and is ready for use + def send(data) + @connected.callback { send_data data } + end + + # Notify upstream plexer that the backend server is done + # processing the request + def unbind + debug [@name, :unbind] + @plexer.unbind_backend(@name) + end + + private + + def debug(*data) + p data if @debug + end + end + end +end diff --git a/lib/em-proxy/connection.rb b/lib/em-proxy/connection.rb index d1e91eb..18c7a78 100644 --- a/lib/em-proxy/connection.rb +++ b/lib/em-proxy/connection.rb @@ -1,82 +1,82 @@ -module EventMachine - module ProxyServer - class Connection < EventMachine::Connection - attr_accessor :debug - - ##### Proxy Methods - def on_data(&blk); @on_data = blk; end - def on_response(&blk); @on_response = blk; end - def on_finish(&blk); @on_finish = blk; end - - ##### EventMachine - def initialize(options) - @debug = options[:debug] || false - @servers = {} - end - - def receive_data(data) - debug [:connection, data] - processed = @on_data.call(data) - - if processed.is_a? Array - data, servers = *processed - - # guard for "unbound" servers - servers = servers.collect {|s| @servers[s]}.compact - else - data = processed - servers ||= @servers.values.compact - end - - servers.each do |s| - s.send_data data unless data.nil? - end - end - - # - # initialize connections to backend servers - # - def server(name, opts) - srv = EventMachine::connect(opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c| - c.name = name - c.plexer = self - end - - @servers[name] = srv - end - - # - # relay data from backend server to client - # - def relay_from_backend(name, data) - debug [:relay_from_backend, name, data] - - data = @on_response.call(name, data) - send_data data unless data.nil? - end - - def unbind - # terminate any unfinished connections - @servers.values.compact.each do |s| - s.close_connection_after_writing - end - - close_connection_after_writing - @on_finish.call(:done) if @servers.values.compact.size.zero? if @on_finish - end - - def unbind_backend(name) - debug [:unbind_backend, name] - @servers[name] = nil - @on_finish.call(name) if @on_finish - close_connection_after_writing if @servers.values.compact.size.zero? - end - - private - - def debug(*data) - p data if @debug - end - end - end -end +module EventMachine + module ProxyServer + class Connection < EventMachine::Connection + attr_accessor :debug + + ##### Proxy Methods + def on_data(&blk); @on_data = blk; end + def on_response(&blk); @on_response = blk; end + def on_finish(&blk); @on_finish = blk; end + + ##### EventMachine + def initialize(options) + @debug = options[:debug] || false + @servers = {} + end + + def receive_data(data) + debug [:connection, data] + processed = @on_data.call(data) + + if processed.is_a? Array + data, servers = *processed + + # guard for "unbound" servers + servers = servers.collect {|s| @servers[s]}.compact + else + data = processed + servers ||= @servers.values.compact + end + + servers.each do |s| + s.send_data data unless data.nil? + end + end + + # + # initialize connections to backend servers + # + def server(name, opts) + srv = EventMachine::connect(opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c| + c.name = name + c.plexer = self + end + + @servers[name] = srv + end + + # + # relay data from backend server to client + # + def relay_from_backend(name, data) + debug [:relay_from_backend, name, data] + + data = @on_response.call(name, data) + send_data data unless data.nil? + end + + def unbind + # terminate any unfinished connections + @servers.values.compact.each do |s| + s.close_connection_after_writing + end + + close_connection_after_writing + @on_finish.call(:done) if @servers.values.compact.size.zero? if @on_finish + end + + def unbind_backend(name) + debug [:unbind_backend, name] + @servers[name] = nil + @on_finish.call(name) if @on_finish + close_connection_after_writing if @servers.values.compact.size.zero? + end + + private + + def debug(*data) + p data if @debug + end + end + end +end diff --git a/lib/em-proxy/proxy.rb b/lib/em-proxy/proxy.rb index dbbc44d..8905db7 100644 --- a/lib/em-proxy/proxy.rb +++ b/lib/em-proxy/proxy.rb @@ -1,19 +1,19 @@ -class Proxy - def self.start(options, &blk) - EM.epoll - EM.run do - trap("TERM") { stop } - trap("INT") { stop } - - EventMachine::start_server(options[:host], options[:port], - EventMachine::ProxyServer::Connection, options) do |c| - c.instance_eval(&blk) - end - end - end - - def self.stop - puts "Terminating ProxyServer" - EventMachine.stop - end -end +class Proxy + def self.start(options, &blk) + EM.epoll + EM.run do + trap("TERM") { stop } + trap("INT") { stop } + + EventMachine::start_server(options[:host], options[:port], + EventMachine::ProxyServer::Connection, options) do |c| + c.instance_eval(&blk) + end + end + end + + def self.stop + puts "Terminating ProxyServer" + EventMachine.stop + end +end