Difference between revisions of "READERNAME CommandFormatter.java"
From RifidiWiki
(New page: This class should implement CommandHandler. This page goes through each of the four methods in CommandHandler and describes in detail what each one does and how it should be implemented. ...) |
|||
Line 34: | Line 34: | ||
=encode= | =encode= | ||
− | After a command has been processed by the command handler, the response it returns must be formatted to be sent back out to the client. | + | After a command has been processed by the command handler, the response it returns must be formatted to be sent back out to the client. |
− | + | ||
<pre>public ArrayList<Object> encode(ArrayList<Object> arg);</pre> | <pre>public ArrayList<Object> encode(ArrayList<Object> arg);</pre> | ||
Line 86: | Line 85: | ||
} | } | ||
</pre> | </pre> | ||
+ | |||
+ | |||
+ | =promptSuppress= | ||
+ | This method is mainly used by the alien reader to determine if the reader should supress the prompt when sending back a response. Other readers may need similar functionality. | ||
+ | |||
+ | <pre>public boolean promptSuppress();</pre> | ||
+ | |||
+ | ==Arguments== | ||
+ | |||
+ | ==Return Value== | ||
+ | true if the prompt should be surpressed | ||
+ | |||
+ | =getActualCommand= | ||
+ | This method is mainly used by the alien reader to return a string that a user actually typed in. Other readers may need similar functionality | ||
+ | |||
+ | <pre>public String getActualCommand(byte[] arg);</pre> | ||
+ | |||
+ | ==Arguments== | ||
+ | #arg - The incoming command | ||
+ | |||
+ | ==Return Value== | ||
+ | A string that is the command the user typed in |
Revision as of 22:07, 8 February 2008
This class should implement CommandHandler. This page goes through each of the four methods in CommandHandler and describes in detail what each one does and how it should be implemented. The first two methods are important, and the last two methods are not as important.
Contents
decode
This method needs to decode an incoming command into a command to be handled in the command formatter. It should put the command into a commandObject and put the commandObject in the first slot in an arraylist and return the arraylist.
public ArrayList<Object> decode(byte[] arg);
Arguments
- arg - The incoming command
Return Value
An array of objects where the first object is a command in the correct format to be processed by the command handler
Reference Implementation
This is the decode method in the alien reader
public ArrayList<Object> decode(byte[] arg) { if (arg.length == 0) { ArrayList<Object> argArray = new ArrayList<Object>(); argArray.add(""); console = false; return argArray; } String newString = new String(arg); logger.debug("Formatter arg: " + newString); ArrayList<Object> retVal = this.parseCommand(newString); return retVal; }
encode
After a command has been processed by the command handler, the response it returns must be formatted to be sent back out to the client.
public ArrayList<Object> encode(ArrayList<Object> arg);
Arguments
- arg - The return message(s) before they has been processed is in the array of objects
Return Value
An array of objects where each object is a formatted response message ready to be sent back to the client.
Reference Implementation
This is the encode method for the symbol reader. Because some commands in the symbol reader need to have more than one response message (e.g. a data message and a final message), more than one raw message can be in the array list in arg. For each message, the CRC must be calculated and appended.
public ArrayList<Object> encode(ArrayList<Object> arg) { ArrayList<Object> retVal = new ArrayList<Object>(); for (Object o : arg) { byte[] command = (byte[]) o; /* * Create outgoing array list that is 3 slots bigger than incoming * one: one for start of frame and two for crc bytes */ byte[] outgoingCommand = new byte[command.length + 3]; for (int i = 0; i < command.length; i++) { outgoingCommand[i + 1] = command[i]; } /* Add Start of Frame Byte */ outgoingCommand[0] = 0x01; // Calculate CRC int crc = CRC16.calculateCRC(command, 0xBEEF, CRC16.XR400_CRC_TABLE, true); byte[] crcBytes = ByteAndHexConvertingUtility .intToByteArray(crc, 2); // put crc in outgoingCommand, LSB first outgoingCommand[outgoingCommand.length - 2] = crcBytes[1]; outgoingCommand[outgoingCommand.length - 1] = crcBytes[0]; logger.debug(ByteAndHexConvertingUtility.toHexString(outgoingCommand)); retVal.add(outgoingCommand); } return retVal; }
promptSuppress
This method is mainly used by the alien reader to determine if the reader should supress the prompt when sending back a response. Other readers may need similar functionality.
public boolean promptSuppress();
Arguments
Return Value
true if the prompt should be surpressed
getActualCommand
This method is mainly used by the alien reader to return a string that a user actually typed in. Other readers may need similar functionality
public String getActualCommand(byte[] arg);
Arguments
- arg - The incoming command
Return Value
A string that is the command the user typed in