Search in sources :

Example 6 with RollCall

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));
}
Also used : Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidDataException(com.github.dedis.popstellar.utility.error.InvalidDataException) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID)

Example 7 with 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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 8 with RollCall

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();
}
Also used : PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Bitmap(android.graphics.Bitmap) RollCall(com.github.dedis.popstellar.model.objects.RollCall) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException) Nullable(androidx.annotation.Nullable)

Example 9 with RollCall

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());
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) WitnessMessage(com.github.dedis.popstellar.model.objects.WitnessMessage) RollCallHandler.closeRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.closeRollCallWitnessMessage) RollCallHandler.createRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.createRollCallWitnessMessage) RollCallHandler.openRollCallWitnessMessage(com.github.dedis.popstellar.utility.handler.data.RollCallHandler.openRollCallWitnessMessage) Test(org.junit.Test)

Example 10 with RollCall

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);
}
Also used : LAOState(com.github.dedis.popstellar.repository.LAOState) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) LAORepository(com.github.dedis.popstellar.repository.LAORepository) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) OpenRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall) RollCall(com.github.dedis.popstellar.model.objects.RollCall) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) Before(org.junit.Before)

Aggregations

RollCall (com.github.dedis.popstellar.model.objects.RollCall)13 CloseRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall)8 CreateRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)8 OpenRollCall (com.github.dedis.popstellar.model.network.method.message.data.rollcall.OpenRollCall)8 Lao (com.github.dedis.popstellar.model.objects.Lao)8 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)5 LAORepository (com.github.dedis.popstellar.repository.LAORepository)5 Test (org.junit.Test)5 Channel (com.github.dedis.popstellar.model.objects.Channel)4 PoPToken (com.github.dedis.popstellar.model.objects.security.PoPToken)4 WitnessMessage (com.github.dedis.popstellar.model.objects.WitnessMessage)3 MessageID (com.github.dedis.popstellar.model.objects.security.MessageID)3 KeyException (com.github.dedis.popstellar.utility.error.keys.KeyException)3 RollCallHandler.closeRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.closeRollCallWitnessMessage)3 RollCallHandler.createRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.createRollCallWitnessMessage)3 RollCallHandler.openRollCallWitnessMessage (com.github.dedis.popstellar.utility.handler.data.RollCallHandler.openRollCallWitnessMessage)3 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)2 LAOState (com.github.dedis.popstellar.repository.LAOState)2 InvalidDataException (com.github.dedis.popstellar.utility.error.InvalidDataException)2 InvalidPoPTokenException (com.github.dedis.popstellar.utility.error.keys.InvalidPoPTokenException)2