Ejemplo n.º 1
0
 def recvEvent(self, event):
     if isinstance(event, CCEventDisconnect) or isinstance(
             event, CCEventFail) or isinstance(event, CCEventRedirect):
         #print 'event', event, 'received in the Connected state sending BYE'
         redirect = None
         if isinstance(event, CCEventDisconnect):
             redirect = event.getData()
         elif isinstance(event, CCEventRedirect):
             redirects = event.getData()
             if redirects != None:
                 redirect = redirects[0]
         if redirect != None and self.ua.useRefer:
             req = self.ua.genRequest('REFER', reason=event.reason)
             self.ua.lCSeq += 1
             also = SipReferTo(address=redirect)
             req.appendHeader(SipHeader(name='refer-to', body=also))
             rby = SipReferredBy(address=SipAddress(
                 url=self.ua.lUri.getUrl()))
             req.appendHeader(SipHeader(name='referred-by', body=rby))
             self.ua.global_config['_sip_tm'].newTransaction(req, self.rComplete, \
               laddress = self.ua.source_address, compact = self.ua.compact_sip)
         else:
             req = self.ua.genRequest('BYE', reason=event.reason)
             self.ua.lCSeq += 1
             if redirect != None:
                 also = SipAlso(address=redirect)
                 req.appendHeader(SipHeader(name='also', body=also))
             self.ua.global_config['_sip_tm'].newTransaction(req, \
               laddress = self.ua.source_address, compact = self.ua.compact_sip)
         self.ua.cancelCreditTimer()
         self.ua.disconnect_ts = event.rtime
         return (UaStateDisconnected, self.ua.disc_cbs, event.rtime,
                 event.origin)
     if isinstance(event, CCEventUpdate):
         body = event.getData()
         if str(self.ua.lSDP) == str(body):
             if self.ua.rSDP != None:
                 self.ua.equeue.append(CCEventConnect((200, 'OK', self.ua.rSDP.getCopy()), \
                     rtime = event.rtime, origin = event.origin))
             else:
                 self.ua.equeue.append(CCEventConnect((200, 'OK', None), rtime = event.rtime, \
                   origin = event.origin))
             return None
         if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
             try:
                 self.ua.on_local_sdp_change(
                     body,
                     lambda x: self.ua.recvEvent(event),
                     en_excpt=True)
             except Exception as e:
                 event = CCEventFail((400, 'Malformed SDP Body'),
                                     rtime=event.rtime)
                 event.setWarning(str(e))
                 self.ua.equeue.append(event)
             return None
         if event.max_forwards != None:
             if event.max_forwards <= 0:
                 self.ua.equeue.append(
                     CCEventFail((483, 'Too Many Hops'), rtime=event.rtime))
                 return None
             max_forwards_hf = SipMaxForwards(number=event.max_forwards - 1)
         else:
             max_forwards_hf = None
         req = self.ua.genRequest('INVITE', body, reason = event.reason, \
           max_forwards = max_forwards_hf)
         self.ua.lCSeq += 1
         self.ua.lSDP = body
         self.ua.tr = self.ua.global_config['_sip_tm'].newTransaction(req, self.ua.recvResponse, \
           laddress = self.ua.source_address, cb_ifver = 2, compact = self.ua.compact_sip)
         return (UacStateUpdating, )
     if isinstance(event, CCEventInfo):
         body = event.getData()
         req = self.ua.genRequest('INFO', reason=event.reason)
         req.setBody(body)
         self.ua.lCSeq += 1
         self.ua.global_config['_sip_tm'].newTransaction(req, None, \
           laddress = self.ua.source_address, compact = self.ua.compact_sip)
         return None
     if self.ua.pending_tr != None and isinstance(event, CCEventConnect):
         if self.ua.expire_timer != None:
             self.ua.expire_timer.cancel()
             self.ua.expire_timer = None
         code, reason, body = event.getData()
         if body != None and self.ua.on_local_sdp_change != None and body.needs_update:
             self.ua.on_local_sdp_change(body,
                                         lambda x: self.ua.recvEvent(event))
             return None
         self.ua.startCreditTimer(event.rtime)
         self.ua.connect_ts = event.rtime
         self.ua.lSDP = body
         self.ua.pending_tr.ack.setBody(body)
         self.ua.global_config['_sip_tm'].sendACK(self.ua.pending_tr)
         self.ua.pending_tr = None
         for callback in self.ua.conn_cbs:
             callback(self.ua, event.rtime, self.ua.origin)
         return None
     #print 'wrong event %s in the Connected state' % event
     return None