def countVotes(self, indexTable): logger.split() logger.verbose("Counting votes") logger.split() logger.info("Election Results:", False) logger.split() voteCount = {} for index in indexTable: # index = int(ind) key = SessionKey(int(indexTable[index], 16)) vote = key.decrypt(self.votes[index]) logger.special("%7s: %s" % (index, vote), False) if vote in voteCount: voteCount[vote] += 1 else: voteCount[vote] = 1 logger.split().split() mVote = None for vote in voteCount: if not mVote or voteCount[vote] > mVote: mVote = voteCount[vote] for vote in voteCount: if voteCount[vote] == mVote: logger.extraspecial("%5s: %s (%d)" % (vote, '#'*voteCount[vote], voteCount[vote]), False) else: logger.special("%5s: %s (%d)" % (vote, '#'*voteCount[vote], voteCount[vote]), False) logger.split() # Send results to clients for client in self.clients: client['connection'].sendResults(voteCount=voteCount, index=client['index'], vote=client['vote'])
def messageReceived(self, message): logger.debug("Message received from Collector") try: if message.action == 'set-index': index = message['index'] logger.info("Index received from Collector:") logger.verbose(index, False) self.factory.receivedIndex(index) elif message.action == 'announce-results': voteCount = message['vote-count'] myVote = message['your-vote'] mVote = None logger.split().split() logger.info("Election Results:") for vote in voteCount: if not mVote or voteCount[vote] > mVote: mVote = voteCount[vote] for vote in voteCount: if voteCount[vote] == mVote: logger.extraspecial("%5s: %s (%d)" % (vote, '#'*voteCount[vote], voteCount[vote]), False) else: logger.special("%5s: %s (%d)" % (vote, '#'*voteCount[vote], voteCount[vote]), False) logger.split() logger.info("Your Vote:") logger.special(" Index: %s" % myVote['index']) logger.special(" Vote: %s" % myVote['vote']) reactor.stop() except KeyError as e: return self.factory.fail("Malformed message received from Collector. No '%s' field" % e)