Search in sources :

Example 1 with ConnectionOpenedHandler

use of org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler in project che by eclipse.

the class WorkspaceComponent method handleWorkspaceEvents.

/**
     * Listens message bus and handles workspace events.
     *
     * @param workspace
     *         workspace to listen
     * @param callback
     *         callback
     * @param restoreFromSnapshot
     *         restore or not the workspace from snapshot
     */
public void handleWorkspaceEvents(final WorkspaceDto workspace, final Callback<Component, Exception> callback, final Boolean restoreFromSnapshot) {
    this.callback = callback;
    if (messageBus != null) {
        messageBus.cancelReconnection();
    }
    messageBus = messageBusProvider.createMessageBus();
    messageBus.addOnOpenHandler(new ConnectionOpenedHandler() {

        @Override
        public void onOpen() {
            loader.show(STARTING_WORKSPACE_RUNTIME);
            messageBus.removeOnOpenHandler(this);
            setCurrentWorkspace(workspace);
            workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
            final WorkspaceStatus workspaceStatus = workspace.getStatus();
            switch(workspaceStatus) {
                case SNAPSHOTTING:
                    loader.show(CREATING_WORKSPACE_SNAPSHOT);
                    break;
                case STARTING:
                    eventBus.fireEvent(new WorkspaceStartingEvent(workspace));
                    break;
                case RUNNING:
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {

                        @Override
                        public void execute() {
                            loader.setSuccess(STARTING_WORKSPACE_RUNTIME);
                            eventBus.fireEvent(new WorkspaceStartedEvent(workspace));
                        }
                    });
                    break;
                default:
                    //
                    workspaceServiceClient.getSettings().then(new Function<Map<String, String>, Map<String, String>>() {

                        @Override
                        public Map<String, String> apply(Map<String, String> settings) throws FunctionException {
                            if (Boolean.parseBoolean(settings.getOrDefault(CHE_WORKSPACE_AUTO_START, "true"))) {
                                startWorkspaceById(workspace.getId(), workspace.getConfig().getDefaultEnv(), restoreFromSnapshot);
                            } else {
                                loader.show(WORKSPACE_STOPPED);
                            }
                            return settings;
                        }
                    });
            }
        }
    });
}
Also used : ConnectionOpenedHandler(org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler) Function(org.eclipse.che.api.promises.client.Function) WorkspaceStartedEvent(org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent) WorkspaceStatus(org.eclipse.che.api.core.model.workspace.WorkspaceStatus) WorkspaceStartingEvent(org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent) Map(java.util.Map)

Example 2 with ConnectionOpenedHandler

use of org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler in project che by eclipse.

the class TerminalPresenter method connectToTerminalWebSocket.

private void connectToTerminalWebSocket(@NotNull String wsUrl) {
    countRetry--;
    socket = WebSocket.create(wsUrl);
    socket.setOnMessageHandler(new MessageReceivedHandler() {

        @Override
        public void onMessageReceived(MessageReceivedEvent event) {
            terminal.write(event.getMessage());
        }
    });
    socket.setOnCloseHandler(new ConnectionClosedHandler() {

        @Override
        public void onClose(WebSocketClosedEvent event) {
            if (CLOSE_NORMAL == event.getCode()) {
                connected = false;
                terminalStateListener.onExit();
            }
        }
    });
    socket.setOnOpenHandler(new ConnectionOpenedHandler() {

        @Override
        public void onOpen() {
            JavaScriptObject terminalJso = moduleHolder.getModule("Xterm");
            // if terminal was created programmatically then we don't set focus on it
            TerminalOptionsJso terminalOptionsJso = TerminalOptionsJso.createDefault();
            if (source instanceof AddTerminalClickHandler || source instanceof Action) {
                terminalOptionsJso.withFocusOnOpen(true);
            }
            terminal = TerminalJso.create(terminalJso, terminalOptionsJso);
            connected = true;
            view.openTerminal(terminal);
            terminal.on(DATA_EVENT_NAME, new Operation<String>() {

                @Override
                public void apply(String arg) throws OperationException {
                    Jso jso = Jso.create();
                    jso.addField("type", "data");
                    jso.addField("data", arg);
                    socket.send(jso.serialize());
                }
            });
        }
    });
    socket.setOnErrorHandler(new ConnectionErrorHandler() {

        @Override
        public void onError() {
            connected = false;
            if (countRetry == 0) {
                view.showErrorMessage(locale.terminalErrorStart());
                notificationManager.notify(locale.connectionFailedWithTerminal(), locale.terminalErrorConnection(), FAIL, FLOAT_MODE);
            } else {
                reconnect();
            }
        }
    });
}
Also used : ConnectionOpenedHandler(org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler) Action(org.eclipse.che.ide.api.action.Action) ConnectionErrorHandler(org.eclipse.che.ide.websocket.events.ConnectionErrorHandler) MessageReceivedHandler(org.eclipse.che.ide.websocket.events.MessageReceivedHandler) Jso(org.eclipse.che.ide.collections.Jso) Operation(org.eclipse.che.api.promises.client.Operation) MessageReceivedEvent(org.eclipse.che.ide.websocket.events.MessageReceivedEvent) WebSocketClosedEvent(org.eclipse.che.ide.websocket.events.WebSocketClosedEvent) AddTerminalClickHandler(org.eclipse.che.ide.extension.machine.client.processes.AddTerminalClickHandler) JavaScriptObject(com.google.gwt.core.client.JavaScriptObject) ConnectionClosedHandler(org.eclipse.che.ide.websocket.events.ConnectionClosedHandler)

Aggregations

ConnectionOpenedHandler (org.eclipse.che.ide.websocket.events.ConnectionOpenedHandler)2 JavaScriptObject (com.google.gwt.core.client.JavaScriptObject)1 Map (java.util.Map)1 WorkspaceStatus (org.eclipse.che.api.core.model.workspace.WorkspaceStatus)1 Function (org.eclipse.che.api.promises.client.Function)1 Operation (org.eclipse.che.api.promises.client.Operation)1 Action (org.eclipse.che.ide.api.action.Action)1 WorkspaceStartedEvent (org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent)1 WorkspaceStartingEvent (org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent)1 Jso (org.eclipse.che.ide.collections.Jso)1 AddTerminalClickHandler (org.eclipse.che.ide.extension.machine.client.processes.AddTerminalClickHandler)1 ConnectionClosedHandler (org.eclipse.che.ide.websocket.events.ConnectionClosedHandler)1 ConnectionErrorHandler (org.eclipse.che.ide.websocket.events.ConnectionErrorHandler)1 MessageReceivedEvent (org.eclipse.che.ide.websocket.events.MessageReceivedEvent)1 MessageReceivedHandler (org.eclipse.che.ide.websocket.events.MessageReceivedHandler)1 WebSocketClosedEvent (org.eclipse.che.ide.websocket.events.WebSocketClosedEvent)1