/* * Copyright (C) 2008 Manish Pandya, [manish at meetamanish dot com] * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * */ package org.hooliguns.ninja.telnet; /** * An abstract stub of command execution unit for mod specific implementation to * extend. It must know how to abort current execution (implement * {@link AbortExecutionListener}), must know how to parse mod specific * commands and must implement {@link Runnable} (i.e. allow run method executing * command queue. * * @author Manish Pandya (July 1 2008) * */ public abstract class CommandExecutionUnit implements AbortExecutionListener, CommandParser, Runnable, Statefull { protected static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; /** * The Queue holding commands */ protected CommandQueue commandQueue = null; /** * place holder of desired azimuth */ protected int destinationX = Integer.MIN_VALUE; /** * place holder of desired altitude */ protected int destinationY = Integer.MIN_VALUE; /** * current velocity for azimuth */ protected int velocity = 0; /** * current command that is being executed */ protected Command currentCommand = null; /** * Creates a CommandExecutionUnit with supplied CommandQueue * * @param commandQueue * the command queue for the execution unit */ public CommandExecutionUnit(CommandQueue commandQueue) { this.commandQueue = commandQueue; } /** * A method to query the internal state of the execution unit * * @return a comma seperated name=value formated name and value pairs of * internal state registers */ public String getState() { StringBuffer sb = new StringBuffer(); if (this instanceof RealtimeStateReporter) { RealtimeStateReporter realTimeStateReporter = (RealtimeStateReporter) this; sb.append("inMotion=" + realTimeStateReporter.isInMotion() + ","); sb.append("currentX=" + realTimeStateReporter.getCurrentX() + ","); sb.append("currentY=" + realTimeStateReporter.getCurrentY() + ","); } else { sb.append("inMotion=?,"); sb.append("currentX=?,"); sb.append("currentY=?,"); } sb.append("destinationX=" + destinationX + ","); sb.append("destinationY=" + destinationY + ","); sb.append("destVelocityX=" + velocity + ","); sb.append("currentCommand=" + currentCommand); sb.append("queue.size=" + commandQueue.size()); return sb.toString(); } /** * A method to write bytes to real hardware. * It takes care of exceptions, port simultions and other * complexities. * @param bytes bytes to be written to hardware port * @return response bytes from hardware */ public abstract byte[] writeToHardware(byte[] bytes); /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#getDestinationX() */ public int getDestinationX() { return destinationX; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#setDestinationX(int) */ public void setDestinationX(int destinationX) { this.destinationX = destinationX; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#getDestinationY() */ public int getDestinationY() { return destinationY; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#setDestinationY(int) */ public void setDestinationY(int destinationY) { this.destinationY = destinationY; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#getVelocity() */ public int getVelocity() { return velocity; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#setVelocity(int) */ public void setVelocity(int velocity) { this.velocity = velocity; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#getCurrentCommand() */ public Command getCurrentCommand() { return currentCommand; } /* (non-Javadoc) * @see org.hooliguns.ninja.telnet.Statefull#setCurrentCommand(org.hooliguns.ninja.telnet.Command) */ public void setCurrentCommand(Command currentCommand) { this.currentCommand = currentCommand; } }