This repository has been archived by the owner on Jul 10, 2018. It is now read-only.
forked from maccman/juggernaut_gem
/
client.rb
174 lines (148 loc) · 4.34 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
require 'timeout'
require 'net/http'
require 'uri'
module Juggernaut
class Client
include Juggernaut::Miscel
attr_reader :id
attr_accessor :session_id
attr_reader :connections
@@clients = []
class << self
# Actually does a find_or_create_by_id
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
def add_client(client)
@@clients << client unless @@clients.include?(client)
end
# Client find methods
def find_all
@@clients
end
def find(&block)
@@clients.select(&block).uniq
end
def find_by_id(id)
find {|client| client.id == id }.first
end
def find_by_signature(signature)
# signature should be unique
find {|client|
client.connections.select {|connection| connection.signature == signature }.any?
}.first
end
def find_by_channels(channels)
find {|client|
client.has_channels?(channels)
}
end
def find_by_id_and_channels(id, channels)
find {|client|
client.has_channels?(channels) && client.id == id
}.first
end
def send_logouts_after_timeout
@@clients.each do |client|
if !client.alive? and client.give_up?
client.logout_request
@@clients.delete(client)
end
end
end
# Called when the server is shutting down
def send_logouts_to_all_clients
@@clients.each do |client|
client.logout_request
end
end
end
def initialize(subscriber, request)
@connections = []
@id = request[:client_id]
@session_id = request[:session_id]
add_new_connection(subscriber)
end
def to_json
{
:client_id => @id,
:session_id => @session_id
}.to_json
end
def add_new_connection(subscriber)
@connections << subscriber
end
def subscription_request(channels)
return true unless options[:subscription_url]
post_request(options[:subscription_url], channels)
end
def logout_connection_request(channels)
return true unless options[:logout_connection_url]
post_request(options[:logout_connection_url], channels)
end
def logout_request
return true unless options[:logout_url]
post_request(options[:logout_url])
end
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
def has_channels?(channels)
@connections.each do |em|
return true if em.has_channels?(channels)
end
false
end
def remove_channels!(channels)
@connections.each do |em|
em.remove_channels!(channels)
end
end
def alive?
@connections.select{|em| em.alive? }.any?
end
def give_up?
@connections.select {|em|
em.logout_timeout and Time.now > em.logout_timeout
}.any?
end
private
def post_request(url, channels = [])
uri = URI.parse(url)
uri.path = '/' if uri.path == ''
params = []
params << "client_id=#{id}" if id
params << "session_id=#{session_id}" if session_id
channels.each {|chan| params << "channels[]=#{chan}" }
headers = {"User-Agent" => "Ruby/#{RUBY_VERSION}"}
begin
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == 'https'
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.read_timeout = 5
resp, data = http.post(uri.path, params.join('&'), headers)
unless resp.is_a?(Net::HTTPOK)
logger.debug("RESPONSE: #{resp.code}")
return false
end
rescue => e
logger.debug("Bad request #{url.to_s}: #{e}")
return false
rescue Timeout::Error
logger.debug("#{url.to_s} timeout")
return false
end
true
end
end
end