Class: BinProxy::Session

Inherits:
Object
  • Object
show all
Includes:
Observable
Defined in:
lib/binproxy/session.rb

Overview

This class represents a pair of TCP connections (client <-> proxy and proxy <-> server), through which a number of messages may be sent.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, client, server, parser_class) ⇒ Session

Returns a new instance of Session.



8
9
10
11
12
13
14
15
16
17
# File 'lib/binproxy/session.rb', line 8

def initialize(id, client, server, parser_class)
  @open_time = Time.now
  @id = id
  @endpoints = { client: client, server: server }
  p = parser_class.new
  @endpoints.each_pair do |peer, conn|
    conn.parser = p
    conn.add_observer(self, :send)
  end
end

Instance Attribute Details

#close_timeObject (readonly)

Returns the value of attribute close_time.



6
7
8
# File 'lib/binproxy/session.rb', line 6

def close_time
  @close_time
end

#endpointsObject (readonly)

Returns the value of attribute endpoints.



6
7
8
# File 'lib/binproxy/session.rb', line 6

def endpoints
  @endpoints
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/binproxy/session.rb', line 6

def id
  @id
end

#open_timeObject (readonly)

Returns the value of attribute open_time.



6
7
8
# File 'lib/binproxy/session.rb', line 6

def open_time
  @open_time
end

Instance Method Details

#connection_completed(conn) ⇒ Object



30
31
32
33
34
35
# File 'lib/binproxy/session.rb', line 30

def connection_completed(conn)
  # this is only called for the upstream connection, as the downstream connection is already completed
  # by the time that the session is created
  log.warn 'unexpected connection_completed on downstream' if conn.peer != :server
  endpoints[:client].upstream_connected(conn)
end

#connection_lost(peer, reason) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/binproxy/session.rb', line 37

def connection_lost(peer, reason)
  @close_time = Time.now
  # XXX Shutdown the endpoints
  #  - but not until we've finished passing through* any existing messages
  #  (this needs to be handled up a level at the proxy)
  #  * or dropping??
  changed
  notify_observers(:session_closed, self, peer, reason)
end

#message_received(pm) ⇒ Object

should receive a ProxyMessage from Connection



20
21
22
23
24
# File 'lib/binproxy/session.rb', line 20

def message_received(pm)
  pm.session = self
  changed
  notify_observers(:message_received, pm)
end

#send_message(message) ⇒ Object



26
27
28
# File 'lib/binproxy/session.rb', line 26

def send_message(message)
  endpoints[message.dest].send_message(message)
end