class Acceptor (object):
    _sync = {}
    _async = ['set_multi', 'onPrepare', 'accept']
    _ref = ['set_multi', 'onPrepare']
    _parallel = []
    
    def __init__(self, quorum_size):
        self.accepted_id = {}
        self.quorum_size = quorum_size
        self.accepted_value = {}
        self.promised_id = {}
        
    def set_multi(self, learners): 
        self.learners = learners
        self.amulti = AMulti(self.learners)
        
    def onPrepare(self, proposer, index, proposal_id):
        '''
        Called when a Prepare message is received from a Proposer
        '''
        if not self.accepted_id.has_key(index):
            self.accepted_id[index] = None
            self.accepted_value[index] = None
        try:
            if proposal_id == self.promised_id[index]:
                # Duplicate prepare message
                proposer.promise(self.id, proposal_id, index, self.accepted_id[index], self.accepted_value[index])
            elif proposal_id > self.promised_id[index]:
                self.promised_id[index] = proposal_id
                proposer.promise(self.id, proposal_id, index, self.accepted_id[index], self.accepted_value[index])
        except:
            self.promised_id[index] = proposal_id
            proposer.promise(self.id, proposal_id, index, self.accepted_id[index], self.accepted_value[index])

            
        
                    
    def accept(self, from_uid, index, proposal_id, value):
        '''
        Called when an Accept! message is received from a Proposer
        '''
        if proposal_id >= self.promised_id[index]:
            self.promised_id[index]     = proposal_id
            self.accepted_id[index]     = proposal_id
            self.accepted_value[index]  = value
            '''send accepted to all learners!! '''
            self.amulti.accepted(self.id, proposal_id, index,self.accepted_value[index])
Exemplo n.º 2
0
class Acceptor (object):
    _sync = {'onPrepare':'2'}
    _async = ['set_multi', 'accept']
    _ref = ['set_multi', 'onPrepare']
    _parallel = []
    
    def __init__(self, quorum_size):
        self.accepted_id = None
        self.quorum_size = quorum_size
        self.accepted_value = None
        self.promised_id = None
        
    def set_multi(self, learners): 
        self.learners = learners
        self.amulti = AMulti(self.learners)
        
    def onPrepare(self, proposer, proposal_id):
        '''
        Called when a Prepare message is received from a Proposer
        '''
        if proposal_id == self.promised_id:
            # Duplicate prepare message
            return [self.id, proposal_id, self.accepted_id, self.accepted_value]
            
        
        elif proposal_id > self.promised_id:
            self.promised_id = proposal_id
            return [self.id, proposal_id, self.accepted_id, self.accepted_value]
        
                    
    def accept(self, from_uid, proposal_id, value):
        '''
        Called when an Accept! message is received from a Proposer
        '''
        if proposal_id >= self.promised_id:
            self.promised_id     = proposal_id
            self.accepted_id     = proposal_id
            self.accepted_value  = value
            '''send accepted to all learners!! '''
            self.amulti.accepted(self.id, proposal_id, self.accepted_value)