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::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary collapse

Attributes included from Logging

#log_level

Instance Method Summary collapse

Methods included from Logging

#log

Methods included from Publisher

#bind, #count_listeners, #trigger, #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
# File 'lib/faye/engines/proxy.rb', line 39

def initialize(options)
  @options     = options
  @connections = {}
  @interval    = @options[:interval] || INTERVAL
  @timeout     = @options[:timeout]  || TIMEOUT

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

  bind :disconnect do |client_id|
    EventMachine.next_tick { close_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

#close_connection(client_id) ⇒ Object



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

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

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



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

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



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

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
96
# File 'lib/faye/engines/proxy.rb', line 90

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

#disconnectObject



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

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

#flush(client_id) ⇒ Object



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

def flush(client_id)
  return unless client_id
  debug 'Flushing connection for ?', client_id
  conn = connection(client_id, false)
  conn.flush!(true) if conn
end

#generate_idObject



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

def generate_id
  Engine.random
end

#has_connection?(client_id) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/faye/engines/proxy.rb', line 63

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

#open_socket(client_id, socket) ⇒ Object



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

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

#publish(message) ⇒ Object



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

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