Search in sources :

Example 1 with CommandHandler

use of com.canoo.dp.impl.server.legacy.communication.CommandHandler in project dolphin-platform by canoo.

the class ServerConnector method receive.

/**
 * doesn't fail on missing commands
 */
public List<Command> receive(final Command command) {
    Assert.requireNonNull(command, "command");
    LOG.trace("Received command of type {}", command.getClass().getSimpleName());
    // collecting parameter pattern
    List<Command> response = new LinkedList();
    if (!(command instanceof InterruptLongPollCommand)) {
        // signal commands must not update thread-confined state
        for (DolphinServerAction it : dolphinServerActions) {
            // todo: can be deleted as soon as all action refer to the SMS
            it.setDolphinResponse(response);
        }
        serverModelStore.setCurrentResponse(response);
    }
    List<CommandHandler> actions = registry.getActionsFor(command.getClass());
    if (actions.isEmpty()) {
        LOG.warn("There is no server action registered for received command type {}, known commands types are {}", command.getClass().getSimpleName(), registry.getActions().keySet());
        return response;
    }
    // copying the list of actions allows an Action to unregister itself
    // avoiding ConcurrentModificationException to be thrown by the loop
    List<CommandHandler> actionsCopy = new ArrayList<CommandHandler>();
    actionsCopy.addAll(actions);
    try {
        for (CommandHandler action : actionsCopy) {
            action.handleCommand(command, response);
        }
    } catch (Exception exception) {
        throw exception;
    }
    return response;
}
Also used : InterruptLongPollCommand(com.canoo.dp.impl.remoting.legacy.commands.InterruptLongPollCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) InterruptLongPollCommand(com.canoo.dp.impl.remoting.legacy.commands.InterruptLongPollCommand) ArrayList(java.util.ArrayList) CommandHandler(com.canoo.dp.impl.server.legacy.communication.CommandHandler) LinkedList(java.util.LinkedList) DolphinServerAction(com.canoo.dp.impl.server.legacy.action.DolphinServerAction)

Example 2 with CommandHandler

use of com.canoo.dp.impl.server.legacy.communication.CommandHandler in project dolphin-platform by canoo.

the class DolphinContext method registerCommand.

protected <T extends Command> void registerCommand(final ActionRegistry registry, final Class<T> commandClass, final Consumer<T> handler) {
    Assert.requireNonNull(registry, "registry");
    Assert.requireNonNull(commandClass, "commandClass");
    Assert.requireNonNull(handler, "handler");
    registry.register(commandClass, new CommandHandler() {

        @Override
        public void handleCommand(final Command command, final List response) {
            LOG.trace("Handling {} for DolphinContext {}", commandClass.getSimpleName(), getId());
            handler.accept((T) command);
        }
    });
}
Also used : CreateContextCommand(com.canoo.dp.impl.remoting.commands.CreateContextCommand) DestroyContextCommand(com.canoo.dp.impl.remoting.commands.DestroyContextCommand) CallActionCommand(com.canoo.dp.impl.remoting.commands.CallActionCommand) CreateControllerCommand(com.canoo.dp.impl.remoting.commands.CreateControllerCommand) InterruptLongPollCommand(com.canoo.dp.impl.remoting.legacy.commands.InterruptLongPollCommand) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) StartLongPollCommand(com.canoo.dp.impl.remoting.legacy.commands.StartLongPollCommand) DestroyControllerCommand(com.canoo.dp.impl.remoting.commands.DestroyControllerCommand) List(java.util.List) LinkedList(java.util.LinkedList) CommandHandler(com.canoo.dp.impl.server.legacy.communication.CommandHandler)

Example 3 with CommandHandler

use of com.canoo.dp.impl.server.legacy.communication.CommandHandler in project dolphin-platform by canoo.

the class TestDolphinCommandHandler method testInvocation.

@Test
public void testInvocation() throws Exception {
    // Given:
    final DolphinTestConfiguration configuration = createDolphinTestConfiguration();
    final ServerModelStore serverModelStore = configuration.getServerModelStore();
    final ClientModelStore clientModelStore = configuration.getClientModelStore();
    final DolphinCommandHandler dolphinCommandHandler = new DolphinCommandHandler(configuration.getClientConnector());
    final String modelId = UUID.randomUUID().toString();
    clientModelStore.createModel(modelId, null, new ClientAttribute("myAttribute", "UNKNOWN"));
    configuration.getServerConnector().register(new DolphinServerAction() {

        @Override
        public void registerIn(ActionRegistry registry) {
            registry.register(TestChangeCommand.class, new CommandHandler() {

                @Override
                public void handleCommand(Command command, List response) {
                    serverModelStore.findPresentationModelById(modelId).getAttribute("myAttribute").setValue("Hello World");
                }
            });
        }
    });
    // When:
    dolphinCommandHandler.invokeDolphinCommand(new TestChangeCommand()).get();
    // Then:
    assertEquals(clientModelStore.findPresentationModelById(modelId).getAttribute("myAttribute").getValue(), "Hello World");
}
Also used : ClientAttribute(com.canoo.dp.impl.client.legacy.ClientAttribute) ServerModelStore(com.canoo.dp.impl.server.legacy.ServerModelStore) CommandHandler(com.canoo.dp.impl.server.legacy.communication.CommandHandler) DolphinCommandHandler(com.canoo.dp.impl.client.DolphinCommandHandler) ActionRegistry(com.canoo.dp.impl.server.legacy.communication.ActionRegistry) DolphinCommandHandler(com.canoo.dp.impl.client.DolphinCommandHandler) Command(com.canoo.dp.impl.remoting.legacy.communication.Command) List(java.util.List) ClientModelStore(com.canoo.dp.impl.client.legacy.ClientModelStore) DolphinServerAction(com.canoo.dp.impl.server.legacy.action.DolphinServerAction) Test(org.testng.annotations.Test) AbstractDolphinBasedTest(com.canoo.dolphin.client.util.AbstractDolphinBasedTest)

Aggregations

Command (com.canoo.dp.impl.remoting.legacy.communication.Command)3 CommandHandler (com.canoo.dp.impl.server.legacy.communication.CommandHandler)3 InterruptLongPollCommand (com.canoo.dp.impl.remoting.legacy.commands.InterruptLongPollCommand)2 DolphinServerAction (com.canoo.dp.impl.server.legacy.action.DolphinServerAction)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 AbstractDolphinBasedTest (com.canoo.dolphin.client.util.AbstractDolphinBasedTest)1 DolphinCommandHandler (com.canoo.dp.impl.client.DolphinCommandHandler)1 ClientAttribute (com.canoo.dp.impl.client.legacy.ClientAttribute)1 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)1 CallActionCommand (com.canoo.dp.impl.remoting.commands.CallActionCommand)1 CreateContextCommand (com.canoo.dp.impl.remoting.commands.CreateContextCommand)1 CreateControllerCommand (com.canoo.dp.impl.remoting.commands.CreateControllerCommand)1 DestroyContextCommand (com.canoo.dp.impl.remoting.commands.DestroyContextCommand)1 DestroyControllerCommand (com.canoo.dp.impl.remoting.commands.DestroyControllerCommand)1 StartLongPollCommand (com.canoo.dp.impl.remoting.legacy.commands.StartLongPollCommand)1 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)1 ActionRegistry (com.canoo.dp.impl.server.legacy.communication.ActionRegistry)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1