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
# 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)

  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



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

def close_connection(client_id)
  debug 'Closing connection for ?', client_id
  @connections.delete(client_id)
end

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



51
52
53
54
55
56
57
# File 'lib/faye/engines/proxy.rb', line 51

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



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

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

#deliver(client_id, messages) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/faye/engines/proxy.rb', line 80

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



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

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

#flush(client_id) ⇒ Object



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

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



88
89
90
# File 'lib/faye/engines/proxy.rb', line 88

def generate_id
  Engine.random
end

#has_connection?(client_id) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/faye/engines/proxy.rb', line 59

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

#open_socket(client_id, socket) ⇒ Object



74
75
76
77
78
# File 'lib/faye/engines/proxy.rb', line 74

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

#publish(message) ⇒ Object



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

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