just refactoring

This commit is contained in:
rusefillc 2022-02-10 22:45:30 -05:00
parent e51e41bfc9
commit 9a7713966d
6 changed files with 15 additions and 14 deletions

View File

@ -30,25 +30,19 @@ public class IncomingDataBuffer {
} }
private static final int BUFFER_SIZE = 32768; private static final int BUFFER_SIZE = 32768;
private static String loggingPrefix; private final String loggingPrefix;
/** /**
* buffer for response bytes from controller * buffer for response bytes from controller
*/ */
private final CircularByteBuffer cbb; private final CircularByteBuffer cbb;
private final AbstractIoStream.StreamStats streamStats; private final AbstractIoStream.StreamStats streamStats;
public IncomingDataBuffer(AbstractIoStream.StreamStats streamStats) { public IncomingDataBuffer(String loggingPrefix, AbstractIoStream.StreamStats streamStats) {
this.loggingPrefix = loggingPrefix;
this.streamStats = Objects.requireNonNull(streamStats, "streamStats"); this.streamStats = Objects.requireNonNull(streamStats, "streamStats");
this.cbb = new CircularByteBuffer(BUFFER_SIZE); this.cbb = new CircularByteBuffer(BUFFER_SIZE);
} }
public static IncomingDataBuffer createDataBuffer(String loggingPrefix, IoStream stream) {
IncomingDataBuffer.loggingPrefix = loggingPrefix;
IncomingDataBuffer incomingData = new IncomingDataBuffer(stream.getStreamStats());
stream.setInputListener(incomingData::addData);
return incomingData;
}
public byte[] getPacket(String msg) throws EOFException { public byte[] getPacket(String msg) throws EOFException {
return getPacket(msg, false, System.currentTimeMillis()); return getPacket(msg, false, System.currentTimeMillis());
} }
@ -104,7 +98,7 @@ public class IncomingDataBuffer {
synchronized (cbb) { synchronized (cbb) {
if (cbb.size() - cbb.length() < freshData.length) { if (cbb.size() - cbb.length() < freshData.length) {
log.error("buffer overflow not expected"); log.error("buffer overflow not expected");
cbb.clear(); throw new IllegalStateException("buffer overflow not expected");
} }
cbb.put(freshData); cbb.put(freshData);
cbb.notifyAll(); cbb.notifyAll();

View File

@ -40,7 +40,7 @@ public class Elm327IoStream extends AbstractIoStream {
private Elm327IoStream(Elm327Connector con, String loggingPrefix, DisconnectListener disconnectListener) { private Elm327IoStream(Elm327Connector con, String loggingPrefix, DisconnectListener disconnectListener) {
this.con = con; this.con = con;
this.disconnectListener = disconnectListener; this.disconnectListener = disconnectListener;
this.dataBuffer = IncomingDataBuffer.createDataBuffer(loggingPrefix, this); this.dataBuffer = this.createDataBuffer(loggingPrefix);
// ByteBuffer inBuf = ByteBuffer.allocate(OUT_BUFFER_SIZE); // ByteBuffer inBuf = ByteBuffer.allocate(OUT_BUFFER_SIZE);
outBuf = ByteBuffer.allocate(OUT_BUFFER_SIZE); outBuf = ByteBuffer.allocate(OUT_BUFFER_SIZE);

View File

@ -1,5 +1,6 @@
package com.rusefi.io.serial; package com.rusefi.io.serial;
import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.io.IoStream; import com.rusefi.io.IoStream;
import java.io.IOException; import java.io.IOException;
@ -12,6 +13,12 @@ public abstract class AbstractIoStream implements IoStream {
private final AtomicInteger bytesOut = new AtomicInteger(); private final AtomicInteger bytesOut = new AtomicInteger();
private long latestActivity; private long latestActivity;
public IncomingDataBuffer createDataBuffer(String loggingPrefix) {
IncomingDataBuffer incomingData = new IncomingDataBuffer(loggingPrefix, getStreamStats());
setInputListener(incomingData::addData);
return incomingData;
}
@Override @Override
public StreamStats getStreamStats() { public StreamStats getStreamStats() {
return streamStats; return streamStats;

View File

@ -22,7 +22,7 @@ public class BufferedSerialIoStream extends SerialIoStream {
*/ */
private BufferedSerialIoStream(SerialPort sp, String port) { private BufferedSerialIoStream(SerialPort sp, String port) {
super(sp, port); super(sp, port);
this.dataBuffer = IncomingDataBuffer.createDataBuffer("[serial] ", this); this.dataBuffer = this.createDataBuffer("[serial] ");
} }
@Override @Override

View File

@ -79,7 +79,7 @@ public class PCanIoStream extends AbstractIoStream {
public PCanIoStream(PCANBasic can) { public PCanIoStream(PCANBasic can) {
this.can = can; this.can = can;
this.dataBuffer = IncomingDataBuffer.createDataBuffer("", this); this.dataBuffer = this.createDataBuffer("");
} }
@Override @Override

View File

@ -36,7 +36,7 @@ public class TcpIoStream extends AbstractIoStream {
InputStream input = new BufferedInputStream(socket.getInputStream()); InputStream input = new BufferedInputStream(socket.getInputStream());
this.output = new BufferedOutputStream(socket.getOutputStream()); this.output = new BufferedOutputStream(socket.getOutputStream());
this.input = input; this.input = input;
this.dataBuffer = IncomingDataBuffer.createDataBuffer(loggingPrefix, this); this.dataBuffer = this.createDataBuffer(loggingPrefix);
} }
@NotNull @NotNull