Пример #1
0
    def handle_init_snapshot(cls, incoming_message):

        # Temporarily pause the money transfer
        MoneyTransferThread.set_enabled(False)

        # Grab the lock, since the current_snapshots data structure we modify ahead is shared between threads
        cls.marker_handler_lock.acquire()

        # Make sure that the new snapshot_id doesn't already exist
        snapshot_id = incoming_message.init_snapshot.snapshot_id
        if snapshot_id in cls.current_snapshots:
            print "ERROR! Got init snapshot again for snapshot id : " + str(
                snapshot_id)
            return

        print "Got init_snapshot msg (snapshot_id: " + str(snapshot_id) + ")"

        # Record local state
        current_balance = BankVault.get_balance()
        state = {"local": current_balance}
        cls.current_snapshots[snapshot_id] = state

        # Create marker message
        marker_msg = bank_pb2.Marker()
        marker_msg.snapshot_id = snapshot_id

        pb_msg = bank_pb2.BranchMessage()
        pb_msg.marker.CopyFrom(marker_msg)

        # Send marker message to everyone else
        total_peers = ThreadPool.get_thread_count()
        for i in range(total_peers):
            a_friend = ThreadPool.get_thread(i)

            if not a_friend:
                continue

            print "Sending marker message to : " + str(a_friend.remote_branch_name) + " (snapshot_id: "\
                  + str(snapshot_id) + ")"
            a_friend.send_msg_to_remote(pb_msg)

            # Start recording all incoming activity
            a_friend.add_recorder(snapshot_id)

        # Release the lock, we're done modifying the shared data structure
        cls.marker_handler_lock.release()

        # Resume the money transfer thread
        MoneyTransferThread.set_enabled(True)
Пример #2
0
    def handle_marker(cls, incoming_message, remote_branch_name):

        # Temporarily pause the money transfer
        MoneyTransferThread.set_enabled(False)

        # Grab the lock, since the current_snapshots data structure we modify ahead is shared between threads
        cls.marker_handler_lock.acquire()

        snapshot_id = incoming_message.marker.snapshot_id
        if snapshot_id in cls.current_snapshots:
            # This is a reply marker message
            print "Got reply marker msg from: " + str(
                remote_branch_name) + " (snapshot_id: " + str(
                    snapshot_id) + ")"

            # Get the state of the channel on which this marker was received
            total_peers = ThreadPool.get_thread_count()
            for i in range(total_peers):

                a_friend = ThreadPool.get_thread(i)
                if not a_friend:
                    continue

                if a_friend.remote_branch_name == remote_branch_name:
                    # Channel found, get the money recorded on this channel
                    money_in_flight = a_friend.pop_recorder(snapshot_id)

                    # Record the state of the channel
                    cls.current_snapshots[snapshot_id][str(
                        remote_branch_name)] = money_in_flight
                    break

        else:
            # This is the first marker message we're seeing
            print "Got the first marker msg from " + str(remote_branch_name) + " (snapshot_id: "\
                  + str(snapshot_id) + ")"

            # Record local state
            current_balance = BankVault.get_balance()
            state = {"local": current_balance}
            cls.current_snapshots[snapshot_id] = state

            # Record the state of the incoming channel from the sender to itself as empty
            cls.current_snapshots[snapshot_id][str(remote_branch_name)] = 0

            # Create marker message
            marker_msg = bank_pb2.Marker()
            marker_msg.snapshot_id = snapshot_id

            pb_msg = bank_pb2.BranchMessage()
            pb_msg.marker.CopyFrom(marker_msg)

            # Send marker msg to all outgoing channels, except self
            total_peers = ThreadPool.get_thread_count()
            for i in range(total_peers):
                a_friend = ThreadPool.get_thread(i)
                if not a_friend:
                    continue
                '''# MY HACK BEGIN --- ONLY FOR TESTING
                if a_friend.remote_branch_name == remote_branch_name:
                    transfer_msg_h = bank_pb2.Transfer()
                    pb_msg_h = bank_pb2.BranchMessage()

                    transfer_msg_h.money = 10

                    pb_msg_h.transfer.CopyFrom(transfer_msg_h)

                    a_friend.send_msg_to_remote(pb_msg_h)
                # MY HACK END ---'''

                print "Sending marker message to : " + str(a_friend.remote_branch_name) + " (snapshot_id: " \
                      + str(snapshot_id) + ")"
                a_friend.send_msg_to_remote(pb_msg)

                # Start recording all incoming activity
                if a_friend.remote_branch_name != remote_branch_name:
                    a_friend.add_recorder(snapshot_id)

        # Release the lock, we're done modifying the shared data structure
        cls.marker_handler_lock.release()

        # Resume the money transfer thread
        MoneyTransferThread.set_enabled(True)