diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java index 768bc919e8..27c1c1a993 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/CreateAndInstallVm.java @@ -106,18 +106,11 @@ public class CreateAndInstallVm implements Function { } private void configureOsInstallationWithKeyboardSequence(String vmName, String installationKeySequence) { - Iterable> scancodelist = transform(Splitter.on(" ").split(installationKeySequence), new StringToKeyCode()); for (List scancodes : scancodelist) { machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new SendScancode(scancodes)); - // this is needed to avoid to miss any scancode - try { - Thread.sleep(300); - } catch (InterruptedException e) { - logger.error("Problem in sleeping the current thread.", e); - } } } diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java index 9bef135c81..8583a4c0da 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java @@ -20,22 +20,58 @@ package org.jclouds.virtualbox.functions; import java.util.List; +import javax.annotation.Resource; +import javax.inject.Named; + +import org.jclouds.compute.reference.ComputeServiceConstants; +import org.jclouds.logging.Logger; +import org.jclouds.virtualbox.settings.KeyboardScancodes; import org.virtualbox_4_1.ISession; +import com.beust.jcommander.internal.Lists; import com.google.common.base.Function; +import com.google.common.collect.Sets; class SendScancode implements Function { + + @Resource + @Named(ComputeServiceConstants.COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; - private final List scancodes; + + private static final int MAX_KEYCODES_ACCEPTED = 30; + private final List scancodes; public SendScancode(List scancodes) { this.scancodes = scancodes; } - + @Override public Void apply(ISession iSession) { - for (Integer scancode : scancodes) { - iSession.getConsole().getKeyboard().putScancode(scancode); + int i = 0, j = 0, length = scancodes.size(); + if (length > MAX_KEYCODES_ACCEPTED) { + while (i <= length) { + j = (i + 30 > length) ? length : i + MAX_KEYCODES_ACCEPTED; + List sublist = Lists.newArrayList(scancodes).subList(i, j); + long codeStores = iSession.getConsole().getKeyboard().putScancodes(sublist); + logger.debug("List of scancodes sent: ", sublist); + assert(codeStores==sublist.size()); + i = i + MAX_KEYCODES_ACCEPTED; + try { + Thread.sleep(50); + } catch (InterruptedException e) { + logger.debug("There was a problem in sleeping this thread", e); + } + } + } else { + iSession.getConsole().getKeyboard().putScancodes(scancodes); + if(Sets.difference(Sets.newHashSet(scancodes), Sets.newHashSet(KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST.values())) != null) { + try { + Thread.sleep(200); + } catch (InterruptedException e) { + logger.debug("There was a problem in sleeping this thread", e); + } + } } return null; }