refactoring
This commit is contained in:
parent
33ec84c120
commit
8d808c7b95
|
@ -54,8 +54,11 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
|||
private ConfigurationImage controller;
|
||||
|
||||
public boolean isClosed;
|
||||
// todo: make a singleton?
|
||||
public static byte[] currentOutputs;
|
||||
/**
|
||||
* Snapshot of current gauges status
|
||||
* @see BinaryProtocolCommands#COMMAND_OUTPUTS
|
||||
*/
|
||||
public byte[] currentOutputs;
|
||||
|
||||
public BinaryProtocol(final Logger logger, IoStream stream) {
|
||||
this.logger = logger;
|
||||
|
@ -70,7 +73,7 @@ public class BinaryProtocol implements BinaryProtocolCommands {
|
|||
incomingData.addData(freshData);
|
||||
}
|
||||
};
|
||||
stream.setDataListener(streamDataListener);
|
||||
stream.setInputListener(streamDataListener);
|
||||
}
|
||||
|
||||
public BinaryProtocol(Logger logger, SerialPort serialPort) {
|
||||
|
|
|
@ -6,7 +6,7 @@ import com.opensr5.io.WriteStream;
|
|||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Physical controller communication level
|
||||
* Physical bi-directional controller communication level
|
||||
*
|
||||
* (c) Andrey Belomutskiy
|
||||
*
|
||||
|
@ -16,11 +16,14 @@ public interface IoStream extends WriteStream {
|
|||
/**
|
||||
* @param listener would be invoked from unknown implementation-dependent thread
|
||||
*/
|
||||
void setDataListener(DataListener listener);
|
||||
void setInputListener(DataListener listener);
|
||||
|
||||
boolean isClosed();
|
||||
|
||||
void close();
|
||||
|
||||
/**
|
||||
* purges pending input and output
|
||||
*/
|
||||
void purge();
|
||||
|
||||
boolean isClosed();
|
||||
}
|
||||
|
|
|
@ -59,7 +59,7 @@ public class SerialIoStream implements IoStream {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setDataListener(DataListener listener) {
|
||||
public void setInputListener(DataListener listener) {
|
||||
try {
|
||||
SerialPortReader reader = new SerialPortReader(serialPort, listener);
|
||||
serialPort.addEventListener(reader.getSerialPortEventListener());
|
||||
|
|
|
@ -136,7 +136,8 @@ public class BinaryProtocolServer implements BinaryProtocolCommands {
|
|||
|
||||
byte[] response = new byte[1 + Fields.TS_OUTPUT_SIZE];
|
||||
response[0] = (byte) TS_OK.charAt(0);
|
||||
byte[] currentOutputs = BinaryProtocol.currentOutputs;
|
||||
BinaryProtocol bp = BinaryProtocolHolder.getInstance().get();
|
||||
byte[] currentOutputs = bp.currentOutputs;
|
||||
if (currentOutputs != null)
|
||||
System.arraycopy(currentOutputs, 1, response, 1, Fields.TS_OUTPUT_SIZE);
|
||||
BinaryProtocol.sendPacket(response, FileLog.LOGGER, stream);
|
||||
|
|
|
@ -107,7 +107,7 @@ public class TcpConnector implements LinkConnector {
|
|||
rb.append(new String(freshData), LinkManager.ENCODER);
|
||||
}
|
||||
};
|
||||
// ioStream.setDataListener(listener1);
|
||||
// ioStream.setInputListener(listener1);
|
||||
|
||||
bp = new BinaryProtocol(FileLog.LOGGER, new TcpIoStream(stream, os));
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ public class TcpIoStream implements IoStream {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void setDataListener(final DataListener listener) {
|
||||
public void setInputListener(final DataListener listener) {
|
||||
LinkManager.IO_EXECUTOR.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
|
|
@ -14,9 +14,10 @@ import java.io.ByteArrayOutputStream;
|
|||
class BinaryProtocolServerSandbox {
|
||||
public static void main(String[] args) {
|
||||
TcpIoStream stream = new TcpIoStream(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream());
|
||||
BinaryProtocolHolder.instance = new BinaryProtocol(FileLog.LOGGER, stream);
|
||||
BinaryProtocolHolder.getInstance().get().setController(new ConfigurationImage(new byte[14008]));
|
||||
BinaryProtocol.currentOutputs = new byte[1 + Fields.TS_OUTPUT_SIZE];
|
||||
BinaryProtocol bp = new BinaryProtocol(FileLog.LOGGER, stream);
|
||||
BinaryProtocolHolder.instance = bp;
|
||||
bp.setController(new ConfigurationImage(new byte[14008]));
|
||||
bp.currentOutputs = new byte[1 + Fields.TS_OUTPUT_SIZE];
|
||||
BinaryProtocolServer.start();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue