@Override
  public void handleAccepted(PaxosMessage proposal) {
    ensureSize(chosen, proposal.getInstanceNum() + 1); // Prevent NPE

    if (chosen.get(proposal.getInstanceNum()) != null)
      return; // Something has already been chosen so I dont care anymore.

    List<PaxosMessage> acceptedVotes = proposalAcceptedVotes.get(proposal.getInstanceNum());
    if (acceptedVotes == null) {
      acceptedVotes = new ArrayList<PaxosMessage>();
      acceptedVotes.add(proposal);
      proposalAcceptedVotes.put(proposal.getInstanceNum(), acceptedVotes);
    } else {
      for (PaxosMessage acceptedVote : acceptedVotes) {
        if (proposal.getProposerId() == acceptedVote.getProposerId())
          return; // This person is reaffirming their acceptance of this instances proposal.
      }
      acceptedVotes.add(proposal);
      proposalAcceptedVotes.put(proposal.getInstanceNum(), acceptedVotes);
    }
    // handle being chosen
    if (proposalAcceptedVotes.get(proposal.getInstanceNum()).size() >= serverSet.getQuorumSize()) {
      onChosen(proposal);
    }
  }
 private void sendMessage(Serializable message, short serverId) {
   try {
     if (serverId == id) { // Sending to myself.
       messageReceived(null, message);
     } else {
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       ObjectSerializationOutputStream objectOutputStream =
           new ObjectSerializationOutputStream(byteArrayOutputStream);
       objectOutputStream.writeObject(message);
       objectOutputStream.flush();
       ByteBuffer payload = ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
       channel.send(payload, serverSet.getServer(serverId));
     }
   } catch (IOException e) {
     System.out.println("Failed to send message");
     e
         .printStackTrace(); // To change body of catch statement use File | Settings | File
                             // Templates.
   } catch (Exception e) {
     e
         .printStackTrace(); // To change body of catch statement use File | Settings | File
                             // Templates.
   }
 }
 private short getLeader() {
   return (short) (preparedFor % serverSet.getNumServers());
 }
 private void sendAllMessage(PaxosMessage message) {
   for (short i = 0; i < serverSet.getNumServers(); i++) {
     sendMessage(message, i);
   }
 }
 private boolean nodeIsLeader() {
   return id == getLeader() && prepareResponseFrom.size() >= serverSet.getQuorumSize();
 }