Search in sources :

Example 1 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class SocialMediaViewModel method sendChirp.

/**
 * Send a chirp to your own channel.
 *
 * <p>Publish a MessageGeneral containing AddChirp data.
 *
 * @param text the text written in the chirp
 * @param parentId the id of the chirp to which you replied
 * @param timestamp the time at which you sent the chirp
 */
public void sendChirp(String text, @Nullable MessageID parentId, long timestamp) {
    Log.d(TAG, "Sending a chirp");
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    AddChirp addChirp = new AddChirp(text, parentId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, addChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, addChirp).subscribe(() -> Log.d(TAG, "sent chirp with messageId: " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_sending_chirp));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : AddChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.AddChirp) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 2 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class SocialMediaViewModel method deleteChirp.

public void deleteChirp(MessageID chirpId, long timestamp) {
    Log.d(TAG, "Deleting the chirp with id: " + chirpId);
    Lao lao = getCurrentLao();
    if (lao == null) {
        Log.e(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    DeleteChirp deleteChirp = new DeleteChirp(chirpId, timestamp);
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        Channel channel = lao.getChannel().subChannel(SOCIAL).subChannel(token.getPublicKey().getEncoded());
        Log.d(TAG, PUBLISH_MESSAGE);
        MessageGeneral msg = new MessageGeneral(token, deleteChirp, gson);
        Disposable disposable = networkManager.getMessageSender().publish(token, channel, deleteChirp).subscribe(() -> {
            Log.d(TAG, "Deleted chirp with messageId: " + msg.getMessageId());
            Toast.makeText(getApplication().getApplicationContext(), "Deleted chirp!", Toast.LENGTH_LONG).show();
        }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_delete_chirp));
        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) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Channel(com.github.dedis.popstellar.model.objects.Channel) Lao(com.github.dedis.popstellar.model.objects.Lao) DeleteChirp(com.github.dedis.popstellar.model.network.method.message.data.socialmedia.DeleteChirp) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 3 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ChirpHandlerTest method setup.

@Before
public void setup() throws GeneralSecurityException, DataHandlingException, 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);
    laoRepository.getLaoById().put(LAO.getId(), new LAOState(LAO));
    MessageGeneral createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
    messageHandler.handleMessage(laoRepository, messageSender, LAO.getChannel(), 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) Before(org.junit.Before)

Example 4 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ConsensusHandlerTest method handleConsensusElectAcceptTest.

// handle an electAccept from node3 for the elect of node2
// This test need be run after the elect message was handled, else the messageId would be invalid
private void handleConsensusElectAcceptTest() throws DataHandlingException {
    ConsensusElectAccept electAccept = new ConsensusElectAccept(INSTANCE_ID, messageId, true);
    MessageGeneral electAcceptMsg = getMsg(NODE_3_KEY, electAccept);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electAcceptMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    Map<PublicKey, MessageID> acceptorsToMessageId = electInstance.getAcceptorsToMessageId();
    assertEquals(1, acceptorsToMessageId.size());
    assertEquals(electAcceptMsg.getMessageId(), acceptorsToMessageId.get(NODE_3));
    assertEquals(3, lao.getNodes().size());
    ConsensusNode organizer = lao.getNode(ORGANIZER);
    ConsensusNode node2 = lao.getNode(NODE_2);
    ConsensusNode node3 = lao.getNode(NODE_3);
    assertNotNull(organizer);
    assertNotNull(node2);
    assertNotNull(node3);
    Set<MessageID> organizerAcceptedMsg = organizer.getAcceptedMessageIds();
    Set<MessageID> node2AcceptedMsg = node2.getAcceptedMessageIds();
    Set<MessageID> node3AcceptedMsg = node3.getAcceptedMessageIds();
    assertTrue(organizerAcceptedMsg.isEmpty());
    assertTrue(node2AcceptedMsg.isEmpty());
    assertEquals(Sets.newSet(electMsg.getMessageId()), node3AcceptedMsg);
}
Also used : ConsensusElectAccept(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept) ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 5 with MessageGeneral

use of com.github.dedis.popstellar.model.network.method.message.MessageGeneral in project popstellar by dedis.

the class ConsensusHandlerTest method handleConsensusFailure.

@Test
public void handleConsensusFailure() throws DataHandlingException {
    // handle an elect from node2 then handle a failure for this elect
    // the state of the node2 for this instanceId should be FAILED
    ConsensusFailure failure = new ConsensusFailure(INSTANCE_ID, messageId, CREATION_TIME);
    MessageGeneral failureMsg = getMsg(ORGANIZER_KEY, failure);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, electMsg);
    messageHandler.handleMessage(laoRepository, messageSender, CONSENSUS_CHANNEL, failureMsg);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(electMsg.getMessageId());
    assertTrue(electInstanceOpt.isPresent());
    ElectInstance electInstance = electInstanceOpt.get();
    assertEquals(FAILED, electInstance.getState());
    ConsensusNode node2 = lao.getNode(NODE_2);
    assertNotNull(node2);
    assertEquals(FAILED, node2.getState(INSTANCE_ID));
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) ConsensusFailure(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusFailure) ConsensusNode(com.github.dedis.popstellar.model.objects.ConsensusNode) Test(org.junit.Test)

Aggregations

MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)33 Test (org.junit.Test)18 Lao (com.github.dedis.popstellar.model.objects.Lao)10 Channel (com.github.dedis.popstellar.model.objects.Channel)8 LAORepository (com.github.dedis.popstellar.repository.LAORepository)8 Disposable (io.reactivex.disposables.Disposable)7 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)6 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)6 RollCall (com.github.dedis.popstellar.model.objects.RollCall)6 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)6 Before (org.junit.Before)6 ConsensusElectAccept (com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElectAccept)5 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)5 Election (com.github.dedis.popstellar.model.objects.Election)5 WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)5 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)5 LAOState (com.github.dedis.popstellar.repository.LAOState)5 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)4 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)4 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)4