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);
}
}
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);
}
}
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);
}
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);
}
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));
}
Aggregations