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 String loggingPrefix;
private final String loggingPrefix;
/**
* buffer for response bytes from controller
*/
private final CircularByteBuffer cbb;
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.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 {
return getPacket(msg, false, System.currentTimeMillis());
}
@ -104,7 +98,7 @@ public class IncomingDataBuffer {
synchronized (cbb) {
if (cbb.size() - cbb.length() < freshData.length) {
log.error("buffer overflow not expected");
cbb.clear();
throw new IllegalStateException("buffer overflow not expected");
}
cbb.put(freshData);
cbb.notifyAll();

View File

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

View File

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

View File

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

View File

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

View File

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