Search in sources :

Example 1 with Data

use of com.github.dedis.popstellar.model.network.method.message.data.Data in project popstellar by dedis.

the class MessageHandler method handleMessage.

/**
 * Send messages to the corresponding handler.
 *
 * @param laoRepository the repository to access the messages and LAOs
 * @param messageSender the service used to send messages to the backend
 * @param channel the channel on which the message was received
 * @param message the message that was received
 */
public void handleMessage(LAORepository laoRepository, MessageSender messageSender, Channel channel, MessageGeneral message) throws DataHandlingException {
    Log.d(TAG, "handle incoming message");
    // Put the message in the state
    laoRepository.getMessageById().put(message.getMessageId(), message);
    Data data = message.getData();
    Log.d(TAG, "data with class: " + data.getClass());
    Objects dataObj = Objects.find(data.getObject());
    Action dataAction = Action.find(data.getAction());
    registry.handle(new HandlerContext(laoRepository, keyManager, messageSender, channel, message), data, dataObj, dataAction);
    notifyLaoUpdate(laoRepository, data, channel);
}
Also used : Action(com.github.dedis.popstellar.model.network.method.message.data.Action) HandlerContext(com.github.dedis.popstellar.utility.handler.data.HandlerContext) Objects(com.github.dedis.popstellar.model.network.method.message.data.Objects) Data(com.github.dedis.popstellar.model.network.method.message.data.Data)

Example 2 with Data

use of com.github.dedis.popstellar.model.network.method.message.data.Data in project popstellar by dedis.

the class LAONetworkManagerTest method publishSendsRightMessage.

@Test
public void publishSendsRightMessage() {
    TestSchedulerProvider schedulerProvider = new TestSchedulerProvider();
    TestScheduler testScheduler = schedulerProvider.getTestScheduler();
    LAONetworkManager networkManager = new LAONetworkManager(laoRepository, handler, connection, JsonModule.provideGson(DataRegistryModule.provideDataRegistry()), schedulerProvider);
    Answer<?> answer = args -> {
        // Retrieve subscribe object
        Publish publish = args.getArgument(0);
        // Make sure the channel is correct
        assertEquals(CHANNEL, publish.getChannel());
        MessageGeneral messageGeneral = publish.getMessage();
        assertEquals(DATA, messageGeneral.getData());
        // Return a positive result
        messages.onNext(new Result(publish.getRequestId()));
        return null;
    };
    doAnswer(answer).when(connection).sendMessage(any(Publish.class));
    // Actual test
    Disposable disposable = networkManager.publish(KEY_PAIR, CHANNEL, DATA).subscribe();
    testScheduler.advanceTimeBy(5, TimeUnit.SECONDS);
    disposable.dispose();
    networkManager.dispose();
    verify(connection).sendMessage(any(Publish.class));
    verify(connection, atLeastOnce()).observeMessage();
    verify(connection).observeConnectionEvents();
    verify(connection).close();
    verifyNoMoreInteractions(connection);
}
Also used : TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Publish(com.github.dedis.popstellar.model.network.method.Publish) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSchedulerProvider(com.github.dedis.popstellar.utility.scheduler.TestSchedulerProvider) ResultMessages(com.github.dedis.popstellar.model.network.answer.ResultMessages) Subscribe(com.github.dedis.popstellar.model.network.method.Subscribe) LAORepository(com.github.dedis.popstellar.repository.LAORepository) Unsubscribe(com.github.dedis.popstellar.model.network.method.Unsubscribe) DataRegistryModule(com.github.dedis.popstellar.di.DataRegistryModule) HashSet(java.util.HashSet) WebSocket(com.tinder.scarlet.WebSocket) Answer(org.mockito.stubbing.Answer) GenericMessage(com.github.dedis.popstellar.model.network.GenericMessage) JsonRPCErrorException(com.github.dedis.popstellar.utility.error.JsonRPCErrorException) CreateLao(com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) Error(com.github.dedis.popstellar.model.network.answer.Error) KeyPair(com.github.dedis.popstellar.model.objects.security.KeyPair) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ErrorCode(com.github.dedis.popstellar.model.network.answer.ErrorCode) JsonModule(com.github.dedis.popstellar.di.JsonModule) Before(org.junit.Before) Channel(com.github.dedis.popstellar.model.objects.Channel) Result(com.github.dedis.popstellar.model.network.answer.Result) 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) Base64DataUtils(com.github.dedis.popstellar.testutils.Base64DataUtils) Query(com.github.dedis.popstellar.model.network.method.Query) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) MessageHandler(com.github.dedis.popstellar.utility.handler.MessageHandler) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) Disposable(io.reactivex.disposables.Disposable) TestScheduler(io.reactivex.schedulers.TestScheduler) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Disposable(io.reactivex.disposables.Disposable) MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) TestScheduler(io.reactivex.schedulers.TestScheduler) Publish(com.github.dedis.popstellar.model.network.method.Publish) Result(com.github.dedis.popstellar.model.network.answer.Result) Test(org.junit.Test)

Example 3 with Data

use of com.github.dedis.popstellar.model.network.method.message.data.Data in project popstellar by dedis.

the class JsonDataSerializer method deserialize.

@Override
public Data deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject obj = json.getAsJsonObject();
    JsonUtils.verifyJson(JsonUtils.DATA_SCHEMA, obj.toString());
    Objects object = Objects.find(obj.get(OBJECT).getAsString());
    Action action = Action.find(obj.get(ACTION).getAsString());
    if (object == null) {
        throw new JsonParseException("Unknown object type : " + obj.get(OBJECT).getAsString());
    }
    if (action == null) {
        throw new JsonParseException("Unknown action type : " + obj.get(ACTION).getAsString());
    }
    Optional<Class<? extends Data>> clazz = dataRegistry.getType(object, action);
    if (!clazz.isPresent()) {
        throw new JsonParseException("The pair (" + object.getObject() + ", " + action.getAction() + ") does not exists in the protocol");
    }
    return context.deserialize(json, clazz.get());
}
Also used : Action(com.github.dedis.popstellar.model.network.method.message.data.Action) Objects(com.github.dedis.popstellar.model.network.method.message.data.Objects) JsonObject(com.google.gson.JsonObject) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) JsonParseException(com.google.gson.JsonParseException)

Example 4 with Data

use of com.github.dedis.popstellar.model.network.method.message.data.Data in project popstellar by dedis.

the class JsonMessageGeneralSerializer method deserialize.

@Override
public MessageGeneral deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonMessageData jsonObject = context.deserialize(json, JsonMessageData.class);
    JsonElement dataElement = JsonParser.parseString(new String(jsonObject.data.getData(), StandardCharsets.UTF_8));
    Data data = context.deserialize(dataElement, Data.class);
    return new MessageGeneral(jsonObject.sender, jsonObject.data, data, jsonObject.signature, jsonObject.messageID, jsonObject.witnessSignatures);
}
Also used : MessageGeneral(com.github.dedis.popstellar.model.network.method.message.MessageGeneral) JsonElement(com.google.gson.JsonElement) Data(com.github.dedis.popstellar.model.network.method.message.data.Data) Base64URLData(com.github.dedis.popstellar.model.objects.security.Base64URLData)

Aggregations

Data (com.github.dedis.popstellar.model.network.method.message.data.Data)4 MessageGeneral (com.github.dedis.popstellar.model.network.method.message.MessageGeneral)2 Action (com.github.dedis.popstellar.model.network.method.message.data.Action)2 Objects (com.github.dedis.popstellar.model.network.method.message.data.Objects)2 DataRegistryModule (com.github.dedis.popstellar.di.DataRegistryModule)1 JsonModule (com.github.dedis.popstellar.di.JsonModule)1 GenericMessage (com.github.dedis.popstellar.model.network.GenericMessage)1 Error (com.github.dedis.popstellar.model.network.answer.Error)1 ErrorCode (com.github.dedis.popstellar.model.network.answer.ErrorCode)1 Result (com.github.dedis.popstellar.model.network.answer.Result)1 ResultMessages (com.github.dedis.popstellar.model.network.answer.ResultMessages)1 Catchup (com.github.dedis.popstellar.model.network.method.Catchup)1 Publish (com.github.dedis.popstellar.model.network.method.Publish)1 Query (com.github.dedis.popstellar.model.network.method.Query)1 Subscribe (com.github.dedis.popstellar.model.network.method.Subscribe)1 Unsubscribe (com.github.dedis.popstellar.model.network.method.Unsubscribe)1 CreateLao (com.github.dedis.popstellar.model.network.method.message.data.lao.CreateLao)1 Channel (com.github.dedis.popstellar.model.objects.Channel)1 Base64URLData (com.github.dedis.popstellar.model.objects.security.Base64URLData)1 KeyPair (com.github.dedis.popstellar.model.objects.security.KeyPair)1