1. VmListModel.java
private void connectToConsoles() {
  final VmConsoles consoles=consolesFactory.getVmConsolesForVm(vm);
  ...
  consoles.connect();
}

2. VmConsoles.java
void connect() throws ConsoleConnectException;

3. VmConsolesImpl.java
    public void connect() throws ConsoleConnectException {
        if (!canConnectToConsole()) {
            throw new ConsoleConnectException(connectErrorMessage());
        }

        getConsoleModel(getSelectedProcotol().getBackingClass()).getConnectCommand().execute();
    }

4. ConsoleModel.java
    @Override
    public void executeCommand(UICommand command) {
        super.executeCommand(command);

        if (command == getConnectCommand()) {
            connect();
        }
    }

    protected abstract void connect();

5. SpiceConsoleModel.java
    @Override
    protected void connect() {
        if (getEntity() != null) {
            getLogger().debug("Connecting to Spice console..."); //$NON-NLS-1$

            // Don't connect if there VM is not running on any host.
            if (getEntity().getRunOnVds() == null) {
                return;
            }

            // If it is not windows or SPICE guest agent is not installed, make sure the WAN options are disabled.
            if (!AsyncDataProvider.getInstance().isWindowsOsType(getEntity().getVmOsId()) || !getEntity().getHasSpiceDriver()) {
                getspice().getOptions().setWanOptionsEnabled(false);
            }

            UICommand invokeConsoleCommand = new UICommand("invokeConsoleCommand", new BaseCommandTarget() { //$NON-NLS-1$
                @Override
                public void executeCommand(UICommand uiCommand) {
                    invokeConsole();
                }
            });
            executeCommandWithConsoleSafenessWarning(invokeConsoleCommand);
        }
    }

6. SpiceConsoleModel.java
public void invokeConsole() {
  Frontend.getInstance().runAction(VdcActionType.VmLogon, new VmOperationParameterBase(getEntity().getId()),  //先登录到虚拟机里面判断用户名和密码是否正确

  spiceConsoleModel.executeQuery(getEntity());

  ((SpiceConsoleModel) model).invokeClient(null);

public void invokeClient(final List<RepoImage> repoImages) {
  try {
       getspice().setOptions(configuredOptions);
       if (getspice() instanceof HasForeignMenuData) {
           setForeignMenuData((HasForeignMenuData) getspice());
       }
       getspice().invokeClient();  //如果上面判断成功了,就会执行到这里,这里就是初始化console.vv文件里面的内容
   } catch (RuntimeException ex) {
       getLogger().error("Exception on Spice connect", ex); //$NON-NLS-1$
  }

}

7. ConsoleClient.java
public interface ConsoleClient {
    ConsoleOptions getOptions();
    void setOptions(ConsoleOptions options);
    void invokeClient();  //根据使用的协议类型来进行跳转
}

8. 如果我们用的是原生spice协议,则 SpiceNativeImpl.java
public class SpiceNativeImpl extends AbstractSpice implements ISpiceNative {

    @Override
    public void invokeClient() {
        AsyncQuery callback = new AsyncQuery(this, new INewAsyncCallback() {
            @Override
            public void onSuccess(Object model, Object returnValue) { // todo avoid code duplication with vnc
                StringBuilder configBuilder = new StringBuilder((String) ((VdcQueryReturnValue) returnValue).getReturnValue());
                writeHYPERSysSection(configBuilder, getOptions());
                ConsoleModel.makeConsoleConfigRequest("console.vv", //$NON-NLS-1$
                        "application/x-virt-viewer; charset=UTF-8", //$NON-NLS-1$
                        configBuilder.toString());
            }
        });

        Frontend.getInstance().runQuery(
                VdcQueryType.GetConsoleDescriptorFile,
                new ConsoleOptionsParams(getOptions()), callback);
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐