use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.
the class LaoHandler method handleStateLao.
/**
* Process a StateLao message.
*
* @param context the HandlerContext of the message
* @param stateLao the message that was received
*/
public static void handleStateLao(HandlerContext context, StateLao stateLao) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Log.d(TAG, "Receive State Lao Broadcast msg=" + stateLao);
Lao lao = laoRepository.getLaoByChannel(channel);
Log.d(TAG, "Receive State Lao Broadcast " + stateLao.getName());
if (!laoRepository.getMessageById().containsKey(stateLao.getModificationId())) {
Log.d(TAG, "Can't find modification id : " + stateLao.getModificationId());
// queue it if we haven't received the update message yet
throw new InvalidMessageIdException(stateLao, stateLao.getModificationId());
}
Log.d(TAG, "Verifying signatures");
for (PublicKeySignaturePair pair : stateLao.getModificationSignatures()) {
if (!pair.getWitness().verify(pair.getSignature(), stateLao.getModificationId())) {
throw new InvalidSignatureException(stateLao, pair.getSignature());
}
}
Log.d(TAG, "Success to verify state lao signatures");
// TODO: verify if lao/state_lao is consistent with the lao/update message
lao.setId(stateLao.getId());
lao.setWitnesses(stateLao.getWitnesses());
lao.setName(stateLao.getName());
lao.setLastModified(stateLao.getLastModified());
lao.setModificationId(stateLao.getModificationId());
PublicKey publicKey = context.getKeyManager().getMainPublicKey();
if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
}
// Now we're going to remove all pending updates which came prior to this state lao
long targetTime = stateLao.getLastModified();
lao.getPendingUpdates().removeIf(pendingUpdate -> pendingUpdate.getModificationTime() <= targetTime);
laoRepository.updateNodes(channel);
}
use of com.github.dedis.popstellar.model.objects.Lao in project popstellar by dedis.
the class SocialMediaActivity method onOptionsItemSelected.
@SuppressLint("NonConstantResourceId")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Retrieve the index of the lao within the list
int i = item.getItemId();
List<Lao> laos = mViewModel.getLAOs().getValue();
if (laos != null && i >= 0 && i < laos.size()) {
Lao lao = laos.get(i);
mViewModel.setLaoId(lao.getId());
mViewModel.setLaoName(lao.getName());
return true;
}
return super.onOptionsItemSelected(item);
}
use of com.github.dedis.popstellar.model.objects.Lao 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.objects.Lao 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.objects.Lao in project popstellar by dedis.
the class ConsensusHandler method handleElectAccept.
public static void handleElectAccept(HandlerContext context, ConsensusElectAccept consensusElectAccept) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
MessageID messageId = context.getMessageId();
PublicKey senderPk = context.getSenderPk();
Lao lao = laoRepository.getLaoByChannel(channel);
Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(consensusElectAccept.getMessageId());
if (!electInstanceOpt.isPresent()) {
Log.w(TAG, "elect_accept for invalid messageId : " + consensusElectAccept.getMessageId());
throw new InvalidMessageIdException(consensusElectAccept, consensusElectAccept.getMessageId());
}
ElectInstance electInstance = electInstanceOpt.get();
electInstance.addElectAccept(senderPk, messageId, consensusElectAccept);
lao.updateElectInstance(electInstance);
laoRepository.updateNodes(lao.getChannel());
}
Aggregations