package art.servers.etdserver.protocols.rtms;
|
|
import art.library.utils.common.*;
|
|
public class RTMS_Message
|
{
|
private String data;
|
private byte[] databytes;
|
private int[] dataints;
|
private int pointer = 0;
|
|
|
public void setData(byte[] input)
|
{
|
data = new String(input);
|
databytes = input;
|
dataints = ArrayUtils.toIntArray(databytes);
|
}
|
|
|
public void setData(byte[] input, byte[] command)
|
{
|
byte[] aux = new byte[input.length-command.length];
|
System.arraycopy(input, command.length, aux, 0, aux.length);
|
data = new String(aux);
|
databytes = aux;
|
dataints = ArrayUtils.toIntArray(databytes);
|
}
|
|
|
public void setData(int[] input)
|
{
|
data = IntToString(input);
|
dataints = input;
|
databytes = ArrayUtils.toByteArray(dataints);
|
}
|
|
|
public void setData(String input)
|
{
|
data = input;
|
databytes = input.getBytes();
|
dataints = ArrayUtils.toIntArray(databytes);
|
}
|
|
|
public void reset ()
|
{
|
pointer = 0;
|
}
|
|
|
public int length ()
|
{
|
return(dataints.length);
|
}
|
|
|
public void checkCommand(byte[] response, byte[] command) throws Exception
|
{
|
String s1 = new String(response);
|
String s2 = new String(command);
|
if (s1.indexOf(s2) < 0) throw new Exception("Response erroneous");
|
}
|
|
|
public void checkError() throws Exception
|
{
|
int indexError = data.indexOf("ERR#");
|
if (indexError > -1)
|
{
|
int errorCode = Integer.parseInt(data.substring(indexError+4, indexError+6), 16);
|
|
// TODO Check Diamond Error Code received
|
}
|
}
|
|
|
public String getString (int length)
|
{
|
String resultado = data.substring(pointer, pointer + length);
|
pointer = pointer + length;
|
return resultado;
|
}
|
|
|
|
public int get1X()
|
{
|
return getX(1);
|
}
|
|
|
public int get2X()
|
{
|
return getX(2);
|
}
|
|
public int get4X()
|
{
|
return getX(4);
|
}
|
|
|
public int get4Xr()
|
{
|
int valor = getX(4);
|
return ((valor >> 8) & 0xFF) + ((valor << 8) & 0xFF00);
|
}
|
|
|
public int get6X()
|
{
|
return getX(6);
|
}
|
|
public int get8X()
|
{
|
return getX(8);
|
}
|
|
|
public int getX(int length)
|
{
|
int resultado = Integer.parseInt(data.substring(pointer, pointer + length), 16);
|
pointer = pointer + length;
|
return resultado;
|
}
|
|
|
public int get1I()
|
{
|
return getI(1);
|
}
|
|
public int get2I()
|
{
|
return getI(2);
|
}
|
|
|
public int get4I()
|
{
|
return getI(4);
|
}
|
|
|
private int getI(int length)
|
{
|
int result = Integer.parseInt(data.substring(pointer, pointer + length));
|
pointer = pointer + length;
|
return result;
|
}
|
|
|
public void skip (int longitud)
|
{
|
pointer = pointer + longitud;
|
}
|
|
|
|
public String getResponseString()
|
{
|
return data;
|
}
|
|
|
public byte[] getDataByte()
|
{
|
return databytes;
|
}
|
|
|
public int[] getDataInt()
|
{
|
return dataints;
|
}
|
|
|
public static String IntToString (int[] command, int start)
|
{
|
byte[] str = new byte[command.length - start];
|
|
int apuntador = 0;
|
|
for (int i=start; i<command.length; i++)
|
{
|
str[apuntador++] = (byte)command[i];
|
}
|
|
return new String(str);
|
}
|
|
|
|
|
public static String IntToString (int[] command)
|
{
|
byte[] str = new byte[command.length];
|
|
for (int i=0; i<command.length; i++)
|
{
|
str[i] = (byte)command[i];
|
}
|
|
return new String(str);
|
}
|
|
}
|