Ejemplo n.º 1
0
 def _check_to_use_best_proto(self):
     # if no incoming traffic - do nothing
     if len(active_protos()) == 0:
         return True
     lid = my_id.getLocalIdentity()
     order = lid.getProtoOrder()
     # if no protocols in local identity - do nothing
     if len(order) == 0:
         return True
     # when transport proxy is working we do not need to check our contacts at all
     if settings.transportIsEnabled('proxy'):
         if driver.is_on('service_proxy_transport'):
             if settings.transportReceivingIsEnabled('proxy'):
                 try:
                     # TODO: change here to receive the value directly from service_proxy_transport object
                     router_idurl = driver.services(
                     )['service_proxy_transport'].transport.options[
                         'router_idurl']
                 except:
                     router_idurl = None
                 if router_idurl:
                     router_identity = identitycache.FromCache(router_idurl)
                     contacts_is_ok = True
                     router_protos = router_identity.getContactsByProto()
                     if lid.getContactsNumber() != len(router_protos):
                         contacts_is_ok = False
                     if contacts_is_ok:
                         for proto, contact in router_protos.items():
                             if lid.getProtoContact(proto) != contact:
                                 contacts_is_ok = False
                     if contacts_is_ok:
                         if _Debug:
                             lg.out(
                                 _DebugLevel - 6,
                                 'p2p_connector._check_to_use_best_proto returning True : proxy_transport is OK'
                             )
                         return True
     first = order[0]
     # if first contact in local identity is not working yet
     # but there is another working methods - switch first method
     if first not in active_protos():
         if _Debug:
             lg.out(
                 _DebugLevel - 6,
                 'p2p_connector._check_to_use_best_proto first contact (%s) is not working!   active_protos()=%s'
                 % (first, str(active_protos())))
         return False
     # #small hack to make udp as first method if all is fine
     # if first != 'udp' and ('udp' in active_protos() and 'tcp' in active_protos()):
     #     lg.out(2, 'p2p_connector._check_to_use_best_proto first contact (%s) but UDP also works!  active_protos()=%s' % (first, str(active_protos())))
     #     return False
     # if tcp contact is on first place and it is working - we are VERY HAPPY! - no need to change anything - return False
     if first == 'tcp' and 'tcp' in active_protos():
         return True
     # but if tcp method is not the first and it works - we want to TURN IT ON! - return True
     if first != 'tcp' and 'tcp' in active_protos():
         if _Debug:
             lg.out(
                 _DebugLevel - 6,
                 'p2p_connector._check_to_use_best_proto tcp is not first but it works active_protos()=%s'
                 % str(active_protos()))
         return False
     # if we are using udp and it is working - this is fantastic!
     if first == 'udp' and 'udp' in active_protos():
         # but let's check if TCP is also working
         # in that case we want to switch to TCP
         if 'tcp' in active_protos():
             return False
         return True
     # udp seems to be working and first contact is not working - so switch to udp
     if first != 'udp' and 'udp' in active_protos():
         if _Debug:
             lg.out(
                 _DebugLevel - 6,
                 'p2p_connector._check_to_use_best_proto udp is not first but it works active_protos()=%s'
                 % str(active_protos()))
         return False
     # http seems to work and it is first - cool!
     if first == 'http' and 'http' in active_protos():
         return True
     # but if http method is not the first and it works - we want to TURN IT ON! - return True
     if first != 'http' and 'http' in active_protos():
         if _Debug:
             lg.out(
                 _DebugLevel - 6,
                 'p2p_connector._check_to_use_best_proto http is not first but it works active_protos()=%s'
                 % str(active_protos()))
         return False
     # if we are using proxy and it is working - that is fine - it must work always!
     if first == 'proxy' and 'proxy' in active_protos():
         return True
     # proxy seems to be working and first contact is not working - so switch to proxy
     if first != 'proxy' and 'proxy' in active_protos():
         if _Debug:
             lg.out(
                 _DebugLevel - 6,
                 'p2p_connector._check_to_use_best_proto proxy is not first but it works active_protos()=%s'
                 % str(active_protos()))
         return False
     # in other cases - do nothing
     return True
Ejemplo n.º 2
0
def buildProtoContacts(id_obj, skip_transports=[]):
    """
    Create a full list of needed transport methods to be able to accept
    incoming traffic from other nodes.

    Make calls to transport services to build a list of my contacts.
    """
    from services import driver
    # prepare contacts
    current_contats = id_obj.getContactsByProto()
    current_order = id_obj.getProtoOrder()
    lg.out(4, 'my_id.buildProtoContacts')
    lg.out(4, '    current contacts: %s' % str(current_contats))
    lg.out(4, '    current order: %s' % str(current_order))
    new_contacts = {}
    new_order_correct = []
    # prepare list of active transports
    active_transports = []
    for proto in getValidTransports():
        if proto in skip_transports:
            continue
        if not settings.transportIsEnabled(proto):
            continue
        if not settings.transportReceivingIsEnabled(proto):
            continue
        if not driver.is_on('service_%s_transport' % proto):
            lg.warn(
                'transport "%s" is enabled, but service_%s_transport() is not ready yet'
                % (proto, proto))
            continue
        active_transports.append(proto)
    # sort active transports by priority
    lg.out(4, '    active transports: %s' % str(active_transports))
    active_transports.sort(key=settings.getTransportPriority)
    lg.out(4, '    sorted transports: %s' % str(active_transports))
    if not driver.is_on('service_gateway'):
        new_contacts = current_contats
        new_order_correct = current_order
    else:
        from transport import gateway
        # build contacts data according transports priorities
        new_order = current_order
        for proto in active_transports:
            clist = gateway.transport(proto).interface.build_contacts(id_obj)
            cdict = {}
            corder = []
            for contact in clist:
                cproto, _ = contact.split(b'://')
                cdict[cproto] = contact
                corder.append(cproto)
            new_contacts.update(cdict)
            for cproto in corder:
                if cproto not in new_order:
                    new_order.append(cproto)
        new_order_correct = list(new_order)
        for nproto in new_order:
            if nproto not in list(new_contacts.keys()):
                new_order_correct.remove(nproto)


#            cset = set(corder)
#            cdiff = cset.intersection(current_set)
#            if cset.isdisjoint()
#
#
#            if len(clist) > 1:
#                # clist.reverse()
#                for contact in clist:
#                    cproto, cdata = contact.split('://')
#                    cdict[cproto] = contact
#                    if cproto in new_order:
#                        new_order.remove(cproto)
#                    new_order.insert(0, cproto)
#            else:
##                 current_order = []
#                for contact in clist:
#                    cproto, cdata = contact.split('://')
#                    cdict[cproto] = contact
# current_order.append(cproto)
#                    new_index = -1
#                    if cproto in new_order:
#                        new_index = new_order.index(cproto)
#                    old_index = -1
#                    if cproto in current_order:
#                        old_index =  current_order.index(cproto)
#                    if new_index < 0:
#                        new_order.insert(0, cproto)
#                    else:
#                        if old_index < new_index:
#                            new_order.remove(cproto)
#                            new_order.insert(0, cproto)
#                        else:
#                            new_order.remove(cproto)
#                            new_order.append(cproto)
#            new_contacts.update(cdict)

    lg.out(4, '    new contacts: %s' % str(new_contacts))
    lg.out(4, '    new order: %s' % str(new_order_correct))

    #    new_list = []
    #    for nproto in new_order_correct:
    #        new_list.append(new_contacts[nproto])

    return new_contacts, new_order_correct
Ejemplo n.º 3
0
def buildProtoContacts(id_obj, skip_transports=[]):
    """
    Create a full list of needed transport methods to be able to accept
    incoming traffic from other nodes.

    Make calls to transport services to build a list of my contacts.
    """
    from services import driver
    # prepare contacts
    current_contats = id_obj.getContactsByProto()
    current_order = id_obj.getProtoOrder()
    if _Debug:
        lg.out(_DebugLevel, 'my_id.buildProtoContacts')
        lg.out(_DebugLevel, '    current contacts: %s' % str(current_contats))
        lg.out(_DebugLevel, '    current order: %s' % str(current_order))
    new_contacts = {}
    new_order_correct = []
    # prepare list of active transports
    active_transports = []
    for proto in getValidTransports():
        if proto in skip_transports:
            continue
        if not settings.transportIsEnabled(proto):
            continue
        if not settings.transportReceivingIsEnabled(proto):
            continue
        if not driver.is_on('service_%s_transport' % proto):
            lg.warn('transport "%s" is enabled, but service_%s_transport() is not ready yet' % (proto, proto))
            continue
        active_transports.append(proto)
    # sort active transports by priority
    if _Debug:
        lg.out(_DebugLevel, '    active transports: %s' % str(active_transports))
    active_transports.sort(key=settings.getTransportPriority)
    if _Debug:
        lg.out(_DebugLevel, '    sorted transports: %s' % str(active_transports))
    if not driver.is_on('service_gateway'):
        new_contacts = current_contats
        new_order_correct = current_order
        lg.warn('service_gateway() is not started, use my current contacts as a source')
    else:
        from transport import gateway
        # build contacts data according transports priorities
        new_order = current_order
        for proto in active_transports:
            clist = gateway.transport(proto).interface.build_contacts(id_obj)
            cdict = {}
            corder = []
            for contact in clist:
                cproto, _ = contact.split(b'://')
                cdict[cproto] = contact
                corder.append(cproto)
            new_contacts.update(cdict)
            for cproto in corder:
                if cproto not in new_order:
                    new_order.append(cproto)
        new_order_correct = list(new_order)
        for nproto in new_order:
            if nproto not in list(new_contacts.keys()):
                new_order_correct.remove(nproto)
    if _Debug:
        lg.out(_DebugLevel, '    new contacts: %s' % str(new_contacts))
        lg.out(_DebugLevel, '    new order: %s' % str(new_order_correct))
    return new_contacts, new_order_correct