blob: 04e6b0bb1aab114aa47fd5f37afaa859847fcd88 (
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package org.reprap.comms;
import java.io.IOException;
import org.reprap.ReprapException;
public abstract class IncomingMessage {
private byte [] payload; // The actual content portion of a packet, not the frilly bits
IncomingContext incomingContext;
public class InvalidPayloadException extends ReprapException {
private static final long serialVersionUID = -5403970405132990115L;
public InvalidPayloadException() {
super();
}
public InvalidPayloadException(String arg) {
super(arg);
}
}
/**
* Receive a message matching context criteria
* @param incomingContext the context in which to receive messages
* @throws IOException
*/
public IncomingMessage(IncomingContext incomingContext) throws IOException {
this.incomingContext = incomingContext;
Communicator comm = incomingContext.getCommunicator();
comm.receiveMessage(this);
}
/**
* Implemented by subclasses to allow them to indicate if they
* understand or expect a given packetType. This is used to
* decide if a received packet should be accepted or possibly discarded.
* @param packetType the type of packet to receive
* @return
*/
protected abstract boolean isExpectedPacketType(byte packetType);
public byte[] getPayload() {
return payload;
}
/**
* Called by the framework to provide data to the IncomingMessage.
* This should not normally be called by a user.
* @param payloadData The completed message to insert into the IncomingMessage
* @return true is the data was accepted, otherwise false.
*/
public boolean receiveData(byte [] payloadData) {
// We assume the packet was for us, etc. But we need to
// know it contains the correct contents
if (isExpectedPacketType(payloadData[0])) {
this.payload = (byte[])payloadData.clone();
return true;
} else {
// That's not what we were after, so discard and wait for more
return false;
}
}
}
|