Esempio n. 1
0
    def __getitem__(self, key):
        if isinstance(key, tuple):
            contact_type, key = key
            if not isinstance(contact_type, ContactURIType):
                raise KeyError("unsupported contact type: %r" % contact_type)
        else:
            contact_type = NoGRUU
        if not isinstance(key, (str, Route)):
            raise KeyError("key must be a transport name or Route instance")

        transport = key if isinstance(key, str) else key.transport
        parameters = {} if transport == 'udp' else {'transport': transport}

        if contact_type is PublicGRUU:
            if self.public_gruu is None:
                raise KeyError("could not get Public GRUU")
            uri = SIPURI.new(self.public_gruu)
        elif contact_type is TemporaryGRUU:
            if self.temporary_gruu is None:
                raise KeyError("could not get Temporary GRUU")
            uri = SIPURI.new(self.temporary_gruu)
        elif contact_type is PublicGRUUIfAvailable and self.public_gruu is not None:
            uri = SIPURI.new(self.public_gruu)
        elif contact_type is TemporaryGRUUIfAvailable and self.temporary_gruu is not None:
            uri = SIPURI.new(self.temporary_gruu)
        else:
            ip = host.default_ip if isinstance(key, str) else host.outgoing_ip_for(key.address)
            if ip is None:
                raise KeyError("could not get outgoing IP address")
            port = getattr(Engine(), '%s_port' % transport, None)
            if port is None:
                raise KeyError("unsupported transport: %s" % transport)
            uri = SIPURI(user=self.username, host=ip, port=port)
        uri.parameters.update(parameters)
        return uri
Esempio n. 2
0
    def __getitem__(self, key):
        if isinstance(key, tuple):
            contact_type, key = key
            if not isinstance(contact_type, ContactURIType):
                raise KeyError("unsupported contact type: %r" % contact_type)
        else:
            contact_type = NoGRUU
        if not isinstance(key, (basestring, Route)):
            raise KeyError("key must be a transport name or Route instance")

        transport = key if isinstance(key, basestring) else key.transport
        parameters = {} if transport=='udp' else {'transport': transport}

        if contact_type is PublicGRUU:
            if self.public_gruu is None:
                raise KeyError("could not get Public GRUU")
            uri = SIPURI.new(self.public_gruu)
        elif contact_type is TemporaryGRUU:
            if self.temporary_gruu is None:
                raise KeyError("could not get Temporary GRUU")
            uri = SIPURI.new(self.temporary_gruu)
        elif contact_type is PublicGRUUIfAvailable and self.public_gruu is not None:
            uri = SIPURI.new(self.public_gruu)
        elif contact_type is TemporaryGRUUIfAvailable and self.temporary_gruu is not None:
            uri = SIPURI.new(self.temporary_gruu)
        else:
            ip = host.default_ip if isinstance(key, basestring) else host.outgoing_ip_for(key.address)
            if ip is None:
                raise KeyError("could not get outgoing IP address")
            port = getattr(Engine(), '%s_port' % transport, None)
            if port is None:
                raise KeyError("unsupported transport: %s" % transport)
            uri = SIPURI(user=self.username, host=ip, port=port)
        uri.parameters.update(parameters)
        return uri
Esempio n. 3
0
 def wait_for_network(wait_time=10, wait_message=None, test_ip='1.2.3.4'):
     """
     Make sure the network is available and can be reached. The function
     will return as soon as the network is reachable or it will raise
     RuntimeError if network is still unreachable after wait_time. The
     default value for test_ip checks if internet is reachable. Optionally
     it can log wait_message at INFO level if the function needs to wait.
     """
     for step in range(wait_time):
         local_ip = host.outgoing_ip_for(test_ip)
         if local_ip is not None:
             break
         elif step == 0 and wait_message is not None:
             log.info(wait_message)
         time.sleep(1)
     else:
         raise RuntimeError('Network is not available after waiting for {} seconds'.format(wait_time))
Esempio n. 4
0
    def __getitem__(self, key):
        if isinstance(key, tuple):
            # The first part of the key might be PublicGRUU and so on, but we don't care about
            # those here, so ignore them
            _, key = key
        if not isinstance(key, (basestring, Route)):
            raise KeyError("key must be a transport name or Route instance")

        transport = key if isinstance(key, basestring) else key.transport
        parameters = {} if transport=='udp' else {'transport': transport}
        if SIPConfig.local_ip not in (None, '0.0.0.0'):
            ip = SIPConfig.local_ip.normalized
        elif isinstance(key, basestring):
            ip = host.default_ip
        else:
            ip = host.outgoing_ip_for(key.address)
        if ip is None:
            raise KeyError("could not get outgoing IP address")
        port = getattr(Engine(), '%s_port' % transport, None)
        if port is None:
            raise KeyError("unsupported transport: %s" % transport)
        uri = SIPURI(user=self.username, host=ip, port=port)
        uri.parameters.update(parameters)
        return uri
Esempio n. 5
0
    def __getitem__(self, key):
        if isinstance(key, tuple):
            # The first part of the key might be PublicGRUU and so on, but we don't care about
            # those here, so ignore them
            _, key = key
        if not isinstance(key, (basestring, Route)):
            raise KeyError("key must be a transport name or Route instance")

        transport = key if isinstance(key, basestring) else key.transport
        parameters = {} if transport=='udp' else {'transport': transport}
        if SIPConfig.local_ip not in (None, '0.0.0.0'):
            ip = SIPConfig.local_ip.normalized
        elif isinstance(key, basestring):
            ip = host.default_ip
        else:
            ip = host.outgoing_ip_for(key.address)
        if ip is None:
            raise KeyError("could not get outgoing IP address")
        port = getattr(Engine(), '%s_port' % transport, None)
        if port is None:
            raise KeyError("unsupported transport: %s" % transport)
        uri = SIPURI(user=self.username, host=ip, port=port)
        uri.parameters.update(parameters)
        return uri