refactoring

This commit is contained in:
rusefi 2017-06-21 07:49:37 +03:00
parent 33ec84c120
commit 8d808c7b95
7 changed files with 22 additions and 14 deletions

View File

@ -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) {

View File

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

View File

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

View File

@ -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);

View File

@ -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));

View File

@ -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() {

View File

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