Search in sources :

Example 1 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class HomeViewModel method onQRCodeDetected.

@Override
public void onQRCodeDetected(Barcode barcode) {
    Log.d(TAG, "Detected barcode with value: " + barcode.rawValue);
    ConnectToLao data;
    try {
        data = ConnectToLao.extractFrom(gson, barcode.rawValue);
    } catch (JsonParseException e) {
        Log.e(TAG, "Invalid QRCode data", e);
        Toast.makeText(getApplication().getApplicationContext(), "Invalid QRCode data", Toast.LENGTH_LONG).show();
        return;
    }
    networkManager.connect(data.server);
    Lao lao = new Lao(data.lao);
    disposables.add(networkManager.getMessageSender().subscribe(lao.getChannel()).doFinally(this::openHome).subscribe(() -> {
        Log.d(TAG, "subscribing to LAO with id " + lao.getId());
        // Create the new LAO and add it to the LAORepository LAO lists
        laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
        laoRepository.setAllLaoSubject();
        Log.d(TAG, "got success result for subscribe to lao");
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_subscribe_lao)));
    setConnectingLao(lao.getId());
    openConnecting();
}
Also used : MutableLiveData(androidx.lifecycle.MutableLiveData) JsonParseException(com.google.gson.JsonParseException) R(com.github.dedis.popstellar.R) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) ErrorUtils(com.github.dedis.popstellar.utility.error.ErrorUtils) SeedValidationException(com.github.dedis.popstellar.utility.error.keys.SeedValidationException) LiveDataReactiveStreams(androidx.lifecycle.LiveDataReactiveStreams) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Inject(javax.inject.Inject) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) Manifest(android.Manifest) GeneralSecurityException(java.security.GeneralSecurityException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Gson(com.google.gson.Gson) Toast(android.widget.Toast) AndroidViewModel(androidx.lifecycle.AndroidViewModel) Log(android.util.Log) LiveData(androidx.lifecycle.LiveData) Channel(com.github.dedis.popstellar.model.objects.Channel) BackpressureStrategy(io.reactivex.BackpressureStrategy) KeyManager(com.github.dedis.popstellar.utility.security.KeyManager) ActivityCompat(androidx.core.app.ActivityCompat) LAOState(com.github.dedis.popstellar.repository.LAOState) Barcode(com.google.android.gms.vision.barcode.Barcode) HiltViewModel(dagger.hilt.android.lifecycle.HiltViewModel) QRCodeScanningViewModel(com.github.dedis.popstellar.ui.qrcode.QRCodeScanningViewModel) ScanningAction(com.github.dedis.popstellar.ui.qrcode.ScanningAction) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Application(android.app.Application) SingleEvent(com.github.dedis.popstellar.SingleEvent) Wallet(com.github.dedis.popstellar.model.objects.Wallet) GlobalNetworkManager(com.github.dedis.popstellar.repository.remote.GlobalNetworkManager) CameraPermissionViewModel(com.github.dedis.popstellar.ui.qrcode.CameraPermissionViewModel) LAOState(com.github.dedis.popstellar.repository.LAOState) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) ConnectToLao(com.github.dedis.popstellar.model.qrcode.ConnectToLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) JsonParseException(com.google.gson.JsonParseException)

Example 2 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class ElectionHandler method handleElectionSetup.

/**
 * Process an ElectionSetup message.
 *
 * @param context the HandlerContext of the message
 * @param electionSetup the message that was received
 */
public static void handleElectionSetup(HandlerContext context, ElectionSetup electionSetup) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    if (channel.isLaoChannel()) {
        Lao lao = laoRepository.getLaoByChannel(channel);
        Log.d(TAG, "handleElectionSetup: channel " + channel + " name " + electionSetup.getName());
        Election election = new Election(lao.getId(), electionSetup.getCreation(), electionSetup.getName());
        election.setChannel(channel.subChannel(election.getId()));
        election.setElectionQuestions(electionSetup.getQuestions());
        election.setStart(electionSetup.getStartTime());
        election.setEnd(electionSetup.getEndTime());
        election.setEventState(OPENED);
        // Once the election is created, we subscribe to the election channel
        context.getMessageSender().subscribe(election.getChannel()).subscribe();
        Log.d(TAG, "election id " + election.getId());
        lao.updateElection(election.getId(), election);
        lao.updateWitnessMessage(messageId, electionSetupWitnessMessage(messageId, election));
    }
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 3 with Lao

use of com.github.dedis.popstellar.model.objects.Lao 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 4 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class ElectionHandler method handleElectionEnd.

/**
 * Process an ElectionEnd message.
 *
 * @param context the HandlerContext of the message
 * @param electionEnd the message that was received
 */
@SuppressWarnings("unused")
public static void handleElectionEnd(HandlerContext context, ElectionEnd electionEnd) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "handleElectionEnd: channel " + channel);
    Lao lao = laoRepository.getLaoByChannel(channel);
    Election election = laoRepository.getElectionByChannel(channel);
    election.setEventState(CLOSED);
    lao.updateElection(election.getId(), election);
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election)

Example 5 with Lao

use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.

the class ElectionHandler method handleElectionResult.

/**
 * Process an ElectionResult message.
 *
 * @param context the HandlerContext of the message
 * @param electionResult the message that was received
 */
public static void handleElectionResult(HandlerContext context, ElectionResult electionResult) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "handling election result");
    Lao lao = laoRepository.getLaoByChannel(channel);
    Election election = laoRepository.getElectionByChannel(channel);
    List<ElectionResultQuestion> resultsQuestions = electionResult.getElectionQuestionResults();
    Log.d(TAG, "size of resultsQuestions is " + resultsQuestions.size());
    if (resultsQuestions.isEmpty())
        throw new DataHandlingException(electionResult, "the questions results is empty");
    election.setResults(resultsQuestions);
    election.setEventState(RESULTS_READY);
    lao.updateElection(election.getId(), election);
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) ElectionResultQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionResultQuestion) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) Election(com.github.dedis.popstellar.model.objects.Election)

Aggregations

Lao (com.github.dedis.popstellar.model.objects.Lao)46 Channel (com.github.dedis.popstellar.model.objects.Channel)31 LAORepository (com.github.dedis.popstellar.repository.LAORepository)24 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)18 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)18 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)16 Disposable (io.reactivex.disposables.Disposable)14 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)12 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)11 Election (com.github.dedis.popstellar.model.objects.Election)10 RollCall (com.github.dedis.popstellar.model.objects.RollCall)10 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)9 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)9 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)8 PublicKey (com.github.dedis.popstellar.model.objects.security.PublicKey)8 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)8 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)7 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)6 ElectInstance (com.github.dedis.popstellar.model.objects.ElectInstance)6 LAOState (com.github.dedis.popstellar.repository.LAOState)6