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
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

#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.



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

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



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

def available?
  @available && connected?
end

#available_subscribed_to_resourcesObject

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_subscribersObject

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.

Returns:

  • (Boolean)


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

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

#hashObject



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.

Returns:

  • (Boolean)


80
81
82
# File 'lib/vines/stream/client/session.rb', line 80

def interested?
  @requested_roster && connected?
end

#ready?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/vines/stream/client/session.rb', line 89

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

#remote_subscribed_to_contactsObject

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).bare
  @user.subscribed_from_contacts.reject do |c|
    @config.local_jid?(c.jid) || (jid && c.jid.bare != 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_typeObject



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