def _transports_verified(all_results):
     if _Debug:
         lg.out(_DebugLevel, 'network_connector._transports_verified : %s' % str(all_results))
     order, all_results = all_results
     not_valid_count = 0
     restarts_count = 0
     if len(order) == 0:
         self.automat('network-transports-verified')
         return
     for proto in order:
         if not all_results[proto]:
             not_valid_count += 1
     for priority in range(len(order)):
         proto = order[priority]
         if not all_results[proto]:
             if _Debug:
                 lg.out(_DebugLevel, '    [%s] at position %d needs restart' % (proto, priority))
             gateway.transport(proto).automat('restart')
             restarts_count += 1
             if not_valid_count > 1:  # this one failed, 2 other failed as well
                 self.automat('network-transports-verified')
                 return
             continue
         if not_valid_count > 0:
             if _Debug:
                 lg.out(_DebugLevel, '    skip %d transport [%s]' % (priority, proto))
             if restarts_count == 0:
                 if _Debug:
                     lg.out(_DebugLevel, '    but no restarts and %d:[%s] is valid' % (priority, proto))
                 self.automat('network-transports-verified')
             return
         if _Debug:
             lg.out(_DebugLevel, '        [%s] at position %d is fine, skip other transports' % (proto, priority))
         self.automat('network-transports-verified')
         return
Example #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
Example #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