auto-sync

This commit is contained in:
rusEfi 2015-03-11 20:05:10 -05:00
parent 1cb86be932
commit 012be3d8a1
7 changed files with 43 additions and 15 deletions

View File

@ -5,7 +5,7 @@
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES> <SOURCES>
<root url="jar://$PROJECT_DIR$/lib/jssc-2.6.0-src.jar!/src" /> <root url="jar://$PROJECT_DIR$/lib/jssc-src.jar!/src" />
</SOURCES> </SOURCES>
</library> </library>
</component> </component>

View File

@ -38,6 +38,7 @@ public class BinaryProtocol {
// todo: fix this, this is HORRIBLE! // todo: fix this, this is HORRIBLE!
@Deprecated @Deprecated
public static BinaryProtocol instance; public static BinaryProtocol instance;
public boolean isClosed;
public BinaryProtocol(final Logger logger, SerialPort serialPort) { public BinaryProtocol(final Logger logger, SerialPort serialPort) {
this.logger = logger; this.logger = logger;
@ -67,6 +68,10 @@ public class BinaryProtocol {
} }
} }
public Logger getLogger() {
return logger;
}
public void switchToBinaryProtocol() { public void switchToBinaryProtocol() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
@ -189,6 +194,9 @@ public class BinaryProtocol {
int offset = 0; int offset = 0;
while (offset < image.getSize()) { while (offset < image.getSize()) {
if (isClosed)
return;
int remainingSize = image.getSize() - offset; int remainingSize = image.getSize() - offset;
int requestSize = Math.min(remainingSize, BLOCKING_FACTOR); int requestSize = Math.min(remainingSize, BLOCKING_FACTOR);
@ -201,7 +209,7 @@ public class BinaryProtocol {
byte[] response = exchange(packet); byte[] response = exchange(packet);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) { if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) {
logger.error("Something is wrong, retrying..."); logger.error("readImage: Something is wrong, retrying...");
continue; continue;
} }
@ -214,13 +222,22 @@ public class BinaryProtocol {
setController(image); setController(image);
} }
/**
* Blocking sending binary packet and waiting for a response
*
* @return null in case of IO issues
*/
public byte[] exchange(byte[] packet) { public byte[] exchange(byte[] packet) {
dropPending(); dropPending();
try { try {
sendCrcPacket(packet); sendCrcPacket(packet);
return receivePacket(); return receivePacket();
} catch (SerialPortException | InterruptedException | EOFException e) { } catch (InterruptedException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (SerialPortException | EOFException e) {
logger.error("exchange failed: " + e);
isClosed = true;
return null;
} }
} }
@ -244,7 +261,7 @@ public class BinaryProtocol {
while (true) { while (true) {
byte[] response = exchange(packet); byte[] response = exchange(packet);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) {
logger.error("Something is wrong, retrying..."); logger.error("writeData: Something is wrong, retrying...");
continue; continue;
} }
break; break;
@ -257,6 +274,8 @@ public class BinaryProtocol {
logger.info("Need to burn"); logger.info("Need to burn");
while (true) { while (true) {
if (isClosed)
return;
byte[] response = exchange(new byte[]{'B'}); byte[] response = exchange(new byte[]{'B'});
if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) {
continue; continue;
@ -275,6 +294,8 @@ public class BinaryProtocol {
public ConfigurationImage getController() { public ConfigurationImage getController() {
synchronized (lock) { synchronized (lock) {
if (controller == null)
return null;
return controller.clone(); return controller.clone();
} }
} }
@ -287,7 +308,7 @@ public class BinaryProtocol {
synchronized (cbb) { synchronized (cbb) {
while (cbb.length() < count) { while (cbb.length() < count) {
int timeout = (int) (start + TIMEOUT - System.currentTimeMillis()); int timeout = (int) (start + TIMEOUT - System.currentTimeMillis());
if (timeout < 0) { if (timeout <= 0) {
return true; // timeout. Sad face. return true; // timeout. Sad face.
} }
cbb.wait(timeout); cbb.wait(timeout);
@ -343,6 +364,8 @@ public class BinaryProtocol {
} }
public String requestText() { public String requestText() {
if (isClosed)
return null;
try { try {
byte[] response = new byte[0]; byte[] response = new byte[0];
response = exchange(new byte[]{'G'}); response = exchange(new byte[]{'G'});

View File

@ -26,6 +26,7 @@ public class PortHolder {
private static final int BAUD_RATE = 115200; private static final int BAUD_RATE = 115200;
private static final int SECOND = 1000; private static final int SECOND = 1000;
private static final int MINUTE = 60 * SECOND; private static final int MINUTE = 60 * SECOND;
private static final int COMMAND_TIMEOUT_SEC = 10; // seconds
private static PortHolder instance = new PortHolder(); private static PortHolder instance = new PortHolder();
private final Object portLock = new Object(); private final Object portLock = new Object();
@ -81,17 +82,20 @@ public class PortHolder {
bp.switchToBinaryProtocol(); bp.switchToBinaryProtocol();
bp.readImage(BinaryProtocol.IMAGE_SIZE); bp.readImage(BinaryProtocol.IMAGE_SIZE);
if (bp.isClosed)
return false;
Runnable textPull = new Runnable() { Runnable textPull = new Runnable() {
@Override @Override
public void run() { public void run() {
while (true) { while (!bp.isClosed) {
if (LinkManager.COMMUNICATION_QUEUE.isEmpty()) { if (LinkManager.COMMUNICATION_QUEUE.isEmpty()) {
LinkManager.COMMUNICATION_EXECUTOR.submit(new Runnable() { LinkManager.COMMUNICATION_EXECUTOR.submit(new Runnable() {
@Override @Override
public void run() { public void run() {
String text = bp.requestText(); String text = bp.requestText();
listener.onDataArrived((text + "\r\n").getBytes()); if (text != null)
listener.onDataArrived((text + "\r\n").getBytes());
} }
}); });
} }
@ -162,9 +166,12 @@ public class PortHolder {
}); });
try { try {
f.get(30, TimeUnit.SECONDS); f.get(COMMAND_TIMEOUT_SEC, TimeUnit.SECONDS);
} catch (ExecutionException | TimeoutException e) { } catch (ExecutionException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (TimeoutException e) {
bp.getLogger().error("timeout, giving up: " + e);
return;
} }
/** /**
* this here to make CommandQueue happy * this here to make CommandQueue happy

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -30,17 +30,15 @@ public class TableEditor extends JPanel {
add(editor.getContent()); add(editor.getContent());
BinaryProtocol instance = BinaryProtocol.instance;
ConfigurationImage image = BinaryProtocol.instance.getController(); if (instance == null)
throw new NullPointerException("instance");
ConfigurationImage image = instance.getController();
ECUEditor.openImage(image.getFileContent(), SettingsManager.getSettings().getEcuDefinitionFiles().elementAt(0), ECUEditor.openImage(image.getFileContent(), SettingsManager.getSettings().getEcuDefinitionFiles().elementAt(0),
"rusEfi"); "rusEfi");
} catch (Exception e) { } catch (Exception e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
UiUtils.trueLayout(this); UiUtils.trueLayout(this);
} }
} }