refactored sendScancodes

This commit is contained in:
Adrian Cole 2012-01-30 00:22:36 +08:00
parent 7c49d058b4
commit 6dda77b5c2
3 changed files with 84 additions and 70 deletions

View File

@ -110,7 +110,7 @@ public class CreateAndInstallVm implements Function<MasterSpec, IMachine> {
transform(Splitter.on(" ").split(installationKeySequence), new StringToKeyCode());
for (List<Integer> scancodes : scancodelist) {
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new SendScancode(scancodes));
machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new SendScancodes(scancodes));
}
}

View File

@ -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<ISession, Void> {
@Resource
@Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL;
private static final int MAX_SIZE = 30;
private final List<Integer> scancodes;
public SendScancode(List<Integer> scancodes) {
this.scancodes = scancodes;
}
@Override
public Void apply(ISession iSession) {
for (List<Integer> 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;
}
}

View File

@ -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<ISession, Void> {
static final ImmutableSet<Integer> 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<Integer> scancodes;
public SendScancodes(List<Integer> scancodes) {
this.scancodes = scancodes;
}
@Override
public Void apply(ISession iSession) {
for (List<Integer> 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);
}
}
}