use of com.github.dedis.popstellar.repository.LAOState in project popstellar by dedis.
the class HomeViewModel method onQRCodeDetected.
@Override
public void onQRCodeDetected(Barcode barcode) {
Log.d(TAG, "Detected barcode with value: " + barcode.rawValue);
ConnectToLao data;
try {
data = ConnectToLao.extractFrom(gson, barcode.rawValue);
} catch (JsonParseException e) {
Log.e(TAG, "Invalid QRCode data", e);
Toast.makeText(getApplication().getApplicationContext(), "Invalid QRCode data", Toast.LENGTH_LONG).show();
return;
}
networkManager.connect(data.server);
Lao lao = new Lao(data.lao);
disposables.add(networkManager.getMessageSender().subscribe(lao.getChannel()).doFinally(this::openHome).subscribe(() -> {
Log.d(TAG, "subscribing to LAO with id " + lao.getId());
// Create the new LAO and add it to the LAORepository LAO lists
laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
laoRepository.setAllLaoSubject();
Log.d(TAG, "got success result for subscribe to lao");
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_subscribe_lao)));
setConnectingLao(lao.getId());
openConnecting();
}
use of com.github.dedis.popstellar.repository.LAOState 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.repository.LAOState 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);
}
use of com.github.dedis.popstellar.repository.LAOState 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)));
}
use of com.github.dedis.popstellar.repository.LAOState 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();
}
}
}
Aggregations