use of com.github.dedis.popstellar.model.network.method.message.data.election.CastVote in project popstellar by dedis.
the class ElectionHandler method handleCastVote.
/**
* Process a CastVote message.
*
* @param context the HandlerContext of the message
* @param castVote the message that was received
*/
public static void handleCastVote(HandlerContext context, CastVote castVote) {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
MessageID messageId = context.getMessageId();
PublicKey senderPk = context.getSenderPk();
Log.d(TAG, "handleCastVote: channel " + channel);
Lao lao = laoRepository.getLaoByChannel(channel);
Election election = laoRepository.getElectionByChannel(channel);
// Verify the vote was created before the end of the election or the election is not closed yet
if (election.getEndTimestamp() >= castVote.getCreation() || election.getState() != CLOSED) {
// Retrieve previous cast vote message stored for the given sender
Optional<MessageID> previousMessageIdOption = election.getMessageMap().entrySet().stream().filter(entry -> senderPk.equals(entry.getValue())).map(Map.Entry::getKey).findFirst();
// Retrieve the creation time of the previous cast vote, if doesn't exist replace with min
// value
long previousMessageCreation = previousMessageIdOption.map(s -> laoRepository.getMessageById().get(s)).map(MessageGeneral::getData).map(CastVote.class::cast).map(CastVote::getCreation).orElse(Long.MIN_VALUE);
// Verify the current cast vote message is the last one received
if (previousMessageCreation <= castVote.getCreation()) {
election.putVotesBySender(senderPk, castVote.getVotes());
election.putSenderByMessageId(senderPk, messageId);
lao.updateElection(election.getId(), election);
}
}
}
use of com.github.dedis.popstellar.model.network.method.message.data.election.CastVote in project popstellar by dedis.
the class LaoDetailViewModel method sendVote.
/**
* Sends a ElectionCastVotes message .
*
* <p>Publish a GeneralMessage containing ElectionCastVotes data.
*
* @param votes the corresponding votes for that election
*/
public void sendVote(List<ElectionVote> votes) {
Election election = mCurrentElection.getValue();
if (election == null) {
Log.d(TAG, "failed to retrieve current election");
return;
}
Log.d(TAG, "sending a new vote in election : " + election + " with election start time" + election.getStartTimestamp());
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
try {
PoPToken token = keyManager.getValidPoPToken(lao);
CastVote castVote = new CastVote(votes, election.getId(), lao.getId());
Channel electionChannel = election.getChannel();
Log.d(TAG, PUBLISH_MESSAGE);
Disposable disposable = networkManager.getMessageSender().publish(token, electionChannel, castVote).doFinally(this::openLaoDetail).subscribe(() -> {
Log.d(TAG, "sent a vote successfully");
// Toast ? + send back to election screen or details screen ?
Toast.makeText(getApplication(), "vote successfully sent !", Toast.LENGTH_LONG).show();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_send_vote));
disposables.add(disposable);
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
}
Aggregations