Search in sources :

Example 21 with LAORepository

use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.

the class LaoHandler method handleUpdateLao.

/**
 * Process an UpdateLao message.
 *
 * @param context the HandlerContext of the message
 * @param updateLao the message that was received
 */
public static void handleUpdateLao(HandlerContext context, UpdateLao updateLao) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Log.d(TAG, " Receive Update Lao Broadcast msg=" + updateLao);
    Lao lao = laoRepository.getLaoByChannel(channel);
    if (lao.getLastModified() > updateLao.getLastModified()) {
        // the current state we have is more up to date
        throw new DataHandlingException(updateLao, "The current Lao is more up to date than the update lao message");
    }
    WitnessMessage message;
    if (!updateLao.getName().equals(lao.getName())) {
        message = updateLaoNameWitnessMessage(messageId, updateLao, lao);
    } else if (!updateLao.getWitnesses().equals(lao.getWitnesses())) {
        message = updateLaoWitnessesWitnessMessage(messageId, updateLao, lao);
    } else {
        Log.d(TAG, "Cannot set the witness message title to update lao");
        throw new DataHandlingException(updateLao, "Cannot set the witness message title to update lao");
    }
    lao.updateWitnessMessage(messageId, message);
    if (!lao.getWitnesses().isEmpty()) {
        // We send a pending update only if there are already some witness that need to sign this
        // UpdateLao
        lao.getPendingUpdates().add(new PendingUpdate(updateLao.getLastModified(), messageId));
    }
    laoRepository.updateNodes(channel);
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) PendingUpdate(com.github.dedis.popstellar.model.objects.PendingUpdate) LAORepository(com.github.dedis.popstellar.repository.LAORepository) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 22 with LAORepository

use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.

the class RollCallHandler method handleCloseRollCall.

/**
 * Process a CloseRollCall message.
 *
 * @param context the HandlerContext of the message
 * @param closeRollCall the message that was received
 */
public static void handleCloseRollCall(HandlerContext context, CloseRollCall closeRollCall) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "handleCloseRollCall: " + channel);
    String updateId = closeRollCall.getUpdateId();
    String closes = closeRollCall.getCloses();
    Optional<RollCall> rollCallOptional = lao.getRollCall(closes);
    if (!rollCallOptional.isPresent()) {
        Log.w(TAG, "Cannot find roll call to close : " + closes);
        throw new InvalidDataException(closeRollCall, "close id", closes);
    }
    RollCall rollCall = rollCallOptional.get();
    rollCall.setEnd(closeRollCall.getClosedAt());
    rollCall.setId(updateId);
    rollCall.getAttendees().addAll(closeRollCall.getAttendees());
    rollCall.setState(EventState.CLOSED);
    lao.updateRollCall(closes, rollCall);
    lao.updateWitnessMessage(messageId, closeRollCallWitnessMessage(messageId, rollCall));
    // Subscribe to the social media channels
    try {
        PoPToken token = context.getKeyManager().getValidPoPToken(lao, rollCall);
        context.getMessageSender().subscribe(channel.subChannel("social").subChannel(token.getPublicKey().getEncoded())).subscribe();
    } catch (InvalidPoPTokenException e) {
        Log.i(TAG, "Received a close roll-call that you did not attend");
    } catch (KeyException e) {
        Log.e(TAG, "Could not retrieve your PoP Token to subscribe you to your social media channel", e);
    }
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) InvalidPoPTokenException(com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 23 with LAORepository

use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.

the class RollCallHandler method handleCreateRollCall.

/**
 * Process a CreateRollCall message.
 *
 * @param context the HandlerContext of the message
 * @param createRollCall the message that was received
 */
public static void handleCreateRollCall(HandlerContext context, CreateRollCall createRollCall) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "handleCreateRollCall: " + channel + " name " + createRollCall.getName());
    RollCall rollCall = new RollCall(createRollCall.getId());
    rollCall.setCreation(createRollCall.getCreation());
    rollCall.setState(EventState.CREATED);
    rollCall.setStart(createRollCall.getProposedStart());
    rollCall.setEnd(createRollCall.getProposedEnd());
    rollCall.setName(createRollCall.getName());
    rollCall.setLocation(createRollCall.getLocation());
    rollCall.setLocation(createRollCall.getLocation());
    rollCall.setDescription(createRollCall.getDescription().orElse(""));
    lao.updateRollCall(rollCall.getId(), rollCall);
    lao.updateWitnessMessage(messageId, createRollCallWitnessMessage(messageId, rollCall));
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 24 with LAORepository

use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.

the class RollCallHandler method handleOpenRollCall.

/**
 * Process an OpenRollCall message.
 *
 * @param context the HandlerContext of the message
 * @param openRollCall the message that was received
 */
public static void handleOpenRollCall(HandlerContext context, OpenRollCall openRollCall) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Log.d(TAG, "handleOpenRollCall: " + channel + " msg=" + openRollCall);
    String updateId = openRollCall.getUpdateId();
    String opens = openRollCall.getOpens();
    Optional<RollCall> rollCallOptional = lao.getRollCall(opens);
    if (!rollCallOptional.isPresent()) {
        Log.w(TAG, "Cannot find roll call to open : " + opens);
        throw new InvalidDataException(openRollCall, "open id", opens);
    }
    RollCall rollCall = rollCallOptional.get();
    rollCall.setStart(openRollCall.getOpenedAt());
    rollCall.setState(EventState.OPENED);
    // We might be opening a closed one
    rollCall.setEnd(0);
    rollCall.setId(updateId);
    lao.updateRollCall(opens, rollCall);
    lao.updateWitnessMessage(messageId, openRollCallWitnessMessage(messageId, rollCall));
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 25 with LAORepository

use of com.github.dedis.popstellar.repository.LAORepository in project popstellar by dedis.

the class HomeViewModel method launchLao.

/**
 * launchLao is invoked when the user tries to create a new LAO. The method creates a `CreateLAO`
 * message and publishes it to the root channel. It observers the response in the background and
 * switches to the home screen on success.
 */
public void launchLao() {
    String laoName = mLaoName.getValue();
    Log.d(TAG, "creating lao with name " + laoName);
    CreateLao createLao = new CreateLao(laoName, keyManager.getMainPublicKey());
    disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), Channel.ROOT, createLao).subscribe(() -> {
        Log.d(TAG, "got success result for create lao");
        // Create new LAO and add it to the LAORepository LAO lists
        Lao lao = new Lao(createLao.getId());
        laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
        laoRepository.setAllLaoSubject();
        // Send subscribe and catchup after creating a LAO
        networkManager.getMessageSender().subscribe(lao.getChannel()).subscribe();
        openHome();
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_lao)));
}
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) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) LAOState(com.github.dedis.popstellar.repository.LAOState) 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)

Aggregations

LAORepository (com.github.dedis.popstellar.repository.LAORepository)30 Channel (com.github.dedis.popstellar.model.objects.Channel)25 Lao (com.github.dedis.popstellar.model.objects.Lao)22 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)15 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)12 Before (org.junit.Before)11 Test (org.junit.Test)7 DataRegistryModule (com.github.dedis.popstellar.di.DataRegistryModule)6 JsonModule (com.github.dedis.popstellar.di.JsonModule)6 GenericMessage (com.github.dedis.popstellar.model.network.GenericMessage)6 Error (com.github.dedis.popstellar.model.network.answer.Error)6 ErrorCode (com.github.dedis.popstellar.model.network.answer.ErrorCode)6 Result (com.github.dedis.popstellar.model.network.answer.Result)6 ResultMessages (com.github.dedis.popstellar.model.network.answer.ResultMessages)6 Catchup (com.github.dedis.popstellar.model.network.method.Catchup)6 Publish (com.github.dedis.popstellar.model.network.method.Publish)6 Query (com.github.dedis.popstellar.model.network.method.Query)6 Subscribe (com.github.dedis.popstellar.model.network.method.Subscribe)6 Unsubscribe (com.github.dedis.popstellar.model.network.method.Unsubscribe)6 Data (com.github.dedis.popstellar.model.network.method.message.data.Data)6