forked from igrigorik/em-proxy
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace DOS line endings with Unix line endings.
- Loading branch information
Showing
4 changed files
with
152 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |