summaryrefslogtreecommitdiff
path: root/branches/sm-unittesting/src/org/reprap/comms/port/TestPort.java
blob: e25581b486d5a61a59a48d619c3f588f66fecea3 (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
64
65
66
67
package org.reprap.comms.port;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.reprap.comms.port.testhandlers.TestDevice;
import org.reprap.comms.snap.SNAPAddress;

public class TestPort implements Port {
	
	private PipedOutputStream out;
	private PipedInputStream in;
	
	private TestPortHandler handler;
	private Thread handlerThread;
	
	public TestPort() throws IOException {
		out = new PipedOutputStream();
		in = new PipedInputStream();
		
		PipedInputStream farsideInput = new PipedInputStream();
		PipedOutputStream farsideOutput = new PipedOutputStream();
		out.connect(farsideInput);
		in.connect(farsideOutput);

		handler = new TestPortHandler(farsideInput, farsideOutput);

		handlerThread = new Thread() {
			public void run() {
				handler.watch();
			}
		};
		handlerThread.start();

	}
	
	public void dispose() {
	}
	
	public OutputStream getOutputStream() throws IOException {
		return out;
	}

	public InputStream getInputStream() throws IOException {
		return in;
	}

	public void close() {
	}
	
	/**
	 * Add a device onto the port
	 * @param device
	 * @param address
	 */
	public void addDevice(TestDevice device, SNAPAddress address) {
	    handler.registerDevice(address, device);
	}
	
	public void dropNextIncomingPacket() {
		handler.dropNextIncomingPacket();
	}

}