package market.market_util;

/**
 * Class that handles packet counting to the rwServer.
 * @author: nik
 */
public class rwCountPacket {
 private short Number = 0;
 private short marketKey = 0;
 private long wrKey = Constants.No_Key;

 private StringBuffer rwKeyBuffer = null;
 private StringBuffer rwCountsBuffer = null;
/**
 * Constructor of the packet counting class
 * @param CmarketKey short
 * @param ClangKey short
 * @param CwrKey long
 * @param CrwString java.lang.String
 * @param CrwCounts long
 * @param indexes short
 * @param firstIndex short 
 */
public rwCountPacket(short CmarketKey, long CwrKey, long CrwKey, byte CrwCounts) {
	marketKey = CmarketKey;
	wrKey = CwrKey;
	rwKeyBuffer = new StringBuffer(String.valueOf(CrwKey));
	rwCountsBuffer = new StringBuffer(String.valueOf(CrwCounts));
	Number = 1;	
}
/**
 * Adds element to rwCountPacket structure
 * @return boolean
 * @param rwString java.lang.String
 * @param rwCounts long
 * @param index short
 */
public synchronized boolean AddElement(long rwKey, byte rwCounts) {
	try {
		if (rwKey < 255 || rwCounts < 1)
			return false;
		rwKeyBuffer.append(Constants.Packet_Delimiter);
		rwKeyBuffer.append(rwKey);
		rwCountsBuffer.append(Constants.Packet_Delimiter);
		rwCountsBuffer.append(rwCounts);
		Number++;
		return true;
	} catch (Exception e) {
		return false;
	}
}
/**
 * Creates the header to be send to the rwServer
 * @return java.lang.StringBuffer
 */
public synchronized StringBuffer CreateHeader() {
	try {
		if (wrKey < 1 || marketKey < 1)
			return null;
		StringBuffer Header = new StringBuffer();
		
		Header.append(Constants.cs_WR_KEY);
		Header.append('=');
		Header.append(wrKey);
		Header.append('&');
		
		Header.append(Constants.cs_MARKET_KEY);
		Header.append('=');
		Header.append(marketKey);
		Header.append('&');

		Header.append(Constants.cs_Packet_Number);
		Header.append('=');
		Header.append(Number);
		Header.append('&');
				
		Header.append(Constants.cs_RW_KEY);
		Header.append('=');
		Header.append(rwKeyBuffer.toString());
		Header.append('&');

		Header.append(Constants.cs_RW_COUNTS);
		Header.append('=');
		Header.append(rwCountsBuffer.toString());
		
		return Header;
	} catch (Exception e) {
		return null;
	}
}
}

