def _generateInvite(self, contacturi, fromName, uri, noSDP): #RFC 3261 8.1.1 #RFC 3261 13.2.1 invite = Request("INVITE", uri) invite.addHeader("to", formatAddress(uri)) invite.addHeader( "from", formatAddress((fromName, URL(contacturi.host, contacturi.username), { 'tag': self.genTag() }))) invite.addHeader( "call-id", "%s@%s" % (md5.md5(str(random.random())).hexdigest(), contacturi.host)) invite.addHeader("cseq", "%s INVITE" % self.localCSeq) invite.addHeader("user-agent", "Divmod Sine") if noSDP: invite.headers["content-length"] = ["0"] else: invite.addHeader("content-type", "application/sdp") #XXX maybe rip off IP discovered in SDP phase? invite.addHeader("contact", formatAddress(contacturi)) def fillSDP(_): def consultSDP(sdpobj): sdp = sdpobj.show() self.sessionDescription = sdp invite.body = sdp invite.headers['content-length'] = [str(len(sdp))] invite.creationFinished() return defer.maybeDeferred(self.rtp.getSDP, self, None).addCallback(consultSDP) def finish(_): self.msg = invite toAddress, fromAddress = self._finishInit() self.localAddress = fromAddress self.remoteAddress = toAddress return self d = self.rtp.createRTPSocket(self, contacturi.host) def gotCookie(c): self.cookie = c d.addCallback(gotCookie) if noSDP: invite.creationFinished() return d.addCallback(finish) else: return d.addCallback(fillSDP).addCallback(finish)
def reinvite(self, newContact, newSDP): """ Send a new INVITE to the remote address for this dialog. @param newContact: a new local address for this dialog. if None, the existing one is used. @param newSDP: An L{xshtoom.sdp.SDP} instance describing the new session. """ newSDP = upgradeSDP(self.sessionDescription, newSDP) msg = self.generateRequest('INVITE') if newContact: msg.headers['contact'] = [formatAddress(newContact)] msg.body = newSDP.show() msg.headers['content-length'] = [str(len(newSDP.show()))] msg.addHeader("content-type", "application/sdp") self.reinviteMsg = msg self.reinviteSDP = newSDP for ct in self.tu.cts: if (isinstance(ct, ClientInviteTransaction) and ct.mode not in ('completed', 'terminated')): self.clientState = "reinvite-waiting" return for st in self.tu.transport.serverTransactions.values(): if (st.tu == self.tu and st.mode not in ('confirmed', 'terminated')): self.clientState = "reinvite-waiting" #XXX gotta do something to trigger the reinvite once the #offending ST finishes return return self._sendReinvite(msg)
def generateRequest(self, method): #RFC 3261 12.2.1.1 r = Request(method, self.remoteAddress[1]) if self.routeSet: r.headers['route'] = [formatAddress(route) for route in self.routeSet] if 'lr' not in self.routeSet[0][1].other: r.headers['route'].append(formatAddress(("", r.uri, {}))) r.uri = parseAddress(r.headers['route'].pop())[1] r.addHeader('to', formatAddress(self.remoteAddress)) r.addHeader('from', formatAddress(self.localAddress)) r.addHeader('cseq', "%s %s" % (self.localCSeq, method)) self.localCSeq += 1 r.addHeader('call-id', self.msg.headers['call-id'][0]) r.addHeader('contact', formatAddress(self.contactURI)) r.addHeader('content-length', 0) return r
def _generateInvite(self, contacturi, fromName, uri, noSDP): #RFC 3261 8.1.1 #RFC 3261 13.2.1 invite = Request("INVITE", uri) invite.addHeader("to", formatAddress(uri)) invite.addHeader("from", formatAddress((fromName, URL(contacturi.host, contacturi.username), {'tag': self.genTag()}))) invite.addHeader("call-id", "%s@%s" % (hashlib.md5(str(random.random())).hexdigest(), contacturi.host)) invite.addHeader("cseq", "%s INVITE" % self.localCSeq) invite.addHeader("user-agent", "Divmod Sine") if noSDP: invite.headers["content-length"] = ["0"] else: invite.addHeader("content-type", "application/sdp") #XXX maybe rip off IP discovered in SDP phase? invite.addHeader("contact", formatAddress(contacturi)) def fillSDP(_): def consultSDP(sdpobj): sdp = sdpobj.show() self.sessionDescription = sdp invite.body = sdp invite.headers['content-length'] = [str(len(sdp))] invite.creationFinished() return defer.maybeDeferred(self.rtp.getSDP, self, None).addCallback(consultSDP) def finish(_): self.msg = invite toAddress,fromAddress = self._finishInit() self.localAddress = fromAddress self.remoteAddress = toAddress return self d = self.rtp.createRTPSocket(self, contacturi.host) def gotCookie(c): self.cookie = c d.addCallback(gotCookie) if noSDP: invite.creationFinished() return d.addCallback(finish) else: return d.addCallback(fillSDP).addCallback(finish)
def generateRequest(self, method): #RFC 3261 12.2.1.1 r = Request(method, self.remoteAddress[1]) if self.routeSet: r.headers['route'] = [ formatAddress(route) for route in self.routeSet ] if 'lr' not in self.routeSet[0][1].other: r.headers['route'].append(formatAddress(("", r.uri, {}))) r.uri = parseAddress(r.headers['route'].pop())[1] r.addHeader('to', formatAddress(self.remoteAddress)) r.addHeader('from', formatAddress(self.localAddress)) r.addHeader('cseq', "%s %s" % (self.localCSeq, method)) self.localCSeq += 1 r.addHeader('call-id', self.msg.headers['call-id'][0]) r.addHeader('contact', formatAddress(self.contactURI)) r.addHeader('content-length', 0) return r
def responseFromRequest(self, code, msg, body, bodyType="application/sdp"): response = Response(code) for name in ("via", "call-id", "record-route", "cseq"): response.headers[name] = msg.headers.get(name, [])[:] if self.direction == 'server': response.addHeader('to', formatAddress(self.localAddress)) response.addHeader('from', formatAddress(self.remoteAddress)) elif self.direction == 'client': response.addHeader('from', formatAddress(self.localAddress)) response.addHeader('to', formatAddress(self.remoteAddress)) response.addHeader('user-agent', "Divmod Sine") if msg.method == 'INVITE' and code == 200: response.addHeader('contact', formatAddress(self.contactURI)) response.addHeader('content-length', len(body)) response.addHeader('content-type', bodyType) response.bodyDataReceived(body) else: response.addHeader('content-length', 0) response.creationFinished() return response