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