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
# File 'lib/em-proxy/connection.rb', line 13

def initialize(options)
  @debug = options[:debug] || 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



74
75
76
77
# File 'lib/em-proxy/connection.rb', line 74

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



59
60
61
62
# File 'lib/em-proxy/connection.rb', line 59

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

#receive_data(data) ⇒ Object



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

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



67
68
69
70
71
72
# File 'lib/em-proxy/connection.rb', line 67

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/em-proxy/connection.rb', line 26

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/em-proxy/connection.rb', line 45

def server(name, opts)
  srv = EventMachine::connect(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
  self.proxy_incoming_to(srv, 10240) if opts[:relay_client]

  @servers[name] = srv
end

#unbindObject



79
80
81
82
83
84
85
86
# File 'lib/em-proxy/connection.rb', line 79

def unbind
  debug [:unbind, :connection]

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

#unbind_backend(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/em-proxy/connection.rb', line 88

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? and close != :keep
    close_connection_after_writing
  end
end