Class: Vines::Stream::Http::Session

Inherits:
Client::Session show all
Includes:
Nokogiri::XML
Defined in:
lib/vines/stream/http/session.rb

Constant Summary collapse

CONTENT_TYPE =
'text/xml; charset=utf-8'.freeze

Instance Attribute Summary collapse

Attributes inherited from Client::Session

#domain, #id, #last_broadcast_presence, #state, #user

Instance Method Summary collapse

Methods inherited from Client::Session

#<=>, #advance, #authenticated?, #available!, #available?, #available_subscribed_to_resources, #available_subscribers, #bind!, #connected?, #hash, #interested?, #remote_subscribed_to_contacts, #remote_subscribers, #requested_roster!, #stream_type

Constructor Details

#initialize(stream) ⇒ Session

Returns a new instance of Session.



13
14
15
16
17
18
19
20
# File 'lib/vines/stream/http/session.rb', line 13

def initialize(stream)
  super
  @state = Http::Start.new(stream)
  @inactivity, @wait, @hold = 20, 60, 1
  @replied = Time.now
  @requests, @responses = [], []
  @content_type = CONTENT_TYPE
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



9
10
11
# File 'lib/vines/stream/http/session.rb', line 9

def content_type
  @content_type
end

#holdObject

Returns the value of attribute hold.



9
10
11
# File 'lib/vines/stream/http/session.rb', line 9

def hold
  @hold
end

#inactivityObject

Returns the value of attribute inactivity.



9
10
11
# File 'lib/vines/stream/http/session.rb', line 9

def inactivity
  @inactivity
end

#waitObject

Returns the value of attribute wait.



9
10
11
# File 'lib/vines/stream/http/session.rb', line 9

def wait
  @wait
end

Instance Method Details

#closeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vines/stream/http/session.rb', line 22

def close
  Sessions.delete(@id)
  router.delete(self)
  delete_from_cluster
  unsubscribe_pubsub
  @requests.each {|req| req.stream.close_connection }
  @requests.clear
  @responses.clear
  @state = Client::Closed.new(nil)
  @unbound = true
  @available = false
  broadcast_unavailable
end

#expired?Boolean

Returns:

  • (Boolean)


44
45
46
47
# File 'lib/vines/stream/http/session.rb', line 44

def expired?
  respond_to_expired_requests
  @requests.empty? && (Time.now - @replied > @inactivity)
end

#ready?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/vines/stream/http/session.rb', line 36

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

#reply(node) ⇒ Object

Send an HTTP 200 OK response wrapping the XMPP node content back to the client.



76
77
78
79
80
81
# File 'lib/vines/stream/http/session.rb', line 76

def reply(node)
  if request = @requests.shift
    request.reply(node, @content_type)
    @replied = Time.now
  end
end

#request(request) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vines/stream/http/session.rb', line 60

def request(request)
  if @responses.any?
    request.reply(wrap_body(@responses.join), @content_type)
    @replied = Time.now
    @responses.clear
  else
    while @requests.size >= @hold
      @requests.shift.reply(wrap_body(''), @content_type)
      @replied = Time.now
    end
    @requests << request
  end
end

#requestsObject



40
41
42
# File 'lib/vines/stream/http/session.rb', line 40

def requests
  @requests.clone
end

#resume(stream, node) ⇒ Object

Resume this session from its most recent state with a new client stream and incoming node.



51
52
53
54
55
56
57
58
# File 'lib/vines/stream/http/session.rb', line 51

def resume(stream, node)
  stream.session.requests.each do |req|
    request(req)
  end
  stream.session = self
  @state.stream = stream
  @state.node(node)
end

#unbind!(stream) ⇒ Object



95
96
97
# File 'lib/vines/stream/http/session.rb', line 95

def unbind!(stream)
  @requests.reject! {|req| req.stream == stream }
end

#write(node) ⇒ Object

Write the XMPP node to the client stream after wrapping it in a BOSH body tag. If there’s a waiting request, the node is written immediately. If not, it’s queued until the next request arrives.



86
87
88
89
90
91
92
93
# File 'lib/vines/stream/http/session.rb', line 86

def write(node)
  if request = @requests.shift
    request.reply(wrap_body(node), @content_type)
    @replied = Time.now
  else
    @responses << node.to_s
  end
end