예제 #1
0
def process_candidate_json(candidate_json):
    candidate_value = calc_value(candidate_json)

    if app.status != C.STATUS_LEADER:
        try:
            dstip, dstport = decode_id(app.nodes[C.NODE_FRONT])

            if int(candidate_value) > int(app.value):
                app_logger.debug(debug_stamp() + 'Resending candidate: %s',
                                 candidate_json)
                app.conn.send_candidate(dstip, dstport, candidate_json)

            elif int(app.value) > int(candidate_value) and app.nodes[
                    C.NODE_LEADER]:
                app_logger.debug(
                    debug_stamp() + 'Swapping me as a candidate: %s',
                    candidate_json)
                start_candidacy()

            elif int(app.value) == int(candidate_value):
                app_logger.debug(debug_stamp() + 'Setting the leader state')
                app.status = C.STATUS_LEADER
                app.nodes[C.NODE_LEADER] = app.id
                app.conn.send_leader(dstip, dstport, app.id)
        except:
            pass
예제 #2
0
def process_new_connection(request):
    headers = request.headers
    newip = headers[C.HEADER_IP]
    newport = headers[C.HEADER_PORT]
    new_node = encode_id(newip, newport)
    app_logger.debug(debug_stamp() + "Welcoming %s", new_node)

    if not app.nodes[C.NODE_FRONT]:
        app.nodes[C.NODE_FRONT] = new_node

    old_back_friend = app.nodes[C.NODE_BACK]
    app.nodes[C.NODE_BACK] = new_node

    welcome_dict = {C.NODE_LEADER: app.nodes[C.NODE_LEADER]}
    if app.nodes[C.NODE_FRONT] == app.nodes[C.NODE_BACK]:
        welcome_dict[C.NODE_BACK] = app.id
    else:
        welcome_dict[C.NODE_BACK] = old_back_friend

    try:
        old_back_ip, old_back_port = decode_id(old_back_friend)
        app.conn.send_front_setting(old_back_ip, old_back_port,
                                    app.nodes[C.NODE_BACK])
    except:
        pass

    try:
        report_new_node(new_node)
    except:
        pass

    return json.dumps(welcome_dict), C.CODE_OK
예제 #3
0
def connection(ip, port):
    app_logger.debug(debug_stamp() + 'Waiting for a friend...')
    while not app.nodes[C.NODE_FRONT]:
        try:
            connect(ip, port)
        except:
            pass
        finally:
            time.sleep(C.SLEEP_TIME)
예제 #4
0
def process_leader_message(leader_json):
    if leader_json == app.id:
        app_logger.debug(debug_stamp() +
                         'Leader information distributed successfully')
        backip, backport = decode_id(app.nodes[C.NODE_BACK])
    elif app.nodes[C.NODE_FRONT]:
        dstip, dstport = decode_id(app.nodes[C.NODE_FRONT])
        app.conn.send_leader(dstip, dstport, leader_json)

    app.nodes[C.NODE_LEADER] = leader_json
    app_logger.info(info_stamp() + 'New leader: %s', app.nodes[C.NODE_LEADER])
예제 #5
0
def friendhit_repeating(front_node):
    app.repeater += 1
    if app.repeater >= 3:
        app_logger.debug(debug_stamp() + "Lost direct connection to %s",
                         app.nodes[C.NODE_FRONT])
        app.nodes[C.NODE_FRONT] = app.nodes[C.NODE_BACKUP_FRONT]
        if app.nodes[C.NODE_FRONT]:
            newfrontip, newfrontport = decode_id(app.nodes[C.NODE_FRONT])
            try:
                app.conn.send_back_setting(newfrontip, newfrontport, app.id)
            except:
                pass
        if app.nodes[C.NODE_BACK] == front_node:
            app.nodes[C.NODE_BACK] = None
        app.repeater = 0
        report_dead_node(front_node)
    else:
        app_logger.debug(
            debug_stamp() + 'Trying to establish connection... (%s)',
            str(app.repeater))
        time.sleep(1)
        friendhit()
예제 #6
0
def input():
    while True:
        input_string = raw_input()
        if input_string == '--i' or input_string == '--info':
            print_info()
        elif input_string == '--l' or input_string == '--logout':
            logout()
        elif input_string == '--d' or input_string == '--debug':
            ch.setLevel(
                logging.DEBUG) if ch.level == logging.INFO else ch.setLevel(
                    logging.DEBUG)
        else:
            try:
                if app.status == C.STATUS_LEADER:
                    send_regular_message_from_leader(input_string)
                else:
                    dstip, dstport = decode_id(app.nodes[C.NODE_LEADER])
                    app.conn.send_message(dstip, dstport, input_string, 0)
                app_logger.debug(debug_stamp() + 'Sending message: %s',
                                 input_string)
            except:
                app_logger.debug(debug_stamp() + 'Message has not been sent.')
                pass
예제 #7
0
def leader_checker():
    while True:
        if not app.nodes[C.NODE_LEADER]:
            app_logger.debug(debug_stamp() + 'Missing leader...')
            start_candidacy()
            time.sleep(C.SLEEP_TIME)
예제 #8
0
def outterHandler():
    headers = request.headers
    if C.HEADER_MESSAGE in headers:
        message_type = headers[C.HEADER_MESSAGE]

        # LEADER
        if message_type == C.TYPE_LEADER_INFO:
            leader = json.loads(request.json)
            app_logger.debug(debug_stamp() + "Received leader %s", leader)
            process_leader_message(leader)

        #  MESSAGE
        elif message_type == C.TYPE_MESSAGE:
            received_message = json.loads(request.json)
            process_regular_message(received_message)

        # CANDIDATE
        elif message_type == C.TYPE_CANDIDATE:
            candidate = json.loads(request.json)
            process_candidate_json(candidate)

        # FRONT FRIEND
        elif message_type == C.TYPE_FRONT:
            front_friend = json.loads(request.json)
            app_logger.debug(
                debug_stamp() + 'Front-friend setting received: %s',
                front_friend)
            process_front_friend_json(front_friend)

        # BACK FRIEND
        elif message_type == C.TYPE_BACK:
            back_friend = json.loads(request.json)
            app_logger.debug(
                debug_stamp() + 'Back-friend setting received: %s',
                back_friend)
            process_back_friend_json(back_friend)

        # HEART BEAT
        elif message_type == C.TYPE_HEARTBEAT:
            node = encode_id(headers[C.HEADER_IP], headers[C.HEADER_PORT])

            if node not in app.topology:
                app.topology.append(node)
                app_logger.debug(debug_stamp() + 'New node in the topology %s',
                                 node)

        # DEATH REPORT
        elif message_type == C.TYPE_DEATH:
            # TODO report death to all nodes
            dead_node = json.loads(request.json)
            process_dead_node(dead_node)

        # INFO DEATH
        elif message_type == C.TYPE_INFO_DEATH:  # follower
            dead_node = json.loads(request.json)
            process_info_death(dead_node)

        # LOGOUT
        elif message_type == C.TYPE_LOGOUT:  # leader
            loggedout_node = json.loads(request.json)
            process_logout(loggedout_node)

        # INFO LOGOUT
        elif message_type == C.TYPE_INFO_LOGOUT:  # follower
            loggedout_node = json.loads(request.json)
            process_info_loggedout(loggedout_node)

        # NEW
        elif message_type == C.TYPE_NEW:  # leader
            new_node = json.loads(request.json)
            process_new_node(new_node)

        # INFO NEW
        elif message_type == C.TYPE_INFO_NEW:  # follower
            new_node = json.loads(request.json)
            process_info_new(new_node)
예제 #9
0
def report_logout(loggedout_node):
    try:
        leaderip, leaderport = decode_id(app.nodes[C.NODE_LEADER])
        app.conn.logout_report(leaderip, leaderport, loggedout_node)
    except:
        app_logger.debug(debug_stamp() + 'Logging off without reporting')