示例#1
0
 def handleElectionMessage(self, message, address):
     processId = message.params[0]
     if (int(processId) < self.sharedData['id']):
         log('sending OK for election ID ', message.params[1])
         message = Message(MessageType.OK, str(self.sharedData['id']),
                           message.params[1])
         self.socket.sendMessage(
             message.toByteStr(),
             address,
         )
         self.startElection()
示例#2
0
    def joinSwarm(self, address):

        print('(%s, %s) getting members' % (address[0], address[1]),
              flush=True)

        message = Message(MessageType.GET_MEMBERS, json.dumps(self.members))

        print('New member (%s, %s) joining swarm' % (address[0], address[1]),
              flush=True)

        self.members.append(address)

        self.socket.sendto(message.toByteStr(), address)
示例#3
0
    def startElection(self):
        log("Starting election...")
        electionId = str(threading.get_ident())
        log('ELECTION STARTED WITH ID ', electionId)
        self.sharedData['elections'][electionId] = {"isLeader": True}

        for member in self.sharedData['swarmMembers']:
            message = Message(MessageType.ELECTION, str(self.sharedData['id']),
                              electionId)
            self.socket.sendMessage(
                message.toByteStr(),
                tuple(member),
            )

        time.sleep(SECONDS_TO_WAIT_FOR_OK_RESPONSES)

        if (self.sharedData['elections'][electionId]['isLeader']):
            log("I'm the new leader!")
            self.sharedData['leader']['isSelf'] = True
            self.broadcastLeadership()
        else:
            log("I'm NOT the new leader!")
示例#4
0
def finishProcess(address, mySocket):
    print(address, flush=True)
    message = Message(MessageType.KILL)
    mySocket.sendto(message.toByteStr(), address)