summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/comms/messages/IncomingIntMessage.java
blob: 433c53db6142997654cdceea9524f3c3b7ae4bf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package org.reprap.comms.messages;

import java.io.IOException;

import org.reprap.comms.IncomingContext;
import org.reprap.comms.IncomingMessage;

public abstract class IncomingIntMessage extends IncomingMessage {

	public IncomingIntMessage(IncomingContext incomingContext)
			throws IOException {
		super(incomingContext);
	}

	public static int ConvertBytesToInt(byte b1, byte b2) {
		int low = b1;
		int high = b2;
	    if (low < 0) low += 256;
	    if (high < 0) high += 256;
	    return low + (high << 8);
	}
	
	public int getValue() throws InvalidPayloadException {
	    byte [] reply = getPayload();
	    if (reply == null || reply.length != 3)
	    	throw new InvalidPayloadException();
	    return ConvertBytesToInt(reply[1], reply[2]);
	}
	
	abstract protected boolean isExpectedPacketType(byte packetType);

}