use of com.github.dedis.popstellar.model.objects.RollCall 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.objects.RollCall 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);
}
use of com.github.dedis.popstellar.model.objects.RollCall in project popstellar by dedis.
the class RollCallTokenFragment method onCreateView.
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRollCallTokenFragmentBinding = RollCallTokenFragmentBinding.inflate(inflater, container, false);
mLaoDetailViewModel = LaoDetailActivity.obtainViewModel(requireActivity());
String rollCallId = requireArguments().getString(EXTRA_ID);
Optional<RollCall> optRollCall = mLaoDetailViewModel.getCurrentLao().getValue().getRollCall(rollCallId);
if (!optRollCall.isPresent()) {
Log.d(TAG, "failed to retrieve roll call with id " + rollCallId);
mLaoDetailViewModel.openLaoWallet();
} else {
rollCall = optRollCall.get();
}
String firstLaoId = mLaoDetailViewModel.getCurrentLaoValue().getId();
String pk = "";
Log.d(TAG, "rollcall: " + rollCallId);
try {
PoPToken token = wallet.generatePoPToken(firstLaoId, rollCall.getPersistentId());
pk = token.getPublicKey().getEncoded();
} catch (KeyException e) {
Log.d(TAG, "failed to retrieve token from wallet", e);
mLaoDetailViewModel.openLaoWallet();
}
mRollCallTokenFragmentBinding.rollcallName.setText("Roll Call: " + rollCall.getName());
mRollCallTokenFragmentBinding.publicKey.setText("Public key:\n" + pk);
Bitmap myBitmap = QRCode.from(pk).bitmap();
mRollCallTokenFragmentBinding.pkQrCode.setImageBitmap(myBitmap);
mRollCallTokenFragmentBinding.setLifecycleOwner(getActivity());
return mRollCallTokenFragmentBinding.getRoot();
}
use of com.github.dedis.popstellar.model.objects.RollCall in project popstellar by dedis.
the class RollCallHandlerTest method testHandleCreateRollCall.
@Test
public void testHandleCreateRollCall() throws DataHandlingException {
// Create the create Roll Call message
CreateRollCall createRollCall = new CreateRollCall("roll call 2", rollCall.getCreation(), rollCall.getStart(), rollCall.getEnd(), rollCall.getLocation(), rollCall.getDescription(), CREATE_LAO.getId());
MessageGeneral message = new MessageGeneral(SENDER_KEY, createRollCall, GSON);
// Call the message handler
messageHandler.handleMessage(laoRepository, messageSender, LAO_CHANNEL, message);
// Check the new Roll Call is present with state CREATED and the correct ID
Optional<RollCall> rollCallOpt = laoRepository.getLaoByChannel(LAO_CHANNEL).getRollCall(createRollCall.getId());
assertTrue(rollCallOpt.isPresent());
assertEquals(EventState.CREATED, rollCallOpt.get().getState());
assertEquals(createRollCall.getId(), 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 = createRollCallWitnessMessage(message.getMessageId(), rollCallOpt.get());
assertEquals(expectedMessage.getTitle(), witnessMessage.get().getTitle());
assertEquals(expectedMessage.getDescription(), witnessMessage.get().getDescription());
}
use of com.github.dedis.popstellar.model.objects.RollCall in project popstellar by dedis.
the class RollCallHandlerTest method setup.
@Before
public void setup() throws GeneralSecurityException, IOException, KeyException {
lenient().when(keyManager.getMainKeyPair()).thenReturn(SENDER_KEY);
lenient().when(keyManager.getMainPublicKey()).thenReturn(SENDER);
lenient().when(keyManager.getValidPoPToken(any(), any())).thenReturn(POP_TOKEN);
when(messageSender.subscribe(any())).then(args -> Completable.complete());
laoRepository = new LAORepository();
messageHandler = new MessageHandler(DataRegistryModule.provideDataRegistry(), keyManager);
// Create one LAO
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");
rollCall.setLocation("EPFL");
lao.setRollCalls(new HashMap<String, RollCall>() {
{
put(rollCall.getId(), rollCall);
}
});
// 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);
}
Aggregations