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(); } }