Class: EventMachine::ProxyServer::Connection

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-proxy/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

EventMachine



13
14
15
16
17
18
# File 'lib/em-proxy/connection.rb', line 13

def initialize(options)
  @debug = options[:debug] || false
  @tls_key  = options[:tls_key] || false
  @tls_cert = options[:tls_cert] || false
  @servers = {}
end

Instance Attribute Details

#debug=(value) ⇒ Object

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



4
5
6
# File 'lib/em-proxy/connection.rb', line 4

def debug=(value)
  @debug = value
end

Instance Method Details

#connected(name) ⇒ Object



103
104
105
106
# File 'lib/em-proxy/connection.rb', line 103

def connected(name)
  debug [:connected]
  @on_connect.call(name) if @on_connect
end

#on_connect(&blk) ⇒ Object



10
# File 'lib/em-proxy/connection.rb', line 10

def on_connect(&blk); @on_connect = blk; end

#on_data(&blk) ⇒ Object

Proxy Methods



7
# File 'lib/em-proxy/connection.rb', line 7

def on_data(&blk); @on_data = blk; end

#on_finish(&blk) ⇒ Object



9
# File 'lib/em-proxy/connection.rb', line 9

def on_finish(&blk); @on_finish = blk; end

#on_response(&blk) ⇒ Object



8
# File 'lib/em-proxy/connection.rb', line 8

def on_response(&blk); @on_response = blk; end

#peerObject

ip, port

of the connected client



76
77
78
79
80
81
# File 'lib/em-proxy/connection.rb', line 76

def peer
  @peer ||= begin
    peername = get_peername
    peername ? Socket.unpack_sockaddr_in(peername).reverse : nil
  end
end

#post_initObject



20
21
22
23
24
# File 'lib/em-proxy/connection.rb', line 20

def post_init
  if @tls_key and @tls_cert
    start_tls :private_key_file => @tls_key, :cert_chain_file => @tls_cert, :verify_peer => false
  end
end

#receive_data(data) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/em-proxy/connection.rb', line 26

def receive_data(data)
  debug [:connection, data]
  processed = @on_data.call(data) if @on_data

  return if processed == :async or processed.nil?
  relay_to_servers(processed)
end

#relay_from_backend(name, data) ⇒ Object

relay data from backend server to client



96
97
98
99
100
101
# File 'lib/em-proxy/connection.rb', line 96

def relay_from_backend(name, data)
  debug [:relay_from_backend, name, data]

  data = @on_response.call(name, data) if @on_response
  send_data data unless data.nil?
end

#relay_to_servers(processed) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/em-proxy/connection.rb', line 34

def relay_to_servers(processed)
  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

#server(name, opts) ⇒ Object

initialize connections to backend servers



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/em-proxy/connection.rb', line 53

def server(name, opts)
  if opts[:socket]
    srv = EventMachine::connect_unix_domain(opts[:socket], EventMachine::ProxyServer::Backend, @debug) do |c|
      c.name = name
      c.plexer = self
      c.proxy_incoming_to(self, 10240) if opts[:relay_server]
    end
  else
    srv = EventMachine::bind_connect(opts[:bind_host], opts[:bind_port], opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
      c.name = name
      c.plexer = self
      c.proxy_incoming_to(self, 10240) if opts[:relay_server]
    end
  end

  self.proxy_incoming_to(srv, 10240) if opts[:relay_client]

  @servers[name] = srv
end

#sockObject

ip, port

of the local server connect



86
87
88
89
90
91
# File 'lib/em-proxy/connection.rb', line 86

def sock
  @sock ||= begin
    sockname = get_sockname
    sockname ? Socket.unpack_sockaddr_in(sockname).reverse : nil
  end
end

#unbind(reason = nil) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/em-proxy/connection.rb', line 108

def unbind(reason = nil)
  debug [:unbind, :connection, reason]

  # terminate any unfinished connections
  @servers.values.compact.each do |s|
    s.close_connection_after_writing
  end
end

#unbind_backend(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/em-proxy/connection.rb', line 117

def unbind_backend(name)
  debug [:unbind_backend, name]
  @servers[name] = nil
  close = :close

  if @on_finish
    close = @on_finish.call(name)
  end

  # if all connections are terminated downstream, then notify client
  if (@servers.values.compact.size.zero? && close != :keep) || (close == :close)
    close_connection_after_writing
  end
end