Search in sources :

Example 21 with Channel

use of com.github.dedis.popstellar.model.objects.Channel in project popstellar by dedis.

the class LaoDetailViewModel method closeRollCall.

/**
 * Closes the roll call event currently open
 *
 * <p>Publish a GeneralMessage containing CloseRollCall data.
 */
public void closeRollCall(int nextFragment) {
    Log.d(TAG, "call closeRollCall");
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    long end = Instant.now().getEpochSecond();
    Channel channel = lao.getChannel();
    CloseRollCall closeRollCall = new CloseRollCall(lao.getId(), currentRollCallId, end, new ArrayList<>(attendees));
    Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, closeRollCall).subscribe(() -> {
        Log.d(TAG, "closed the roll call");
        currentRollCallId = "";
        attendees.clear();
        mCloseRollCallEvent.setValue(new SingleEvent<>(nextFragment));
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_close_rollcall));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) CloseRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CloseRollCall) 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)

Example 22 with Channel

use of com.github.dedis.popstellar.model.objects.Channel in project popstellar by dedis.

the class LaoDetailViewModel method sendVote.

/**
 * Sends a ElectionCastVotes message .
 *
 * <p>Publish a GeneralMessage containing ElectionCastVotes data.
 *
 * @param votes the corresponding votes for that election
 */
public void sendVote(List<ElectionVote> votes) {
    Election election = mCurrentElection.getValue();
    if (election == null) {
        Log.d(TAG, "failed to retrieve current election");
        return;
    }
    Log.d(TAG, "sending a new vote in election : " + election + " with election start time" + election.getStartTimestamp());
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    try {
        PoPToken token = keyManager.getValidPoPToken(lao);
        CastVote castVote = new CastVote(votes, election.getId(), lao.getId());
        Channel electionChannel = election.getChannel();
        Log.d(TAG, PUBLISH_MESSAGE);
        Disposable disposable = networkManager.getMessageSender().publish(token, electionChannel, castVote).doFinally(this::openLaoDetail).subscribe(() -> {
            Log.d(TAG, "sent a vote successfully");
            // Toast ? + send back to election screen or details screen ?
            Toast.makeText(getApplication(), "vote successfully sent !", Toast.LENGTH_LONG).show();
        }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_send_vote));
        disposables.add(disposable);
    } catch (KeyException e) {
        ErrorUtils.logAndShow(getApplication(), TAG, e, R.string.error_retrieve_own_token);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) PoPToken(com.github.dedis.popstellar.model.objects.security.PoPToken) Channel(com.github.dedis.popstellar.model.objects.Channel) CastVote(com.github.dedis.popstellar.model.network.method.message.data.election.CastVote) 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) Election(com.github.dedis.popstellar.model.objects.Election) KeyException(com.github.dedis.popstellar.utility.error.keys.KeyException)

Example 23 with Channel

use of com.github.dedis.popstellar.model.objects.Channel in project popstellar by dedis.

the class LAONetworkManager method subscribe.

@Override
public Completable subscribe(Channel channel) {
    Log.d(TAG, "sending a subscribe on the channel " + channel);
    Subscribe subscribe = new Subscribe(channel, requestCounter.incrementAndGet());
    return request(subscribe).doOnSuccess(answer -> subscribedChannels.add(channel)).doAfterSuccess(answer -> catchup(channel).subscribe()).ignoreElement();
}
Also used : Publish(com.github.dedis.popstellar.model.network.method.Publish) Answer(com.github.dedis.popstellar.model.network.answer.Answer) Completable(io.reactivex.Completable) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) Single(io.reactivex.Single) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) WebSocket(com.tinder.scarlet.WebSocket) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) Error(com.github.dedis.popstellar.model.network.answer.Error) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Gson(com.google.gson.Gson) Broadcast(com.github.dedis.popstellar.model.network.method.Broadcast) Observable(io.reactivex.Observable) LinkedList(java.util.LinkedList) Log(android.util.Log) Subject(io.reactivex.subjects.Subject) Channel(com.github.dedis.popstellar.model.objects.Channel) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Query(com.github.dedis.popstellar.model.network.method.Query) SchedulerProvider(com.github.dedis.popstellar.utility.scheduler.SchedulerProvider) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) PublishSubject(io.reactivex.subjects.PublishSubject) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe)

Example 24 with Channel

use of com.github.dedis.popstellar.model.objects.Channel in project popstellar by dedis.

the class LAONetworkManager method unsubscribe.

@Override
public Completable unsubscribe(Channel channel) {
    Log.d(TAG, "sending an unsubscribe on the channel " + channel);
    Unsubscribe unsubscribe = new Unsubscribe(channel, requestCounter.incrementAndGet());
    return request(unsubscribe).doOnSuccess(answer -> subscribedChannels.remove(channel)).ignoreElement();
}
Also used : Publish(com.github.dedis.popstellar.model.network.method.Publish) Answer(com.github.dedis.popstellar.model.network.answer.Answer) Completable(io.reactivex.Completable) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) Single(io.reactivex.Single) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataHandlingException(com.github.dedis.popstellar.utility.error.DataHandlingException) WebSocket(com.tinder.scarlet.WebSocket) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) Error(com.github.dedis.popstellar.model.network.answer.Error) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Gson(com.google.gson.Gson) Broadcast(com.github.dedis.popstellar.model.network.method.Broadcast) Observable(io.reactivex.Observable) LinkedList(java.util.LinkedList) Log(android.util.Log) Subject(io.reactivex.subjects.Subject) Channel(com.github.dedis.popstellar.model.objects.Channel) Catchup(com.github.dedis.popstellar.model.network.method.Catchup) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Query(com.github.dedis.popstellar.model.network.method.Query) SchedulerProvider(com.github.dedis.popstellar.utility.scheduler.SchedulerProvider) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) PublishSubject(io.reactivex.subjects.PublishSubject) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe)

Example 25 with Channel

use of com.github.dedis.popstellar.model.objects.Channel in project popstellar by dedis.

the class LaoDetailViewModel method createNewRollCall.

/**
 * Creates new roll call event.
 *
 * <p>Publish a GeneralMessage containing CreateRollCall data.
 *
 * @param title the title of the roll call
 * @param description the description of the roll call, can be empty
 * @param creation the creation time of the roll call
 * @param proposedStart the proposed start time of the roll call
 * @param proposedEnd the proposed end time of the roll call
 * @param open true if we want to directly open the roll call
 */
public void createNewRollCall(String title, String description, long creation, long proposedStart, long proposedEnd, boolean open) {
    Log.d(TAG, "creating a new roll call with title " + title);
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel();
    // FIXME Location : Lausanne ?
    CreateRollCall createRollCall = new CreateRollCall(title, creation, proposedStart, proposedEnd, "Lausanne", description, lao.getId());
    Log.d(TAG, PUBLISH_MESSAGE);
    Disposable disposable = networkManager.getMessageSender().publish(keyManager.getMainKeyPair(), channel, createRollCall).subscribe(() -> {
        Log.d(TAG, "created a roll call with id: " + createRollCall.getId());
        if (open) {
            openRollCall(createRollCall.getId());
        } else {
            mCreatedRollCallEvent.postValue(new SingleEvent<>(true));
        }
    }, error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_create_rollcall));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Channel(com.github.dedis.popstellar.model.objects.Channel) 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) CreateRollCall(com.github.dedis.popstellar.model.network.method.message.data.rollcall.CreateRollCall)

Aggregations

Channel (com.github.dedis.popstellar.model.objects.Channel)39 Lao (com.github.dedis.popstellar.model.objects.Lao)30 LAORepository (com.github.dedis.popstellar.repository.LAORepository)28 Disposable (io.reactivex.disposables.Disposable)19 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)17 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)16 StateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao)14 UpdateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)14 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)13 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)10 GenericMessage (com.github.dedis.popstellar.model.network.GenericMessage)8 Error (com.github.dedis.popstellar.model.network.answer.Error)8 ResultMessages (com.github.dedis.popstellar.model.network.answer.ResultMessages)8 Catchup (com.github.dedis.popstellar.model.network.method.Catchup)8 Publish (com.github.dedis.popstellar.model.network.method.Publish)8 Query (com.github.dedis.popstellar.model.network.method.Query)8 Subscribe (com.github.dedis.popstellar.model.network.method.Subscribe)8 Unsubscribe (com.github.dedis.popstellar.model.network.method.Unsubscribe)8 Data (com.github.dedis.popstellar.model.network.method.message.data.Data)8 JsonRPCErrorException (com.github.dedis.popstellar.utility.error.JsonRPCErrorException)8