Class: Kibosh::Session

Inherits:
Object
  • Object
show all
Includes:
Exceptions
Defined in:
lib/kibosh/session.rb,
lib/kibosh/session/stream.rb

Direct Known Subclasses

XMPP::Session

Defined Under Namespace

Modules: NewSessionResponse Classes: Stream, Streams, Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ Session

Returns a new instance of Session.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/kibosh/session.rb', line 100

def initialize request, response
  @timer = nil
  @timer_fires_at = nil
  @responses = []
  @bodies = []

  client[:xml_lang] = request["xml:lang"] 
  client[:ver] = Version.new(request["ver"]) if request["ver"]
  client[:wait] = Integer(request["wait"]) if request["wait"]
  client[:hold] = Integer(request["hold"]) if request["hold"]
  client[:from] = request["from"]
  client[:ack] = Integer(request["ack"]) if request["ack"]
  client[:content] = request["content"]

  @sid = ('%16.16f' % rand)[2..-1]
  @content = request["content"]

  response.session = self

  self.class.const_get(:Stream).new self,
                                     request,
                                     response.extend(NewSessionResponse) do |response|
    yield response
  end
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



12
13
14
# File 'lib/kibosh/session.rb', line 12

def content
  @content
end

#sidObject (readonly)

Returns the value of attribute sid.



12
13
14
# File 'lib/kibosh/session.rb', line 12

def sid
  @sid
end

Class Method Details

.find(request) ⇒ Object



14
15
16
# File 'lib/kibosh/session.rb', line 14

def self.find request
  raise "hell"
end

Instance Method Details

#acceptObject



164
165
# File 'lib/kibosh/session.rb', line 164

def accept
end

#charsetsObject



171
172
# File 'lib/kibosh/session.rb', line 171

def charsets
end

#defer(response) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/kibosh/session.rb', line 197

def defer response
  # puts "defer: now "
  raise "hell" if response.deliver_fired || response.deferred
  # puts "b4 #{@bodies.length}"
  if body = @bodies.shift
    # puts "body pop #{body.object_id}"
    streams[body["stream"]].lock body
    response.body = body
  else
    # puts "deferring: #{@responses.length} #{response.object_id} #{@bodies.length}"
    response.deferred = true
    response.on_close do
      response._cancel!
      # puts "response del #{response.object_id}"
      @responses.delete response
      check_responses 0
    end
    # puts "response push #{response.object_id}"
    @responses << response
    check_responses 0
  end
end

#handle(request, response) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/kibosh/session.rb', line 127

def handle request, response
  check_responses 1
  if stream = streams[request]
    stream.handle request, response
  else
    raise "hell: new stream"
  end
end

#holdObject



160
161
162
# File 'lib/kibosh/session.rb', line 160

def hold
  @hold ||= client[:hold] ? client[:hold] : 1;
end

#inactivityObject



152
153
154
# File 'lib/kibosh/session.rb', line 152

def inactivity
  @polling ||= 60
end

#maxpauseObject



167
168
169
# File 'lib/kibosh/session.rb', line 167

def maxpause
  @maxpause ||= 60*5
end

#pollingObject



148
149
150
# File 'lib/kibosh/session.rb', line 148

def polling
  @polling ||= 5
end

#requestsObject



156
157
158
# File 'lib/kibosh/session.rb', line 156

def requests
  @requests ||= hold+1
end

#respond(body) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/kibosh/session.rb', line 178

def respond body
  # p "respond"
  raise "hell" if !body
  if response = @responses.shift
    # puts "response pop #{response.object_id} #{body.object_id}"
    raise "hell" if !response.deferred || response.delivered
    response.body = body
    response._deliver
    body = nil
  else
    raise "hell" if !body
    # puts "body push #{body.object_id} #{@bodies.length}"
    # puts caller(0)
    @bodies << body unless @bodies.include? body
  end
  check_responses 0
  body
end

#stopObject



174
175
176
# File 'lib/kibosh/session.rb', line 174

def stop
  streams.stop
end

#streamsObject



84
85
86
# File 'lib/kibosh/session.rb', line 84

def streams
  @streams ||= Streams.new self
end

#verObject



141
142
143
144
145
146
# File 'lib/kibosh/session.rb', line 141

def ver
  @ver ||= (!client[:ver] && Version.new("1.8") ||
             client[:ver] < Version.new("1.8") ?
             client[:ver] :
             Version.new("1.8")).to_s
end

#waitObject



136
137
138
139
# File 'lib/kibosh/session.rb', line 136

def wait
  default = 60
  @wait ||= !client[:wait] && default || [ client[:wait], default ].min
end