From 6dda77b5c2ff1d32944774070fd95622ff072bda Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 30 Jan 2012 00:22:36 +0800 Subject: [PATCH] refactored sendScancodes --- .../functions/CreateAndInstallVm.java | 2 +- .../virtualbox/functions/SendScancode.java | 69 --------------- .../virtualbox/functions/SendScancodes.java | 83 +++++++++++++++++++ 3 files changed, 84 insertions(+), 70 deletions(-) delete mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancodes.java 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 f78c9b4b7f..fe36700e8c 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 @@ -110,7 +110,7 @@ public class CreateAndInstallVm implements Function { transform(Splitter.on(" ").split(installationKeySequence), new StringToKeyCode()); for (List scancodes : scancodelist) { - machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new SendScancode(scancodes)); + machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new SendScancodes(scancodes)); } } 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 deleted file mode 100644 index 3940f4aea8..0000000000 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancode.java +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -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.virtualbox_4_1.ISession; - -import com.google.common.base.Function; -import com.google.common.collect.Lists; - -class SendScancode implements Function { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private static final int MAX_SIZE = 30; - private final List scancodes; - - public SendScancode(List scancodes) { - this.scancodes = scancodes; - } - - @Override - public Void apply(ISession iSession) { - for (List maxOrLess : Lists.partition(scancodes, MAX_SIZE)) { - long codeStores = iSession.getConsole().getKeyboard().putScancodes(maxOrLess); - logger.debug("List of scancodes sent: ", maxOrLess); - assert (codeStores == maxOrLess.size()); - // TODO @Adrian if maxOrLess contains SPECIAL CHAR sleep should be higher than NORMAL CHAR (300 ms - 50 ms) -// if (Iterables.any(maxOrLess, Predicates.in(SPECIAL_KEYBOARD_BUTTON_MAP_LIST.values()))) { -// try { -// Thread.sleep(300); -// } catch (InterruptedException e) { -// logger.debug("There was a problem in sleeping this thread", e); -// } -// } - // TODO without extra check the extra time needed is more or less 250 ms - try { - Thread.sleep(250); - } catch (InterruptedException e) { - logger.debug("There was a problem in sleeping this thread", e); - } - } - return null; - } -} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancodes.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancodes.java new file mode 100644 index 0000000000..2366839638 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/functions/SendScancodes.java @@ -0,0 +1,83 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.virtualbox.functions; + +import static com.google.common.base.Predicates.in; +import static com.google.common.base.Throwables.propagate; +import static com.google.common.collect.Iterables.any; +import static com.google.common.collect.Iterables.concat; +import static com.google.common.collect.Lists.partition; +import static org.jclouds.compute.reference.ComputeServiceConstants.COMPUTE_LOGGER; +import static org.jclouds.virtualbox.settings.KeyboardScancodes.SPECIAL_KEYBOARD_BUTTON_MAP_LIST; + +import java.util.List; + +import javax.annotation.Resource; +import javax.inject.Named; + +import org.jclouds.logging.Logger; +import org.virtualbox_4_1.ISession; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; + +class SendScancodes implements Function { + static final ImmutableSet SPECIAL_SCANCODES = ImmutableSet.copyOf(concat(SPECIAL_KEYBOARD_BUTTON_MAP_LIST + .values())); + + @Resource + @Named(COMPUTE_LOGGER) + protected Logger logger = Logger.NULL; + + private static final int MAX_SIZE = 30; + + private final List scancodes; + + public SendScancodes(List scancodes) { + this.scancodes = scancodes; + } + + @Override + public Void apply(ISession iSession) { + for (List maxOrLess : partition(scancodes, MAX_SIZE)) { + long codesSent = iSession.getConsole().getKeyboard().putScancodes(maxOrLess); + logger.debug("List of scancodes sent: ", maxOrLess); + assert (codesSent == maxOrLess.size()); + if (any(maxOrLess, in(SPECIAL_SCANCODES))) { + sleepOrPropagateInterrupt(300); + } else { + sleepOrPropagateInterrupt(50); + } + } + return null; + } + + @Override + public String toString() { + return "sendScancodes(" + scancodes + ")"; + } + + public void sleepOrPropagateInterrupt(long ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + throw propagate(e); + } + } +} \ No newline at end of file