fizx-proxymachine 1.7.1 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,7 +13,7 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'fizx-proxymachine'
16
- s.version = '1.7.1'
16
+ s.version = '1.8.0'
17
17
  s.date = '2011-05-07'
18
18
  s.rubyforge_project = 'fizx-proxymachine'
19
19
 
@@ -48,10 +48,8 @@ Gem::Specification.new do |s|
48
48
 
49
49
  ## List your development dependencies here. Development dependencies are
50
50
  ## those that are only needed during development
51
- # s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
51
+ s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
52
52
  s.add_development_dependency(%q<shoulda>, ["~> 2.11.3"])
53
- # s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
54
- # s.add_development_dependency("ruby-debug")
55
53
 
56
54
  ## Leave this section as-is. It will be automatically generated from the
57
55
  ## contents of your Git repository via the gemspec task. DO NOT REMOVE
@@ -28,7 +28,8 @@ class ProxyMachine
28
28
  @@counter
29
29
  end
30
30
 
31
- def self.incr
31
+ def self.incr(client = nil)
32
+ @@incr_callback && @@incr_callback.call(client)
32
33
  @@totalcounter += 1
33
34
  @@counter += 1
34
35
  @@maxcounter = @@counter if @@counter > @@maxcounter
@@ -36,7 +37,8 @@ class ProxyMachine
36
37
  @@counter
37
38
  end
38
39
 
39
- def self.decr
40
+ def self.decr(client = nil)
41
+ @@decr_callback &&@@decr_callback.call(client)
40
42
  @@counter -= 1
41
43
  if $server.nil?
42
44
  $logger.info "Waiting for #{@@counter} connections to finish."
@@ -86,15 +88,31 @@ class ProxyMachine
86
88
  def self.set_inactivity_error_callback(&block)
87
89
  @@inactivity_error_callback = block
88
90
  end
89
-
91
+
90
92
  def self.set_inactivity_warning_callback(&block)
91
93
  @@inactivity_warning_callback = block
92
94
  end
93
95
 
96
+ def self.set_incr_callback(&block)
97
+ @@incr_callback = block
98
+ end
99
+
100
+ def self.set_decr_callback(&block)
101
+ @@decr_callback = block
102
+ end
103
+
104
+ def self.incr_callback(&block)
105
+ @@incr_callback
106
+ end
107
+
108
+ def self.decr_callback(&block)
109
+ @@decr_callback
110
+ end
111
+
94
112
  def self.inactivity_error_callback
95
113
  @@inactivity_error_callback
96
114
  end
97
-
115
+
98
116
  def self.inactivity_warning_callback
99
117
  @@inactivity_warning_callback
100
118
  end
@@ -108,6 +126,9 @@ class ProxyMachine
108
126
  @@connect_error_callback ||= proc { |remote| }
109
127
  @@inactivity_error_callback ||= proc { |remote| }
110
128
  @@inactivity_warning_callback ||= proc { |remote| }
129
+ @@incr_callback ||= proc { |remote| }
130
+ @@decr_callback ||= proc { |remote| }
131
+
111
132
  self.update_procline
112
133
  EM.epoll
113
134
 
@@ -138,9 +159,9 @@ module Kernel
138
159
  def proxy_inactivity_error(&block)
139
160
  ProxyMachine.set_inactivity_error_callback(&block)
140
161
  end
141
-
162
+
142
163
  def proxy_inactivity_warning(&block)
143
164
  ProxyMachine.set_inactivity_warning_callback(&block)
144
165
  end
145
-
166
+
146
167
  end
@@ -15,7 +15,7 @@ class ProxyMachine
15
15
  @connected = false
16
16
  @connect_timeout = nil
17
17
  @inactivity_timeout = nil
18
- ProxyMachine.incr
18
+ ProxyMachine.incr(self)
19
19
  end
20
20
 
21
21
  def peer
@@ -39,14 +39,14 @@ class ProxyMachine
39
39
  # Called when new data is available from the client but no remote
40
40
  # server has been established. If a remote can be established, an
41
41
  # attempt is made to connect and proxy to the remote server.
42
- def establish_remote_server
42
+ def establish_remote_server(routes = nil)
43
43
  fail "establish_remote_server called with remote established" if @remote
44
- @routes = [ProxyMachine.router.call(@buffer.join)].flatten
44
+ @routes = [routes || ProxyMachine.router.call(@buffer.join, self) || {}].flatten
45
45
  try_connect
46
46
  end
47
-
47
+
48
48
  attr_reader :inactivity_warning_timeout, :inactivity_warning_callback
49
-
49
+
50
50
  def try_connect
51
51
  @commands = @routes.shift
52
52
  $logger.info "#{peer} #{@commands.inspect}"
@@ -104,7 +104,7 @@ class ProxyMachine
104
104
  @buffer = []
105
105
  proxy_incoming_to(@server_side, 10240)
106
106
  end
107
-
107
+
108
108
  def inactivity_warning_triggered
109
109
  proc {
110
110
  (@inactivity_warning_callback || ProxyMachine.inactivity_warning_callback).call(@remote.join(':'))
@@ -145,7 +145,7 @@ class ProxyMachine
145
145
 
146
146
  def unbind
147
147
  @server_side.close_connection_after_writing if @server_side
148
- ProxyMachine.decr
148
+ ProxyMachine.decr(self)
149
149
  end
150
150
 
151
151
  # Proxy connection has been lost
@@ -4,7 +4,21 @@ callback = proc do |data|
4
4
  data + ":callback"
5
5
  end
6
6
 
7
- proxy do |data|
7
+ class TestServerConnection < EventMachine::Connection
8
+ def self.request(host, port, callback)
9
+ EventMachine.connect(host, port, self, callback)
10
+ end
11
+
12
+ def initialize(callback)
13
+ @callback = callback
14
+ end
15
+
16
+ def receive_data(data)
17
+ @callback.call(data)
18
+ end
19
+ end
20
+
21
+ proxy do |data, conn|
8
22
  if data == 'a'
9
23
  { :remote => "localhost:9980" }
10
24
  elsif data == 'b'
@@ -25,6 +39,12 @@ proxy do |data|
25
39
  { :remote => "localhost:9989" }
26
40
  elsif data == 'inactivity'
27
41
  { :remote => "localhost:9980", :data => 'sleep 3', :inactivity_timeout => 1, :inactivity_warning_timeout => 0.5 }
42
+ elsif data == 'delayed'
43
+ sc = TestServerConnection.request("localhost", 9981, proc{
44
+ conn.establish_remote_server(:close => "ohai")
45
+ })
46
+ sc.send_data "delayed"
47
+ { :noop => true }
28
48
  else
29
49
  { :close => true }
30
50
  end
@@ -41,6 +41,10 @@ class ProxymachineTest < Test::Unit::TestCase
41
41
  assert_proxy('localhost', 9990, 'g', 'g3-9980:g2')
42
42
  end
43
43
 
44
+ should "handle delayed routes" do
45
+ assert_proxy('localhost', 9990, 'delayed', 'ohai')
46
+ end
47
+
44
48
  should "handle noop" do
45
49
  sock = TCPSocket.new('localhost', 9990)
46
50
  sock.write('e' * 2048)
@@ -53,7 +57,7 @@ class ProxymachineTest < Test::Unit::TestCase
53
57
  should "execute a callback" do
54
58
  assert_proxy('localhost', 9990, 'h', '9980:h:callback')
55
59
  end
56
-
60
+
57
61
  should "call proxy_connect_error when a connection is rejected" do
58
62
  sock = TCPSocket.new('localhost', 9990)
59
63
  sock.write('connect reject')
@@ -73,7 +77,7 @@ class ProxymachineTest < Test::Unit::TestCase
73
77
  assert_equal "activity error: localhost:9980", File.read(@proxy_error_file)
74
78
  sock.close
75
79
  end
76
-
80
+
77
81
  should "call proxy_inactivity_error when initial read times out" do
78
82
  sock = TCPSocket.new('localhost', 9990)
79
83
  sent = Time.now
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: fizx-proxymachine
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.7.1
5
+ version: 1.8.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tom Preston-Werner
@@ -25,16 +25,27 @@ dependencies:
25
25
  type: :runtime
26
26
  version_requirements: *id001
27
27
  - !ruby/object:Gem::Dependency
28
- name: shoulda
28
+ name: rake
29
29
  prerelease: false
30
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
31
  none: false
32
32
  requirements:
33
33
  - - ~>
34
34
  - !ruby/object:Gem::Version
35
- version: 2.11.3
35
+ version: 0.8.7
36
36
  type: :development
37
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: shoulda
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.11.3
47
+ type: :development
48
+ version_requirements: *id003
38
49
  description: ProxyMachine is a simple content aware (layer 7) TCP routing proxy written in Ruby with EventMachine.
39
50
  email: tom@mojombo.com
40
51
  executables:
@@ -87,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
98
  requirements: []
88
99
 
89
100
  rubyforge_project: fizx-proxymachine
90
- rubygems_version: 1.8.10
101
+ rubygems_version: 1.8.15
91
102
  signing_key:
92
103
  specification_version: 2
93
104
  summary: ProxyMachine is a simple content aware (layer 7) TCP routing proxy.