Class: Vines::Stream::Client::Session

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

Http::Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Session

Returns a new instance of Session.



16
17
18
19
20
21
22
23
24
25
# File 'lib/vines/stream/client/session.rb', line 16

def initialize(stream)
  @id = Kit.uuid
  @state = Client::Start.new(stream)
  @available = false
  @domain = nil
  @last_broadcast_presence = nil
  @requested_roster = false
  @unbound = false
  @user = nil
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



13
14
15
# File 'lib/vines/stream/client/session.rb', line 13

def domain
  @domain
end

#idObject (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_presenceObject

Returns the value of attribute last_broadcast_presence.



13
14
15
# File 'lib/vines/stream/client/session.rb', line 13

def last_broadcast_presence
  @last_broadcast_presence
end

#stateObject (readonly)

Returns the value of attribute state.



14
15
16
# File 'lib/vines/stream/client/session.rb', line 14

def state
  @state
end

#userObject

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



27
28
29
# File 'lib/vines/stream/client/session.rb', line 27

def <=>(session)
  session.is_a?(Session) ? self.id <=> session.id : nil
end

#advance(state) ⇒ Object



37
38
39
# File 'lib/vines/stream/client/session.rb', line 37

def advance(state)
  @state = state
end

#authenticated?Boolean

Returns true if this client has properly authenticated with the server.

Returns:

  • (Boolean)


43
44
45
# File 'lib/vines/stream/client/session.rb', line 43

def authenticated?
  !@user.nil?
end

#available!Object



47
48
49
# File 'lib/vines/stream/client/session.rb', line 47

def available!
  @available = true
end

#available?Boolean

An available resource has sent initial presence and can receive presence subscription requests.

Returns:

  • (Boolean)


53
54
55
# File 'lib/vines/stream/client/session.rb', line 53

def available?
  @available && connected?
end

#available_subscribed_to_resourcesObject

Returns streams for available resources to which this user has successfully subscribed.



92
93
94
95
# File 'lib/vines/stream/client/session.rb', line 92

def available_subscribed_to_resources
  subscribed = @user.subscribed_to_contacts.map {|c| c.jid }
  router.available_resources(subscribed)
end

#available_subscribersObject

Returns streams for available resources that are subscribed to this user’s presence updates.



99
100
101
102
# File 'lib/vines/stream/client/session.rb', line 99

def available_subscribers
  subscribed = @user.subscribed_from_contacts.map {|c| c.jid }
  router.available_resources(subscribed)
end

#connected?Boolean

A connected resource has authenticated and bound a resource identifier.

Returns:

  • (Boolean)


59
60
61
# File 'lib/vines/stream/client/session.rb', line 59

def connected?
  !@unbound && authenticated? && !@user.jid.bare?
end

#hashObject



33
34
35
# File 'lib/vines/stream/client/session.rb', line 33

def hash
  @id.hash
end

#interested?Boolean

An interested resource has requested its roster and can receive roster pushes.

Returns:

  • (Boolean)


65
66
67
# File 'lib/vines/stream/client/session.rb', line 65

def interested?
  @requested_roster && connected?
end

#ready?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/vines/stream/client/session.rb', line 69

def ready?
  @state.class == Client::Ready
end

#remote_subscribers(to = nil) ⇒ Object

Returns contacts hosted at remote servers that are subscribed to this user’s presence updates.



106
107
108
109
110
111
# File 'lib/vines/stream/client/session.rb', line 106

def remote_subscribers(to=nil)
  jid = (to.nil? || to.empty?) ? nil : JID.new(to).bare
  @user.subscribed_from_contacts.reject do |c|
    router.local_jid?(c.jid) || (jid && c.jid.bare != jid)
  end
end

#requested_roster!Object



73
74
75
# File 'lib/vines/stream/client/session.rb', line 73

def requested_roster!
  @requested_roster = true
end

#stream_typeObject



77
78
79
# File 'lib/vines/stream/client/session.rb', line 77

def stream_type
  :client
end

#unbind!(stream) ⇒ Object

Called by the stream when its disconnected from the client. The stream passes itself to this method in case multiple streams are accessing this session.



84
85
86
87
88
# File 'lib/vines/stream/client/session.rb', line 84

def unbind!(stream)
  @unbound = true
  @available = false
  broadcast_unavailable
end