use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall in project popstellar by dedis.
the class RollCallHandlerTest method testHandleOpenRollCall.
@Test
public void testHandleOpenRollCall() throws DataHandlingException {
// Create the open Roll Call message
OpenRollCall openRollCall = new OpenRollCall(CREATE_LAO.getId(), rollCall.getId(), rollCall.getStart(), EventState.CREATED);
MessageGeneral message = new MessageGeneral(SENDER_KEY, openRollCall, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the Roll Call is present with state OPENED and the correct ID
Optional<RollCall> rollCallOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getRollCall(openRollCall.getUpdateId());
assertTrue(rollCallOpt.isPresent());
assertEquals(EventState.OPENED, rollCallOpt.get().getState());
assertEquals(openRollCall.getUpdateId(), rollCallOpt.get().getId());
// Check the WitnessMessage has been created
Optional<WitnessMessage> witnessMessage = laoRepository.getLaoByChannel(LAO_CHANNEL).getWitnessMessage(message.getMessageId());
assertTrue(witnessMessage.isPresent());
// Check the Witness message contains the expected title and description
WitnessMessage expectedMessage = openRollCallWitnessMessage(message.getMessageId(), rollCallOpt.get());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall in project popstellar by dedis.
the class RollCallHandler method handleOpenRollCall.
/**
* Process an OpenRollCall message.
*
* @param context the HandlerContext of the message
* @param openRollCall the message that was received
*/
public static void handleOpenRollCall(HandlerContext context, OpenRollCall openRollCall) throws DataHandlingException {
LAORepository laoRepository = context.getLaoRepository();
Channel channel = context.getChannel();
MessageID messageId = context.getMessageId();
Lao lao = laoRepository.getLaoByChannel(channel);
Log.d(TAG, "handleOpenRollCall: " + channel + " msg=" + openRollCall);
String updateId = openRollCall.getUpdateId();
String opens = openRollCall.getOpens();
Optional<RollCall> rollCallOptional = lao.getRollCall(opens);
if (!rollCallOptional.isPresent()) {
Log.w(TAG, "Cannot find roll call to open : " + opens);
throw new InvalidDataException(openRollCall, "open id", opens);
}
RollCall rollCall = rollCallOptional.get();
rollCall.setStart(openRollCall.getOpenedAt());
rollCall.setState(EventState.OPENED);
// We might be opening a closed one
rollCall.setEnd(0);
rollCall.setId(updateId);
lao.updateRollCall(opens, rollCall);
lao.updateWitnessMessage(messageId, openRollCallWitnessMessage(messageId, rollCall));
}
use of com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall in project popstellar by dedis.
the class LaoDetailViewModel method openRollCall.
/**
* Opens a roll call event.
*
* <p>Publish a GeneralMessage containing OpenRollCall data.
*
* @param id the roll call id to open
*/
public void openRollCall(String id) {
Log.d(TAG, "call openRollCall");
Lao lao = getCurrentLaoValue();
if (lao == null) {
Log.d(TAG, LAO_FAILURE_MESSAGE);
return;
}
long openedAt = Instant.now().getEpochSecond();
Channel channel = lao.getChannel();
String laoId = lao.getId();
Optional<RollCall> optRollCall = lao.getRollCall(id);
if (!optRollCall.isPresent()) {
Log.d(TAG, "failed to retrieve roll call with id " + id + "laoID: " + laoId);
return;
}
RollCall rollCall = optRollCall.get();
OpenRollCall openRollCall = new OpenRollCall(laoId, id, openedAt, rollCall.getState());
attendees = new HashSet<>(rollCall.getAttendees());
try {
attendees.add(keyManager.getPoPToken(lao, rollCall).getPublicKey());
} catch (KeyException e) {
ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
}
Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, openRollCall).subscribe(() -> {
Log.d(TAG, "opened the roll call");
currentRollCallId = openRollCall.getUpdateId();
scanningAction = ScanningAction.ADD_ROLL_CALL_ATTENDEE;
openScanning();
}, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_open_rollcall));
disposables.add(disposable);
}
Aggregations