READERNAME Protocol.java

From RifidiWiki

Jump to: navigation, search

This class removes protocols from incoming messages and adds protocols to outgoing messages. In addition it parses incoming messages to make sure that only one command at a time is placed on the incoming message buffer. See Command Flow. This class should implement Protocol. If nothing special needs to happen, the reader can use RawProtocol.

addProtocol

This method adds a protocol to outgoing messages. For example, if the outgoing message needs to be an HTTP message, the protocol should construct the necessary HTTP part of the outgoing message

public byte[] addProtocol(byte[] data);

Arguments

  1. data - The response from the reader that needs a protocol added to it.

Return Value

A message that is properly encoded with the correct protocol that is ready to be sent back to the client.

Reference Implementation

This is the implementation of the addProtocol() method in the alien reader. For it, nothing special needs to happen.

	public byte[] addProtocol(byte[] data) {
		return data;
	}

removeProtocol

This method removes protocols on incoming messages. It also splits up commands if more than two commands are sent at the same time and read from the TCP socket all at once. These command are sent one at a time to READERNAME_CommandFormatter.java::decode(byte[] arg) to be parsed and sent to the command handler.

public List<byte[]> removeProtocol(byte[] data);

Arguments

  1. data - A list of bytes that has been read from the TCP socket

Return Value

A list of commands, where each item in the list is a single command

Reference Implementation

This is the implementation of the removeProtocol() method in the alien reader. It splits up messages using '\n' as a deliminator. It then puts each individual message in its own slot in an array and returns the array.

	public List<byte[]> removeProtocol(byte[] data)
			throws ProtocolValidationException {
		List<byte[]> retVal = new ArrayList<byte[]>();
		String strData = new String(data);
		String[] arrayData = strData.split("\n");
		for( String i:arrayData ) {
			retVal.add(i.getBytes());
		}
		return retVal;
	}
Personal tools