use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao 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.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.
the class LaoHandler method handleCreateLao.
/**
* Process a CreateLao message.
*
* @param context the HandlerContext of the message
* @param createLao the message that was received
*/
public static void handleCreateLao(HandlerContext context, CreateLao createLao) {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
Log.d(TAG, "handleCreateLao: channel " + channel + ", msg=" + createLao);
Lao lao = laoRepository.getLaoByChannel(channel);
lao.setName(createLao.getName());
lao.setCreation(createLao.getCreation());
lao.setLastModified(createLao.getCreation());
lao.setOrganizer(createLao.getOrganizer());
lao.setId(createLao.getId());
lao.setWitnesses(new HashSet<>(createLao.getWitnesses()));
PublicKey publicKey = context.getKeyManager().getMainPublicKey();
if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
}
laoRepository.updateNodes(channel);
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao 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.model.network.method.message.data.lao.CreateLao 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();
}
}
}
use of com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao in project popstellar by dedis.
the class LaoHandlerTest method setup.
@Before
public void setup() throws GeneralSecurityException, 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);
// Create one LAO and add it to the LAORepository
lao = new Lao(CREATE_LAO.getName(), CREATE_LAO.getOrganizer(), CREATE_LAO.getCreation());
lao.setLastModified(lao.getCreation());
laoRepository.getLaoById().put(lao.getId(), new LAOState(lao));
laoRepository.setAllLaoSubject();
// Add the CreateLao message to the LAORepository
createLaoMessage = new MessageGeneral(SENDER_KEY, CREATE_LAO, GSON);
laoRepository.getMessageById().put(createLaoMessage.getMessageId(), createLaoMessage);
}
Aggregations