Class: Juggernaut::Client

Inherits:
Object
  • Object
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.



87
88
89
90
91
92
93
# File 'lib/juggernaut/client.rb', line 87

def initialize(subscriber, request)
  @connections = []
  @id         = request[:client_id]
  @session_id = request[:session_id]
  self.register
  add_new_connection(subscriber)
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



13
14
15
# File 'lib/juggernaut/client.rb', line 13

def connections
  @connections
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/juggernaut/client.rb', line 11

def id
  @id
end

#session_idObject

Returns the value of attribute session_id.



12
13
14
# File 'lib/juggernaut/client.rb', line 12

def session_id
  @session_id
end

Class Method Details

.client_registered?(client) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/juggernaut/client.rb', line 78

def client_registered?(client)
  @@clients.include?(client)
end

.find(&block) ⇒ Object



32
33
34
# File 'lib/juggernaut/client.rb', line 32

def find(&block)
  @@clients.select(&block).uniq
end

.find_allObject

Client find methods



28
29
30
# File 'lib/juggernaut/client.rb', line 28

def find_all
  @@clients
end

.find_by_channels(channels) ⇒ Object



47
48
49
50
51
# File 'lib/juggernaut/client.rb', line 47

def find_by_channels(channels)
  find do |client| 
    client.has_channels?(channels)
  end
end

.find_by_id(id) ⇒ Object



36
37
38
# File 'lib/juggernaut/client.rb', line 36

def find_by_id(id)
  find { |client| client.id == id }.first
end

.find_by_id_and_channels(id, channels) ⇒ Object



53
54
55
56
57
# File 'lib/juggernaut/client.rb', line 53

def find_by_id_and_channels(id, channels)
  find do |client| 
    client.has_channels?(channels) && client.id == id
  end.first
end

.find_by_signature(signature) ⇒ Object



40
41
42
43
44
45
# File 'lib/juggernaut/client.rb', line 40

def find_by_signature(signature)
  # signature should be unique
  find do |client| 
    client.connections.select { |connection| connection.signature == signature }.any?
  end.first
end

.find_or_create(subscriber, request) ⇒ Object

Actually does a find_or_create_by_id



17
18
19
20
21
22
23
24
25
# File 'lib/juggernaut/client.rb', line 17

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

.register_client(client) ⇒ Object



74
75
76
# File 'lib/juggernaut/client.rb', line 74

def register_client(client)
  @@clients << client unless @@clients.include?(client)
end

.send_logouts_after_timeoutObject



59
60
61
62
63
64
65
# File 'lib/juggernaut/client.rb', line 59

def send_logouts_after_timeout
  @@clients.each do |client|
    if !client.alive? and client.give_up?
      client.logout_request
    end
  end
end

.send_logouts_to_all_clientsObject

Called when the server is shutting down



68
69
70
71
72
# File 'lib/juggernaut/client.rb', line 68

def send_logouts_to_all_clients
  @@clients.each do |client|
    client.logout_request
  end
end

.unregister_client(client) ⇒ Object



82
83
84
# File 'lib/juggernaut/client.rb', line 82

def unregister_client(client)
  @@clients.delete(client)
end

Instance Method Details

#add_new_connection(subscriber) ⇒ Object



103
104
105
# File 'lib/juggernaut/client.rb', line 103

def add_new_connection(subscriber)
  @connections << subscriber
end

#alive?Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/juggernaut/client.rb', line 155

def alive?
  @connections.select{|em| em.alive? }.any?
end

#friendly_idObject



107
108
109
110
111
112
113
# File 'lib/juggernaut/client.rb', line 107

def friendly_id
  if self.id
    "with ID #{self.id}"
  else
    "session #{self.session_id}"
  end
end

#give_up?Boolean

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/juggernaut/client.rb', line 159

def give_up?
  @connections.select do |em| 
    em.logout_timeout and Time.now > em.logout_timeout 
  end.any?
end

#has_channels?(channels) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
# File 'lib/juggernaut/client.rb', line 142

def has_channels?(channels)
  @connections.each do |em|
    return true if em.has_channels?(channels)
  end
  false
end

#logout_connection_request(channels) ⇒ Object



120
121
122
123
# File 'lib/juggernaut/client.rb', line 120

def logout_connection_request(channels)
  return true unless options[:logout_connection_url]
  post_request(options[:logout_connection_url], channels)
end

#logout_requestObject



125
126
127
128
129
# File 'lib/juggernaut/client.rb', line 125

def logout_request
  self.unregister
  return true unless options[:logout_url]
  post_request(options[:logout_url])
end

#remove_channels!(channels) ⇒ Object



149
150
151
152
153
# File 'lib/juggernaut/client.rb', line 149

def remove_channels!(channels)
  @connections.each do |em|
    em.remove_channels!(channels)
  end
end

#remove_connection(connection) ⇒ Object



131
132
133
134
# File 'lib/juggernaut/client.rb', line 131

def remove_connection(connection)
  @connections.delete(connection)
  self.unregister if @connections.empty?
end

#send_message(msg, channels = nil) ⇒ Object



136
137
138
139
140
# File 'lib/juggernaut/client.rb', line 136

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

#subscription_request(channels) ⇒ Object



115
116
117
118
# File 'lib/juggernaut/client.rb', line 115

def subscription_request(channels)
  return true unless options[:subscription_url]
  post_request(options[:subscription_url], channels)
end

#to_jsonObject



95
96
97
98
99
100
101
# File 'lib/juggernaut/client.rb', line 95

def to_json
  {
    :client_id  => @id, 
    :num_connections => @connections.size,
    :session_id => @session_id
  }.to_json
end