summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/comms/port/testhandlers/TestDevice.java
blob: d33730b4498e0508a4ceec143d18b8d74207cf82 (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
package org.reprap.comms.port.testhandlers;

import java.io.IOException;
import java.io.PipedOutputStream;

import org.reprap.comms.snap.SNAPPacket;

public abstract class TestDevice {
	
	public static final int version = 0x100;

	public void receivePacket(PipedOutputStream out, SNAPPacket packet) throws Exception {
		byte payload [] = packet.getPayload();
		switch(payload[0]) {
		case 0:
			reply(out, packet, new byte [] {0, version & 0xff, (version & 0xff00) >> 8});
			return;
		}
		
		throw new Exception("Unknown packet type " + (int)payload[0] + " on address " + packet.getDestinationAddress().getAddress());
	}
	
	protected void reply(PipedOutputStream out, SNAPPacket packet, byte [] payload) throws IOException {
		System.out.println("Sending reply");
		SNAPPacket response = new SNAPPacket(packet.getDestinationAddress(),
				packet.getSourceAddress(), payload);
		out.write(response.getRawData());
		out.flush();
	}
	
}