Search in sources :

Example 26 with Channel

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

the class LaoDetailViewModel method sendConsensusElect.

/**
 * Sends a ConsensusElect message.
 *
 * <p>Publish a GeneralMessage containing ConsensusElect data.
 *
 * @param creation the creation time of the consensus
 * @param objId the id of the object the consensus refers to (e.g. election_id)
 * @param type the type of object the consensus refers to (e.g. election)
 * @param property the property the value refers to (e.g. "state")
 * @param value the proposed new value for the property (e.g. "started")
 */
public void sendConsensusElect(long creation, String objId, String type, String property, Object value) {
    Log.d(TAG, String.format("creating a new consensus for type: %s, property: %s, value: %s", type, property, value));
    Lao lao = getCurrentLaoValue();
    if (lao == null) {
        Log.d(TAG, LAO_FAILURE_MESSAGE);
        return;
    }
    Channel channel = lao.getChannel().subChannel("consensus");
    ConsensusElect consensusElect = new ConsensusElect(creation, objId, type, property, value);
    Log.d(TAG, PUBLISH_MESSAGE);
    MessageGeneral msg = new MessageGeneral(keyManager.getMainKeyPair(), consensusElect, gson);
    Disposable disposable = networkManager.getMessageSender().publish(channel, msg).subscribe(() -> Log.d(TAG, "created a consensus with message id : " + msg.getMessageId()), error -> ErrorUtils.logAndShow(getApplication(), TAG, error, R.string.error_start_election));
    disposables.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ConsensusElect(com.github.dedis.popstellar.model.network.method.message.data.consensus.ConsensusElect) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) 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)

Example 27 with Channel

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

the class LAONetworkManager method catchup.

@Override
public Completable catchup(Channel channel) {
    Log.d(TAG, "sending a catchup to the channel " + channel);
    Catchup catchup = new Catchup(channel, requestCounter.incrementAndGet());
    return request(catchup).map(ResultMessages.class::cast).map(ResultMessages::getMessages).doOnSuccess(messages -> Log.d(TAG, "Catchup on " + channel + " retrieved : " + messages)).doOnSuccess(messages -> handleMessages(messages, 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) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Catchup(com.github.dedis.popstellar.model.network.method.Catchup)

Example 28 with Channel

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

the class ConsensusHandler method handleLearn.

public static void handleLearn(HandlerContext context, ConsensusLearn consensusLearn) throws DataHandlingException {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Optional<ElectInstance> electInstanceOpt = lao.getElectInstance(consensusLearn.getMessageId());
    if (!electInstanceOpt.isPresent()) {
        Log.w(TAG, "learn for invalid messageId : " + consensusLearn.getMessageId());
        throw new InvalidMessageIdException(consensusLearn, consensusLearn.getMessageId());
    }
    ElectInstance electInstance = electInstanceOpt.get();
    if (consensusLearn.getLearnValue().isDecision()) {
        electInstance.setState(ElectInstance.State.ACCEPTED);
    }
    lao.updateElectInstance(electInstance);
    laoRepository.updateNodes(lao.getChannel());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) InvalidMessageIdException(com.github.dedis.popstellar.utility.error.InvalidMessageIdException) Lao(com.github.dedis.popstellar.model.objects.Lao)

Example 29 with Channel

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

the class ConsensusHandler method handleElect.

/**
 * Process an Elect message.
 *
 * @param context the HandlerContext of the message
 * @param consensusElect the data of the message that was received
 */
public static void handleElect(HandlerContext context, ConsensusElect consensusElect) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    MessageID messageId = context.getMessageId();
    PublicKey senderPk = context.getSenderPk();
    Lao lao = laoRepository.getLaoByChannel(channel);
    Set<PublicKey> nodes = new HashSet<>(lao.getWitnesses());
    nodes.add(lao.getOrganizer());
    ElectInstance electInstance = new ElectInstance(messageId, channel, senderPk, nodes, consensusElect);
    lao.updateElectInstance(electInstance);
    laoRepository.updateNodes(lao.getChannel());
}
Also used : ElectInstance(com.github.dedis.popstellar.model.objects.ElectInstance) PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Lao(com.github.dedis.popstellar.model.objects.Lao) MessageID(com.github.dedis.popstellar.model.objects.security.MessageID) HashSet(java.util.HashSet)

Example 30 with Channel

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

the class LaoHandler method handleCreateLao.

/**
 * Process a CreateLao message.
 *
 * @param context the HandlerContext of the message
 * @param createLao the message that was received
 */
public static void handleCreateLao(HandlerContext context, CreateLao createLao) {
    LAORepository laoRepository = context.getLaoRepository();
    Channel channel = context.getChannel();
    Log.d(TAG, "handleCreateLao: channel " + channel + ", msg=" + createLao);
    Lao lao = laoRepository.getLaoByChannel(channel);
    lao.setName(createLao.getName());
    lao.setCreation(createLao.getCreation());
    lao.setLastModified(createLao.getCreation());
    lao.setOrganizer(createLao.getOrganizer());
    lao.setId(createLao.getId());
    lao.setWitnesses(new HashSet<>(createLao.getWitnesses()));
    PublicKey publicKey = context.getKeyManager().getMainPublicKey();
    if (lao.getOrganizer().equals(publicKey) || lao.getWitnesses().contains(publicKey)) {
        context.getMessageSender().subscribe(lao.getChannel().subChannel("consensus")).subscribe();
    }
    laoRepository.updateNodes(channel);
}
Also used : PublicKey(com.github.dedis.popstellar.model.objects.security.PublicKey) Channel(com.github.dedis.popstellar.model.objects.Channel) LAORepository(com.github.dedis.popstellar.repository.LAORepository) StateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.StateLao) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Lao(com.github.dedis.popstellar.model.objects.Lao) UpdateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.UpdateLao)

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