class Vines::Stream::Client::Session
A Session
tracks the state of a client stream over its lifetime from negotiation to processing stanzas to shutdown. By disconnecting the stream's state from the stream, we can allow multiple TCP connections to access one logical session (e.g. HTTP streams).
Attributes
Public Class Methods
# File lib/vines/stream/client/session.rb, line 16 def initialize(stream) @stream = stream @id = Kit.uuid @config = stream.config @state = Client::Start.new(stream) @available = false @domain = nil @last_broadcast_presence = nil @requested_roster = false @unbound = false @user = nil end
Public Instance Methods
# File lib/vines/stream/client/session.rb, line 29 def <=>(session) session.is_a?(Session) ? self.id <=> session.id : nil end
# File lib/vines/stream/client/session.rb, line 39 def advance(state) @state = state end
Returns true if this client has properly authenticated with the server.
# File lib/vines/stream/client/session.rb, line 45 def authenticated? !@user.nil? end
Notify the session that the client has sent an initial presence broadcast and is now considered to be an “available” resource. Available resources are sent presence subscription stanzas.
# File lib/vines/stream/client/session.rb, line 52 def available! @available = true save_to_cluster end
An available resource has sent initial presence and can receive presence subscription requests.
# File lib/vines/stream/client/session.rb, line 59 def available? @available && connected? end
Returns streams for available resources to which this user has successfully subscribed.
# File lib/vines/stream/client/session.rb, line 123 def available_subscribed_to_resources subscribed = @user.subscribed_to_followers.map {|c| c.jid } router.available_resources(subscribed, @user.jid) end
Returns streams for available resources that are subscribed to this user's presence updates.
# File lib/vines/stream/client/session.rb, line 130 def available_subscribers subscribed = @user.subscribed_from_followers.map {|c| c.jid } router.available_resources(subscribed, @user.jid) end
Complete resource binding with the given resource name, provided by the client or generated by the server. Once resource binding is completed, the stream is considered to be “connected” and ready for traffic.
# File lib/vines/stream/client/session.rb, line 66 def bind!(resource) @user.jid.resource = resource router << self save_to_cluster end
A connected resource has authenticated and bound a resource identifier.
# File lib/vines/stream/client/session.rb, line 74 def connected? !@unbound && authenticated? && !@user.jid.bare? end
# File lib/vines/stream/client/session.rb, line 35 def hash @id.hash end
An interested resource has requested its roster and can receive roster pushes.
# File lib/vines/stream/client/session.rb, line 80 def interested? @requested_roster && connected? end
# File lib/vines/stream/client/session.rb, line 84 def last_broadcast_presence=(node) @last_broadcast_presence = node save_to_cluster end
# File lib/vines/stream/client/session.rb, line 89 def ready? @state.class == Client::Ready end
Returns followers hosted at remote servers to which this user has successfully subscribed.
# File lib/vines/stream/client/session.rb, line 137 def remote_subscribed_to_followers @user.subscribed_to_followers.reject do |c| @config.local_jid?(c.jid) end end
Returns followers hosted at remote servers that are subscribed to this user's presence updates.
# File lib/vines/stream/client/session.rb, line 145 def remote_subscribers(to=nil) jid = (to.nil? || to.empty?) ? nil : JID.new(to).bare @user.subscribed_from_followers.reject do |c| @config.local_jid?(c.jid) || (jid && c.jid.bare != jid) end end
Notify the session that the client has requested its roster and is now considered to be an “interested” resource. Interested resources are sent roster pushes when changes are made to their followers.
# File lib/vines/stream/client/session.rb, line 96 def requested_roster! @requested_roster = true save_to_cluster end
# File lib/vines/stream/client/session.rb, line 101 def stream_type :client end
Called by the stream when it's disconnected from the client. The stream passes itself to this method in case multiple streams are accessing this session (e.g. BOSH/HTTP).
# File lib/vines/stream/client/session.rb, line 112 def unbind!(stream) router.delete(self) delete_from_cluster unsubscribe_pubsub @unbound = true @available = false broadcast_unavailable end
# File lib/vines/stream/client/session.rb, line 105 def write(data) @stream.write(data) end
Private Instance Methods
# File lib/vines/stream/client/session.rb, line 174 def broadcast(stanza, recipients) recipients.each do |recipient| stanza['to'] = recipient.user.jid.to_s recipient.write(stanza) end end
# File lib/vines/stream/client/session.rb, line 191 def delete_from_cluster if connected? && @config.cluster? @config.cluster.delete_session(@user.jid) end end
# File lib/vines/stream/client/session.rb, line 181 def router @config.router end
# File lib/vines/stream/client/session.rb, line 185 def save_to_cluster if @config.cluster? @config.cluster.save_session(@user.jid, to_hash) end end
# File lib/vines/stream/client/session.rb, line 203 def to_hash presence = @last_broadcast_presence ? @last_broadcast_presence.to_s : nil {available: @available, interested: @requested_roster, presence: presence.to_s} end
# File lib/vines/stream/client/session.rb, line 197 def unsubscribe_pubsub if connected? @config.vhost(@user.jid.domain).unsubscribe_pubsub(@user.jid) end end