issue 384: refactor sendScancode - refactoring with List.partition

This commit is contained in:
andreaturli 2012-01-29 12:20:43 +00:00
parent 8258415bbd
commit 1b53c2824c
1 changed files with 20 additions and 29 deletions

View File

@ -25,12 +25,10 @@ import javax.inject.Named;
import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.logging.Logger; import org.jclouds.logging.Logger;
import org.jclouds.virtualbox.settings.KeyboardScancodes;
import org.virtualbox_4_1.ISession; import org.virtualbox_4_1.ISession;
import com.beust.jcommander.internal.Lists;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.collect.Sets; import com.google.common.collect.Lists;
class SendScancode implements Function<ISession, Void> { class SendScancode implements Function<ISession, Void> {
@ -38,8 +36,7 @@ class SendScancode implements Function<ISession, Void> {
@Named(ComputeServiceConstants.COMPUTE_LOGGER) @Named(ComputeServiceConstants.COMPUTE_LOGGER)
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
private static final int MAX_SIZE = 30;
private static final int MAX_KEYCODES_ACCEPTED = 30;
private final List<Integer> scancodes; private final List<Integer> scancodes;
public SendScancode(List<Integer> scancodes) { public SendScancode(List<Integer> scancodes) {
@ -48,31 +45,25 @@ class SendScancode implements Function<ISession, Void> {
@Override @Override
public Void apply(ISession iSession) { public Void apply(ISession iSession) {
int i = 0, j = 0, length = scancodes.size(); for (List<Integer> maxOrLess : Lists.partition(scancodes, MAX_SIZE)) {
if (length > MAX_KEYCODES_ACCEPTED) { long codeStores = iSession.getConsole().getKeyboard().putScancodes(maxOrLess);
while (i <= length) { logger.debug("List of scancodes sent: ", maxOrLess);
j = (i + 30 > length) ? length : i + MAX_KEYCODES_ACCEPTED; assert (codeStores == maxOrLess.size());
List<Integer> sublist = Lists.newArrayList(scancodes).subList(i, j); // TODO @Adrian if maxOrLess contains SPECIAL CHAR sleep should be higher than NORMAL CHAR (300 ms - 50 ms)
long codeStores = iSession.getConsole().getKeyboard().putScancodes(sublist); // if (Iterables.any(maxOrLess, Predicates.in(SPECIAL_KEYBOARD_BUTTON_MAP_LIST.values()))) {
logger.debug("List of scancodes sent: ", sublist); // try {
assert(codeStores==sublist.size()); // Thread.sleep(300);
i = i + MAX_KEYCODES_ACCEPTED; // } 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 { try {
Thread.sleep(50); Thread.sleep(250);
} catch (InterruptedException e) { } catch (InterruptedException e) {
logger.debug("There was a problem in sleeping this thread", 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; return null;
} }
} }