Search in sources :

Example 1 with CreateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.

the class ElectionHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, IOException {
    lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
    lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
    laoRepository = new LAORepository();
    when(messageSender.subscribe(any())).then(args -> Completable.complete());
    laoRepository = new LAORepository();
    messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
    // Create one LAO
    lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
    lao.setLastModified(lao.getCreation());
    // Create one Roll Call and add it to the LAO
    rollCall = new RollCall(lao.getId(), Instant.now().getEpochSecond(), "roll call 1");
    lao.setRollCalls(new HashMap<String, RollCall>() {

        {
            put(rollCall.getId(), rollCall);
        }
    });
    // Create one Election and add it to the LAO
    election = new Election(lao.getId(), Instant.now().getEpochSecond(), "election 1");
    election.setStart(Instant.now().getEpochSecond());
    election.setEnd(Instant.now().getEpochSecond() + 20L);
    election.setChannel(lao.getChannel().subChannel(election.getId()));
    electionQuestion = new ElectionQuestion("question", "Plurality", false, Arrays.asList("a", "b"), election.getId());
    election.setElectionQuestions(Collections.singletonList(electionQuestion));
    lao.setElections(new HashMap<String, Election>() {

        {
            put(election.getId(), election);
        }
    });
    // Add the LAO to the LAORepository
    laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
    laoRepository.setAllLaoSubject();
    // Add the CreateLao message to the LAORepository
    MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LAORepository(com.github.dedis.popstellar.repository.LAORepository) ElectionQuestion(com.github.dedis.popstellar.model.network.method.message.data.election.ElectionQuestion) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Election(com.github.dedis.popstellar.model.objects.Election) Before(org.junit.Before)

Example 2 with CreateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.

the class LaoHandler method handleCreateLao.

/**
 * Process a CreateLao message.
 *
 * @param context the HandlerContext of the message
 * @param createLao the message that was received
 */
public static void handleCreateLao(HandlerContext context, CreateLao createLao) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "handleCreateLao: channel " + channel + ", msg=" + createLao);
    Lao lao = laoRepository.getLaoByChannel(channel);
    lao.setName(createLao.getName());
    lao.setCreation(createLao.getCreation());
    lao.setLastModified(createLao.getCreation());
    lao.setOrganizer(createLao.getOrganizer());
    lao.setId(createLao.getId());
    lao.setWitnesses(new HashSet<>(createLao.getWitnesses()));
    PublicKey publicKey = context.getKeyManager().getMainPublicKey();
    if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
        context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
    }
    laoRepository.updateNodes(channel);
}
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) 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)

Example 3 with CreateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao 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)

Example 4 with CreateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.

the class MessageHandler method notifyLaoUpdate.

/**
 * Keep the UI up to date by notifying all observers the updated LAO state.
 *
 * <p>The LAO is updated if the channel of the message is a LAO channel and the message is not a
 * WitnessSignatureMessage.
 *
 * <p>If a LAO has been created or modified then the LAO lists in the LAORepository are updated.
 *
 * @param laoRepository the repository to access the LAO lists
 * @param data the data received
 * @param channel the channel of the message received
 */
private void notifyLaoUpdate(LAORepository laoRepository, Data data, Channel channel) {
    if (!(data instanceof WitnessMessageSignature) && channel.isLaoChannel()) {
        LAOState laoState = laoRepository.getLaoById().get(channel.extractLaoId());
        // Trigger an onNext
        laoState.publish();
        if (data instanceof StateLao || data instanceof CreateLao) {
            laoRepository.setAllLaoSubject();
        }
    }
}
Also used : WitnessMessageSignature(com.github.dedis.popstellar.model.network.method.message.data.message.WitnessMessageSignature) LAOState(com.github.dedis.popstellar.repository.LAOState) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)

Example 5 with CreateLao

use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.

the class LaoHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, IOException {
    lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
    lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
    when(messageSender.subscribe(any())).then(args -> Completable.complete());
    laoRepository = new LAORepository();
    messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
    // Create one LAO and add it to the LAORepository
    lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
    lao.setLastModified(lao.getCreation());
    laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
    laoRepository.setAllLaoSubject();
    // Add the CreateLao message to the LAORepository
    createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) 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) Before(org.junit.Before)

Aggregations

CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)6 Lao (com.github.dedis.popstellar.model.objects.Lao)5 LAORepository (com.github.dedis.popstellar.repository.LAORepository)5 LAOState (com.github.dedis.popstellar.repository.LAOState)5 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)3 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)3 Before (org.junit.Before)3 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)2 Channel (com.github.dedis.popstellar.model.objects.Channel)2 RollCall (com.github.dedis.popstellar.model.objects.RollCall)2 Manifest (android.Manifest)1 Application (android.app.Application)1 PackageManager (android.content.pm.PackageManager)1 Log (android.util.Log)1 Toast (android.widget.Toast)1 NonNull (androidx.annotation.NonNull)1 ActivityCompat (androidx.core.app.ActivityCompat)1 AndroidViewModel (androidx.lifecycle.AndroidViewModel)1 LiveData (androidx.lifecycle.LiveData)1 LiveDataReactiveStreams (androidx.lifecycle.LiveDataReactiveStreams)1