Class: Juggernaut::Client
- Inherits:
-
Object
- Object
- Juggernaut::Client
show all
- Includes:
- Miscel
- Defined in:
- lib/juggernaut/client.rb
Constant Summary
collapse
- @@clients =
[]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Miscel
#config_path, #log_path, #logger, #options, #options=, #pid_path
Constructor Details
#initialize(subscriber, request) ⇒ Client
Returns a new instance of Client.
74
75
76
77
78
79
|
# File 'lib/juggernaut/client.rb', line 74
def initialize(subscriber, request)
@connections = []
@id = request[:client_id]
@session_id = request[:session_id]
add_new_connection(subscriber)
end
|
Instance Attribute Details
#connections ⇒ Object
Returns the value of attribute connections.
8
9
10
|
# File 'lib/juggernaut/client.rb', line 8
def connections
@connections
end
|
#id ⇒ Object
Returns the value of attribute id.
6
7
8
|
# File 'lib/juggernaut/client.rb', line 6
def id
@id
end
|
#session_id ⇒ Object
Returns the value of attribute session_id.
7
8
9
|
# File 'lib/juggernaut/client.rb', line 7
def session_id
@session_id
end
|
Class Method Details
.add_client(client) ⇒ Object
23
24
25
|
# File 'lib/juggernaut/client.rb', line 23
def add_client(client)
@@clients << client unless @@clients.include?(client)
end
|
.find(&block) ⇒ Object
33
34
35
|
# File 'lib/juggernaut/client.rb', line 33
def find(&block)
@@clients.select(&block).uniq
end
|
.find_all ⇒ Object
29
30
31
|
# File 'lib/juggernaut/client.rb', line 29
def find_all
@@clients
end
|
.find_by_channels(channels) ⇒ Object
48
49
50
51
52
|
# File 'lib/juggernaut/client.rb', line 48
def find_by_channels(channels)
find {|client|
client.has_channels?(channels)
}
end
|
.find_by_id(id) ⇒ Object
37
38
39
|
# File 'lib/juggernaut/client.rb', line 37
def find_by_id(id)
find {|client| client.id == id }.first
end
|
.find_by_id_and_channels(id, channels) ⇒ Object
54
55
56
57
58
|
# File 'lib/juggernaut/client.rb', line 54
def find_by_id_and_channels(id, channels)
find {|client|
client.has_channels?(channels) && client.id == id
}.first
end
|
.find_by_signature(signature) ⇒ Object
41
42
43
44
45
46
|
# File 'lib/juggernaut/client.rb', line 41
def find_by_signature(signature)
find {|client|
client.connections.select {|connection| connection.signature == signature }.any?
}.first
end
|
.find_or_create(subscriber, request) ⇒ Object
Actually does a find_or_create_by_id
13
14
15
16
17
18
19
20
21
|
# File 'lib/juggernaut/client.rb', line 13
def find_or_create(subscriber, request)
if client = find_by_id(request[:client_id])
client.session_id = request[:session_id]
client.add_new_connection(subscriber)
client
else
self.new(subscriber, request)
end
end
|
.send_logouts_after_timeout ⇒ Object
60
61
62
63
64
|
# File 'lib/juggernaut/client.rb', line 60
def send_logouts_after_timeout
@@clients.each do |client|
client.logout_request if !client.alive? and client.give_up? and !client.sent_logout?
end
end
|
.send_logouts_to_all_clients ⇒ Object
Called when the server is shutting down
67
68
69
70
71
|
# File 'lib/juggernaut/client.rb', line 67
def send_logouts_to_all_clients
@@clients.each do |client|
client.logout_request if !client.sent_logout?
end
end
|
Instance Method Details
#add_new_connection(subscriber) ⇒ Object
88
89
90
|
# File 'lib/juggernaut/client.rb', line 88
def add_new_connection(subscriber)
@connections << subscriber
end
|
#alive? ⇒ Boolean
131
132
133
|
# File 'lib/juggernaut/client.rb', line 131
def alive?
@connections.select{|em| em.alive? }.any?
end
|
#give_up? ⇒ Boolean
135
136
137
|
# File 'lib/juggernaut/client.rb', line 135
def give_up?
@connections.select {|em| em.logout_timeout and Time.now > em.logout_timeout }.any?
end
|
#has_channels?(channels) ⇒ Boolean
118
119
120
121
122
123
|
# File 'lib/juggernaut/client.rb', line 118
def has_channels?(channels)
@connections.each do |em|
return true if em.has_channels?(channels)
end
false
end
|
#logout_connection_request(channels) ⇒ Object
97
98
99
100
|
# File 'lib/juggernaut/client.rb', line 97
def logout_connection_request(channels)
return true unless options[:logout_connection_url]
post_request(options[:logout_connection_url], channels)
end
|
#logout_request ⇒ Object
102
103
104
105
106
|
# File 'lib/juggernaut/client.rb', line 102
def logout_request
return true unless options[:logout_url]
@sent_logout = true
post_request(options[:logout_url])
end
|
#remove_channels!(channels) ⇒ Object
125
126
127
128
129
|
# File 'lib/juggernaut/client.rb', line 125
def remove_channels!(channels)
@connections.each do |em|
em.remove_channels!(channels)
end
end
|
#send_message(msg, channels = nil) ⇒ Object
112
113
114
115
116
|
# File 'lib/juggernaut/client.rb', line 112
def send_message(msg, channels = nil)
@connections.each do |em|
em.broadcast(msg) if !channels or channels.empty? or em.has_channels?(channels)
end
end
|
#sent_logout? ⇒ Boolean
108
109
110
|
# File 'lib/juggernaut/client.rb', line 108
def sent_logout?
!!@sent_logout
end
|
#subscription_request(channels) ⇒ Object
92
93
94
95
|
# File 'lib/juggernaut/client.rb', line 92
def subscription_request(channels)
return true unless options[:subscription_url]
post_request(options[:subscription_url], channels)
end
|
#to_json ⇒ Object
81
82
83
84
85
86
|
# File 'lib/juggernaut/client.rb', line 81
def to_json
{
:id => @id,
:session_id => @session_id
}.to_json
end
|