use of com.github.dedis.popstellar.model.network.answer.Answer 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();
}
use of com.github.dedis.popstellar.model.network.answer.Answer 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();
}
use of com.github.dedis.popstellar.model.network.answer.Answer in project popstellar by dedis.
the class LAONetworkManager method request.
private Single<Answer> request(Query query) {
Single<Answer> answerSingle = connection.observeMessage().filter(// Filter the Answers
Answer.class::isInstance).map(Answer.class::cast).filter(answer -> answer.getId() == query.getRequestId()).doOnNext(answer -> Log.d(TAG, "request id: " + answer.getId())).firstOrError().flatMap(answer -> {
if (answer instanceof Error) {
return Single.error(new JsonRPCErrorException((Error) answer));
} else {
return Single.just(answer);
}
}).subscribeOn(schedulerProvider.io()).observeOn(schedulerProvider.mainThread()).timeout(5, TimeUnit.SECONDS).cache();
// Only send the message after the single is created to make sure it is already waiting
// before the answer is received
connection.sendMessage(query);
return answerSingle;
}
Aggregations