Class: Fenris::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/fenris/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, options = {}) ⇒ Connection

Returns a new instance of Connection.



17
18
19
20
21
22
23
24
25
26
# File 'lib/fenris/connection.rb', line 17

def initialize(client = nil, options = {})
  @client = client
  ## TODO -- peer_to_be vs @peer is a poor name choice - refactor
  ## sometimes I call the proxying connection on the local box 'peer', sometimes I call the other fenris process 'peer' - confusing
  @binding      = options[:binding]
  @peer_to_be   = options[:peer]
  @peer_name    = options[:peer_name]
  @peer_binding = options[:peer_binding]
  @peer = []
end

Class Method Details

.mkbinding(action, binding) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fenris/connection.rb', line 5

def self.mkbinding(action, binding)
  if binding =~ /^:?(\d+)$/
    [ action, "0.0.0.0", $1.to_i ]
  elsif binding =~ /^(.+):(\d+)/
    [ action, $1, $2.to_i ]
  elsif binding == "--"
    [ :attach, $stdin ]
  else
    [ action, binding ]
  end
end

Instance Method Details

#log(msg) ⇒ Object



28
29
30
# File 'lib/fenris/connection.rb', line 28

def log msg
  @client.log msg if @client
end

#proxy(peer, leader = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/fenris/connection.rb', line 54

def proxy(peer, leader = true)
  peer.close if @unbound
  @peer.each { |d| peer.push d}
  @peer = peer
  @peer_to_be = nil
  if leader
    @peer.proxy(self, false)
    EventMachine::enable_proxy(self, @peer)
  end
end

#proxy_target_unboundObject



65
66
67
# File 'lib/fenris/connection.rb', line 65

def proxy_target_unbound
  unbind
end

#push(data) ⇒ Object



32
33
34
35
# File 'lib/fenris/connection.rb', line 32

def push(data)
  # this is the black magic to switch @peer between a connection and an array
  send_data data
end

#receive_data(data) ⇒ Object



69
70
71
# File 'lib/fenris/connection.rb', line 69

def receive_data data
  @peer.push data
end

#ssl_handshake_completedObject



50
51
52
# File 'lib/fenris/connection.rb', line 50

def ssl_handshake_completed
  @post_ssl.call
end

#ssl_verify_peer(pem) ⇒ Object



37
38
39
# File 'lib/fenris/connection.rb', line 37

def ssl_verify_peer(pem)
  @verify ||= @client.validate_peer pem, @peer_to_be, @peer_name
end

#unbindObject



41
42
43
44
45
46
47
48
# File 'lib/fenris/connection.rb', line 41

def unbind
  log "Connection closed"
  @unbound = true
  ## TODO clean up buffer vs peer vs peer_to_be
  (@peer_to_be || @peer).close_connection_after_writing rescue nil
  close_connection
  EventMachine::stop if @binding[0] == :attach
end