Class: Vines::Stream::Client::Session
- Inherits:
-
Object
- Object
- Vines::Stream::Client::Session
- Includes:
- Comparable
- Defined in:
- lib/vines/stream/client/session.rb
Overview
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).
Direct Known Subclasses
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_broadcast_presence ⇒ Object
Returns the value of attribute last_broadcast_presence.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #<=>(session) ⇒ Object
- #advance(state) ⇒ Object
-
#authenticated? ⇒ Boolean
Returns true if this client has properly authenticated with the server.
-
#available! ⇒ Object
Notify the session that the client has sent an initial presence broadcast and is now considered to be an “available” resource.
-
#available? ⇒ Boolean
An available resource has sent initial presence and can receive presence subscription requests.
-
#available_subscribed_to_resources ⇒ Object
Returns streams for available resources to which this user has successfully subscribed.
-
#available_subscribers ⇒ Object
Returns streams for available resources that are subscribed to this user’s presence updates.
-
#bind!(resource) ⇒ Object
Complete resource binding with the given resource name, provided by the client or generated by the server.
-
#connected? ⇒ Boolean
A connected resource has authenticated and bound a resource identifier.
- #hash ⇒ Object
-
#initialize(stream) ⇒ Session
constructor
A new instance of Session.
-
#interested? ⇒ Boolean
An interested resource has requested its roster and can receive roster pushes.
- #ready? ⇒ Boolean
-
#remote_subscribed_to_contacts ⇒ Object
Returns contacts hosted at remote servers to which this user has successfully subscribed.
-
#remote_subscribers(to = nil) ⇒ Object
Returns contacts hosted at remote servers that are subscribed to this user’s presence updates.
-
#requested_roster! ⇒ Object
Notify the session that the client has requested its roster and is now considered to be an “interested” resource.
- #stream_type ⇒ Object
-
#unbind!(stream) ⇒ Object
Called by the stream when it’s disconnected from the client.
- #write(data) ⇒ Object
Constructor Details
#initialize(stream) ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 22 23 24 25 26 27 |
# 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 |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
13 14 15 |
# File 'lib/vines/stream/client/session.rb', line 13 def domain @domain end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/vines/stream/client/session.rb', line 14 def id @id end |
#last_broadcast_presence ⇒ Object
Returns the value of attribute last_broadcast_presence.
14 15 16 |
# File 'lib/vines/stream/client/session.rb', line 14 def last_broadcast_presence @last_broadcast_presence end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
14 15 16 |
# File 'lib/vines/stream/client/session.rb', line 14 def state @state end |
#user ⇒ Object
Returns the value of attribute user.
13 14 15 |
# File 'lib/vines/stream/client/session.rb', line 13 def user @user end |
Instance Method Details
#<=>(session) ⇒ Object
29 30 31 |
# File 'lib/vines/stream/client/session.rb', line 29 def <=>(session) session.is_a?(Session) ? self.id <=> session.id : nil end |
#advance(state) ⇒ Object
39 40 41 |
# File 'lib/vines/stream/client/session.rb', line 39 def advance(state) @state = state end |
#authenticated? ⇒ Boolean
Returns true if this client has properly authenticated with the server.
45 46 47 |
# File 'lib/vines/stream/client/session.rb', line 45 def authenticated? !@user.nil? end |
#available! ⇒ Object
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.
52 53 54 55 |
# File 'lib/vines/stream/client/session.rb', line 52 def available! @available = true save_to_cluster end |
#available? ⇒ Boolean
An available resource has sent initial presence and can receive presence subscription requests.
59 60 61 |
# File 'lib/vines/stream/client/session.rb', line 59 def available? @available && connected? end |
#available_subscribed_to_resources ⇒ Object
Returns streams for available resources to which this user has successfully subscribed.
123 124 125 126 |
# File 'lib/vines/stream/client/session.rb', line 123 def available_subscribed_to_resources subscribed = @user.subscribed_to_contacts.map {|c| c.jid } router.available_resources(subscribed, @user.jid) end |
#available_subscribers ⇒ Object
Returns streams for available resources that are subscribed to this user’s presence updates.
130 131 132 133 |
# File 'lib/vines/stream/client/session.rb', line 130 def available_subscribers subscribed = @user.subscribed_from_contacts.map {|c| c.jid } router.available_resources(subscribed, @user.jid) end |
#bind!(resource) ⇒ Object
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.
66 67 68 69 70 |
# File 'lib/vines/stream/client/session.rb', line 66 def bind!(resource) @user.jid.resource = resource router << self save_to_cluster end |
#connected? ⇒ Boolean
A connected resource has authenticated and bound a resource identifier.
74 75 76 |
# File 'lib/vines/stream/client/session.rb', line 74 def connected? !@unbound && authenticated? && !@user.jid. end |
#hash ⇒ Object
35 36 37 |
# File 'lib/vines/stream/client/session.rb', line 35 def hash @id.hash end |
#interested? ⇒ Boolean
An interested resource has requested its roster and can receive roster pushes.
80 81 82 |
# File 'lib/vines/stream/client/session.rb', line 80 def interested? @requested_roster && connected? end |
#ready? ⇒ Boolean
89 90 91 |
# File 'lib/vines/stream/client/session.rb', line 89 def ready? @state.class == Client::Ready end |
#remote_subscribed_to_contacts ⇒ Object
Returns contacts hosted at remote servers to which this user has successfully subscribed.
137 138 139 140 141 |
# File 'lib/vines/stream/client/session.rb', line 137 def remote_subscribed_to_contacts @user.subscribed_to_contacts.reject do |c| @config.local_jid?(c.jid) end end |
#remote_subscribers(to = nil) ⇒ Object
Returns contacts hosted at remote servers that are subscribed to this user’s presence updates.
145 146 147 148 149 150 |
# File 'lib/vines/stream/client/session.rb', line 145 def remote_subscribers(to=nil) jid = (to.nil? || to.empty?) ? nil : JID.new(to). @user.subscribed_from_contacts.reject do |c| @config.local_jid?(c.jid) || (jid && c.jid. != jid) end end |
#requested_roster! ⇒ Object
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 contacts.
96 97 98 99 |
# File 'lib/vines/stream/client/session.rb', line 96 def requested_roster! @requested_roster = true save_to_cluster end |
#stream_type ⇒ Object
101 102 103 |
# File 'lib/vines/stream/client/session.rb', line 101 def stream_type :client end |
#unbind!(stream) ⇒ Object
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).
112 113 114 115 116 117 118 119 |
# 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 |
#write(data) ⇒ Object
105 106 107 |
# File 'lib/vines/stream/client/session.rb', line 105 def write(data) @stream.write(data) end |