Search in sources :

Example 1 with CastVote

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);
        }
    }
}
Also used : PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) Map(java.util.Map) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 2 with CastVote

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);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Channel(com.github.dedis.popstellar.model.objects.Channel) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) Election(com.github.dedis.popstellar.model.objects.Election) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Aggregations

CastVote (com.github.dedis.popstellar.model.network.method.message.data.election.CastVote)2 Channel (com.github.dedis.popstellar.model.objects.Channel)2 Election (com.github.dedis.popstellar.model.objects.Election)2 Lao (com.github.dedis.popstellar.model.objects.Lao)2 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)1 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)1 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)1 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)1 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)1 LAORepository (com.github.dedis.popstellar.repository.LAORepository)1 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)1 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)1 Disposable (io.reactivex.disposables.Disposable)1 Map (java.util.Map)1