Class: Faye::Engine::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Logging, Publisher
Defined in:
lib/faye/engines/proxy.rb

Constant Summary

Constants included from Logging

Logging::LOG_LEVELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publisher

#unbind

Constructor Details

#initialize(options) ⇒ Proxy

Returns a new instance of Proxy.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/faye/engines/proxy.rb', line 39

def initialize(options)
  super()

  @options     = options
  @connections = {}
  @interval    = @options[:interval] || INTERVAL
  @timeout     = @options[:timeout]  || TIMEOUT

  engine_class = @options[:type] || Memory
  @engine      = engine_class.create(self, @options)

  bind :close do |client_id|
    EventMachine.next_tick { flush_connection(client_id) }
  end

  debug('Created new engine: ?', @options)
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



34
35
36
# File 'lib/faye/engines/proxy.rb', line 34

def interval
  @interval
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



34
35
36
# File 'lib/faye/engines/proxy.rb', line 34

def timeout
  @timeout
end

Instance Method Details

#closeObject



110
111
112
113
# File 'lib/faye/engines/proxy.rb', line 110

def close
  @connections.keys.each { |client_id| flush_connection(client_id) }
  @engine.disconnect
end

#close_connection(client_id) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/faye/engines/proxy.rb', line 77

def close_connection(client_id)
  debug('Closing connection for ?', client_id)
  return unless conn = @connections[client_id]
  conn.socket.close if conn.socket
  trigger('connection:close', client_id)
  @connections.delete(client_id)
end

#connect(client_id, options = {}, &callback) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/faye/engines/proxy.rb', line 57

def connect(client_id, options = {}, &callback)
  debug('Accepting connection from ?', client_id)
  @engine.ping(client_id)
  conn = connection(client_id, true)
  conn.connect(options, &callback)
  @engine.empty_queue(client_id)
end

#connection(client_id, create) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/faye/engines/proxy.rb', line 69

def connection(client_id, create)
  conn = @connections[client_id]
  return conn if conn or not create
  @connections[client_id] = Connection.new(self, client_id)
  trigger('connection:open', client_id)
  @connections[client_id]
end

#deliver(client_id, messages) ⇒ Object



90
91
92
93
94
95
# File 'lib/faye/engines/proxy.rb', line 90

def deliver(client_id, messages)
  return if !messages || messages.empty?
  return false unless conn = connection(client_id, false)
  messages.each(&conn.method(:deliver))
  true
end

#disconnectObject



115
116
117
# File 'lib/faye/engines/proxy.rb', line 115

def disconnect
  @engine.disconnect if @engine.respond_to?(:disconnect)
end

#flush_connection(client_id, close = true) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/faye/engines/proxy.rb', line 101

def flush_connection(client_id, close = true)
  return unless client_id
  debug('Flushing connection for ?', client_id)
  return unless conn = connection(client_id, false)
  conn.socket = nil unless close
  conn.flush
  close_connection(client_id)
end

#generate_idObject



97
98
99
# File 'lib/faye/engines/proxy.rb', line 97

def generate_id
  Engine.random
end

#has_connection?(client_id) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/faye/engines/proxy.rb', line 65

def has_connection?(client_id)
  @connections.has_key?(client_id)
end

#open_socket(client_id, socket) ⇒ Object



85
86
87
88
# File 'lib/faye/engines/proxy.rb', line 85

def open_socket(client_id, socket)
  conn = connection(client_id, true)
  conn.socket = socket
end

#publish(message) ⇒ Object



119
120
121
122
# File 'lib/faye/engines/proxy.rb', line 119

def publish(message)
  channels = Channel.expand(message['channel'])
  @engine.publish(message, channels)
end