Skip to content

Commit

Permalink
Replace DOS line endings with Unix line endings.
Browse files Browse the repository at this point in the history
  • Loading branch information
imbriaco committed Sep 19, 2009
1 parent ccfc706 commit 6d6d6ba
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 152 deletions.
16 changes: 8 additions & 8 deletions 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
86 changes: 43 additions & 43 deletions 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
164 changes: 82 additions & 82 deletions 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
38 changes: 19 additions & 19 deletions 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

0 comments on commit 6d6d6ba

Please sign in to comment.