use of com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao in project popstellar by dedis.
the class LaoDetailViewModel method updateLaoName.
/**
* Method to update the name of a Lao by sending an updateLao msg and a stateLao msg to the
* backend
*/
public void updateLaoName() {
Log.d(TAG, "Updating lao name to " + mLaoName.getValue());
Lao lao = getCurrentLaoValue();
Channel channel = lao.getChannel();
KeyPair mainKey = keyManager.getMainKeyPair();
long now = Instant.now().getEpochSecond();
UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), mLaoName.getValue(), now, lao.getWitnesses());
MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
Log.d(TAG, "updated lao name");
dispatchLaoUpdate("lao name", updateLao, lao, channel, msg);
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
disposables.add(disposable);
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao in project popstellar by dedis.
the class LaoDetailViewModel method updateLaoWitnesses.
/**
* Method to update the list of witnesses of a Lao by sending an updateLao msg and a stateLao msg
* to the backend
*/
public void updateLaoWitnesses() {
Log.d(TAG, "Updating lao witnesses ");
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
Channel channel = lao.getChannel();
KeyPair mainKey = keyManager.getMainKeyPair();
long now = Instant.now().getEpochSecond();
UpdateLao updateLao = new UpdateLao(mainKey.getPublicKey(), lao.getCreation(), lao.getName(), now, witnesses);
MessageGeneral msg = new MessageGeneral(mainKey, updateLao, gson);
Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> {
Log.d(TAG, "updated lao witnesses");
dispatchLaoUpdate("lao state with new witnesses", updateLao, lao, channel, msg);
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_update_lao));
disposables.add(disposable);
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao 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);
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao in project popstellar by dedis.
the class LaoDetailViewModel method dispatchLaoUpdate.
/**
* Helper method for updateLaoWitnesses and updateLaoName to send a stateLao message
*/
private void dispatchLaoUpdate(String desc, UpdateLao updateLao, Lao lao, Channel channel, MessageGeneral msg) {
StateLao stateLao = new StateLao(updateLao.getId(), updateLao.getName(), lao.getCreation(), updateLao.getLastModified(), lao.getOrganizer(), msg.getMessageId(), updateLao.getWitnesses(), new ArrayList<>());
disposables.add(networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, stateLao).subscribe(() -> Log.d(TAG, "updated " + desc), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_state_lao)));
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao in project popstellar by dedis.
the class LaoHandlerTest method testHandleUpdateLao.
@Test
public void testHandleUpdateLao() throws DataHandlingException {
// Create the update LAO message
UpdateLao updateLao = new UpdateLao(SENDER, CREATE_LAO.getCreation(), "new name", Instant.now().getEpochSecond(), new HashSet<>());
MessageGeneral message = new MessageGeneral(SENDER_KEY, updateLao, GSON);
// Create the expected WitnessMessage
WitnessMessage expectedMessage = updateLaoNameWitnessMessage(message.getMessageId(), updateLao, lao);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the WitnessMessage has been created
Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
assertTrue(witnessMessage.isPresent());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
Aggregations