コード例 #1
0
    def datagramReceived(self, packet, from_addr):
        try:
            
            message_type, data = packet.split(' ', 1)

            if message_type == 'propose':
                print data
                self.replicated_val.propose_update( data )

            else:
                from_uid = self.addrs[from_addr]

                print 'rcv', from_uid, ':', packet

                # Dynamically search the class for a method to handle this message
                handler = getattr(self.replicated_val, 'receive_' + message_type, None)

                if handler:
                    kwargs = json.loads(data)

                    for k in kwargs.keys():
                        if k.endswith('_id') and kwargs[k] is not None:
                            # JSON encodes the proposal ids as lists,
                            # composable-paxos requires requires ProposalID instances
                            kwargs[k] = ProposalID(*kwargs[k])
                        
                    handler(from_uid, **kwargs)
            
        except Exception:
            print 'Error processing packet: ', packet
            import traceback
            traceback.print_exc()
コード例 #2
0
    def datagramReceived(self, packet, from_addr):
        try:

            message_type, data = packet.split(' ', 1)
            kwargs = json.loads(data)
            if message_type != 'propose':
                try:
                    from_uid = self.addrs[from_addr]
                except Exception:
                    from_uid = "UNK"

                print
                'rcv', from_uid, ':', packet

                handler = getattr(self, 'receive_' + message_type, None)

                if handler:
                    kwargs = json.loads(data)

                    for k in kwargs.keys():
                        if k.endswith('_id') and kwargs[k] is not None:
                            # JSON encodes the proposal ids as lists,
                            # composable-paxos requires requires ProposalID instances
                            kwargs[k] = ProposalID(*kwargs[k])

                    handler(from_uid, **kwargs)
        except Exception:
            print
            'Error processing packet: ', packet
            import traceback
            traceback.print_exc()
コード例 #3
0
ファイル: master_strategy.py プロジェクト: tszngai/consensus
    def advance_instance(self, new_instance_number, new_current_value, catchup=False):

        self.master_attempt = False

        if catchup:
            super(DedicatedMasterStrategyMixin,self).advance_instance(new_instance_number, new_current_value)
            self.paxos.prepare() # ensure we won't send any prepare messages with ID 1 (might conflict with the current master)
            return
        
        t = json.loads(new_current_value) # Returns a list: [master_uid, application_value]. Only one element will be valid

        if t[0] is not None:
            print '   Lease Granted: ', t[0]
            self.update_lease( t[0] )
            
            new_current_value = self.current_value
        else:
            print '   Application Value:', t[1]
            new_current_value = t[1]

        super(DedicatedMasterStrategyMixin,self).advance_instance(new_instance_number, new_current_value)

        if self.master_uid:

            master_pid = ProposalID(1,self.master_uid)
            
            if self.master_uid == self.network_uid:
                self.paxos.prepare()
                
                for uid in self.peers:
                    self.paxos.receive_promise( Promise(uid, self.network_uid, master_pid, None, None) )
            else:
                self.paxos.receive_prepare( Prepare(self.master_uid, master_pid) )
                self.paxos.observe_proposal( master_pid )
コード例 #4
0
ファイル: replicated_value.py プロジェクト: tszngai/consensus
 def to_pid(v):
     return ProposalID(*v) if v else None
コード例 #5
0
    def datagramReceived(self, packet0, from_addr):
        packet = str(packet0, 'utf-8')
        #print(packet)
        #print('received\n')
        try:
            
            message_type, data = packet.split(' ', 1)
            if message_type == 'propose_update':
                self.replicated_val.propose_update(data)

            elif message_type == 'mobility_req':
                node, new_cluster = data.split('-', 1)
                self.replicated_val.receive_mobility_req(node, int(new_cluster))
            elif message_type == 'account':
                print("rcv: ", packet)
                handler = getattr(self.replicated_val, 'receive_' + message_type, None)
                kwargs = json.loads(data)
                handler(**kwargs)
            elif message_type == 'propose':
                #case on algorihm - either optimistic or coordinator
                sndr, rcvr, amount = data.split('-', 2)
                if config.algorithm == 'optimistic':
                    #case on whether inter or intra cluster 
                    if self.replicated_val.same_ledger(sndr, rcvr):
                        self.replicated_val.propose_update( data )
                    else:
                        print("inter-ledger transaction initiated - optimistic algorithm")
                        self.replicated_val.propose_update(data)
                        self.send_propose(rcvr, data)
                elif config.algorithm == 'coordinator':      
                    if self.replicated_val.double_spending(sndr):
                        print("ERROR: no double spending: must wait until pending commit for next transaction")
                        return

                    #case on whether inter or intra cluster 
                    if self.replicated_val.same_ledger(sndr, rcvr):
                        self.replicated_val.propose_update( data )
                    else:
                        #TODO error checking, move the below case toa coordinator alg function call
                        print("inter-ledger transaction initiated - coordinator based algorithm")
                        lca_addr, lca_id = config.lca[(self.replicated_val.get_cluster(sndr), self.replicated_val.get_cluster(rcvr))]
                        self.replicated_val.new_inter_ledger(data)
                        self.send_propose_to_lca(lca_addr, data)
            # optimistic alg
            elif message_type == 'propose_to_rcvr':
                self.replicated_val.propose_update( data )
            # coordinator based alg
            elif message_type == 'propose_to_lca' or message_type == 'seq_req' or message_type == 'seq' or message_type == 'lcacommit_c':
                print("rcv: ", packet)
                handler = getattr(self.replicated_val, 'receive_' + message_type, None)
                kwargs = json.loads(data)
                handler(**kwargs)
            #normal case
            else:
                from_uid = self.addrs[from_addr]

                print('rcv', from_uid, ':', packet)

                # Dynamically search the class for a method to handle this message
                handler = getattr(self.replicated_val, 'receive_' + message_type, None)

                if handler:
                    kwargs = json.loads(data)

                    for k in kwargs.keys():
                        if k.endswith('_id') and kwargs[k] is not None:
                            # JSON encodes the proposal ids as lists,
                            # composable-paxos requires requires ProposalID instances
                            kwargs[k] = ProposalID(*kwargs[k])
                        
                    handler(from_uid, **kwargs)
            
        except Exception:
            print('Error processing packet: ', packet)
            import traceback
            traceback.print_exc()