auto-sync

This commit is contained in:
rusEfi 2015-03-11 22:05:01 -05:00
parent 75b6d93918
commit 5fd6f2eff4
6 changed files with 75 additions and 32 deletions

View File

@ -17,6 +17,21 @@ public enum FileLog {
private static final String DIR = "out/"; private static final String DIR = "out/";
public static final String END_OF_TIMESTAND_TAG = "<EOT>: "; public static final String END_OF_TIMESTAND_TAG = "<EOT>: ";
public static final Logger LOGGER = new Logger() {
@Override
public void trace(String msg) {
}
@Override
public void info(String msg) {
rlog(msg);
}
@Override
public void error(String msg) {
rlog(msg);
}
};
@Nullable @Nullable
private OutputStream fileLog; // null if not opened yet or already closed private OutputStream fileLog; // null if not opened yet or already closed

View File

@ -5,12 +5,10 @@ package com.rusefi;
*/ */
public interface Timeouts { public interface Timeouts {
int SECOND = 1000; int SECOND = 1000;
int COMMAND_TIMEOUT_SEC = 10; // seconds int COMMAND_TIMEOUT_SEC = 10; // seconds
int RESTART_DELAY = 20 * SECOND; int RESTART_DELAY = 20 * SECOND;
int BINARY_IO_TIMEOUT = 5 * SECOND;
int BINARY_IO_TIMEOUT = 10 * SECOND;
int CMD_TIMEOUT = 20; int CMD_TIMEOUT = 20;
int READ_IMAGE_TIMEOUT = 60 * SECOND;
} }

View File

@ -73,6 +73,12 @@ public class BinaryProtocol {
} }
public void switchToBinaryProtocol() { public void switchToBinaryProtocol() {
// we do not have reliable implementation yet :(
for (int i = 0; i < 15; i++)
doSwitchToBinary();
}
private void doSwitchToBinary() {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (true) { while (true) {
@ -81,12 +87,18 @@ public class BinaryProtocol {
try { try {
serialPort.writeBytes("~\n".getBytes()); serialPort.writeBytes("~\n".getBytes());
synchronized (cbb) { synchronized (cbb) {
waitForBytes(2, start); boolean isTimeout = waitForBytes(2, start, "switch to binary");
if (isTimeout) {
close();
System.out.println("Timeout waiting for switch response");
return;
}
int response = cbb.getShort(); int response = cbb.getShort();
if (response != SWITCH_TO_BINARY_RESPONSE) { if (response != SWITCH_TO_BINARY_RESPONSE) {
logger.error(String.format("Unexpected response [%x], re-trying", response)); logger.error(String.format("Unexpected response [%x], re-trying", response));
continue; continue;
} }
logger.info("Switched to binary protocol");
} }
} catch (SerialPortException | EOFException e) { } catch (SerialPortException | EOFException e) {
close(); close();
@ -95,7 +107,6 @@ public class BinaryProtocol {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
logger.info("Switched to binary protocol");
break; break;
} }
} }
@ -165,20 +176,23 @@ public class BinaryProtocol {
return (((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000); return (((x) >> 24) & 0xff) | (((x) << 8) & 0xff0000) | (((x) >> 8) & 0xff00) | (((x) << 24) & 0xff000000);
} }
private byte[] receivePacket() throws InterruptedException, EOFException { private byte[] receivePacket(String msg, boolean allowLongResponse) throws InterruptedException, EOFException {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
synchronized (cbb) { synchronized (cbb) {
waitForBytes(2, start); boolean isTimeout = waitForBytes(2, start, msg + " header");
if (isTimeout)
return null;
int packetSize = BinaryProtocol.swap16(cbb.getShort()); int packetSize = BinaryProtocol.swap16(cbb.getShort());
logger.trace("Got packet size " + packetSize); logger.trace("Got packet size " + packetSize);
if (packetSize < 0 if (packetSize < 0)
// || packetSize > 300 return null;
) { if (!allowLongResponse && packetSize > BLOCKING_FACTOR + 10)
// invalid packet size return null;
isTimeout = waitForBytes(packetSize + 4, start, msg + " body");
if (isTimeout)
return null; return null;
}
waitForBytes(packetSize + 4, start);
byte[] packet = new byte[packetSize]; byte[] packet = new byte[packetSize];
int packetCrc; int packetCrc;
@ -204,7 +218,9 @@ public class BinaryProtocol {
int offset = 0; int offset = 0;
while (offset < image.getSize()) { long start = System.currentTimeMillis();
while (offset < image.getSize() && (System.currentTimeMillis() - start < Timeouts.READ_IMAGE_TIMEOUT)) {
if (isClosed) if (isClosed)
return; return;
@ -217,7 +233,7 @@ public class BinaryProtocol {
putShort(packet, 3, swap16(offset)); putShort(packet, 3, swap16(offset));
putShort(packet, 5, swap16(requestSize)); putShort(packet, 5, swap16(requestSize));
byte[] response = exchange(packet); byte[] response = exchange(packet, "load image", false);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) { if (!checkResponseCode(response, RESPONSE_OK) || response.length != requestSize + 1) {
logger.error("readImage: Something is wrong, retrying..."); logger.error("readImage: Something is wrong, retrying...");
@ -238,17 +254,17 @@ public class BinaryProtocol {
* *
* @return null in case of IO issues * @return null in case of IO issues
*/ */
public byte[] exchange(byte[] packet) { public byte[] exchange(byte[] packet, String msg, boolean allowLongResponse) {
if (isClosed) if (isClosed)
return null; return null;
dropPending(); dropPending();
try { try {
sendCrcPacket(packet); sendCrcPacket(packet);
return receivePacket(); return receivePacket(msg, allowLongResponse);
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} catch (SerialPortException | EOFException e) { } catch (SerialPortException | EOFException e) {
logger.error("exchange failed: " + e); logger.error(msg + ": exchange failed: " + e);
close(); close();
return null; return null;
} }
@ -282,7 +298,7 @@ public class BinaryProtocol {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) {
byte[] response = exchange(packet); byte[] response = exchange(packet, "writeImage", false);
if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_OK) || response.length != 1) {
logger.error("writeData: Something is wrong, retrying..."); logger.error("writeData: Something is wrong, retrying...");
continue; continue;
@ -299,7 +315,7 @@ public class BinaryProtocol {
while (true) { while (true) {
if (isClosed) if (isClosed)
return; return;
byte[] response = exchange(new byte[]{'B'}); byte[] response = exchange(new byte[]{'B'}, "burn", false);
if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_BURN_OK) || response.length != 1) {
continue; continue;
} }
@ -326,8 +342,8 @@ public class BinaryProtocol {
/** /**
* @return true in case of timeout, false if everything is fine * @return true in case of timeout, false if everything is fine
*/ */
private boolean waitForBytes(int count, long start) throws InterruptedException { private boolean waitForBytes(int count, long start, String msg) throws InterruptedException {
logger.info("Waiting for " + count + " byte(s)"); logger.info("Waiting for " + count + " byte(s): " + msg);
synchronized (cbb) { synchronized (cbb) {
while (cbb.length() < count) { while (cbb.length() < count) {
int timeout = (int) (start + Timeouts.BINARY_IO_TIMEOUT - System.currentTimeMillis()); int timeout = (int) (start + Timeouts.BINARY_IO_TIMEOUT - System.currentTimeMillis());
@ -379,7 +395,7 @@ public class BinaryProtocol {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) { while (!isClosed && (System.currentTimeMillis() - start < Timeouts.BINARY_IO_TIMEOUT)) {
byte[] response = exchange(command); byte[] response = exchange(command, "execute", false);
if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) { if (!checkResponseCode(response, RESPONSE_COMMAND_OK) || response.length != 1) {
continue; continue;
} }
@ -393,7 +409,7 @@ public class BinaryProtocol {
return null; return null;
try { try {
byte[] response = new byte[0]; byte[] response = new byte[0];
response = exchange(new byte[]{'G'}); response = exchange(new byte[]{'G'}, "text", true);
if (response != null && response.length == 1) if (response != null && response.length == 1)
Thread.sleep(100); Thread.sleep(100);
// System.out.println(result); // System.out.println(result);

View File

@ -76,7 +76,7 @@ public class PortHolder {
portLock.notifyAll(); portLock.notifyAll();
} }
bp = new BinaryProtocol(Logger.STDOUT, serialPort); bp = new BinaryProtocol(FileLog.LOGGER, serialPort);
bp.switchToBinaryProtocol(); bp.switchToBinaryProtocol();
bp.readImage(BinaryProtocol.IMAGE_SIZE); bp.readImage(BinaryProtocol.IMAGE_SIZE);
@ -90,6 +90,7 @@ public class PortHolder {
@Override @Override
public void run() { public void run() {
while (!bp.isClosed) { while (!bp.isClosed) {
// FileLog.rlog("queue: " + LinkManager.COMMUNICATION_QUEUE.toString());
if (LinkManager.COMMUNICATION_QUEUE.isEmpty()) { if (LinkManager.COMMUNICATION_QUEUE.isEmpty()) {
LinkManager.COMMUNICATION_EXECUTOR.submit(new Runnable() { LinkManager.COMMUNICATION_EXECUTOR.submit(new Runnable() {
@Override @Override
@ -102,6 +103,7 @@ public class PortHolder {
} }
sleep(); sleep();
} }
FileLog.rlog("Stopping text pull");
} }
}; };
Thread tr = new Thread(textPull); Thread tr = new Thread(textPull);
@ -164,6 +166,11 @@ public class PortHolder {
public void run() { public void run() {
bp.sendTextCommand(command); bp.sendTextCommand(command);
} }
@Override
public String toString() {
return "Runnable for " + command;
}
}); });
try { try {

View File

@ -18,6 +18,7 @@ import java.lang.reflect.InvocationTargetException;
*/ */
public class UploadChanges { public class UploadChanges {
public static final Logger logger = createUiLogger(); public static final Logger logger = createUiLogger();
public static void main(String[] args) throws SerialPortException, InvocationTargetException, InterruptedException { public static void main(String[] args) throws SerialPortException, InvocationTargetException, InterruptedException {
if (args.length != 1) { if (args.length != 1) {
System.out.println("Exactly one parameter expected"); System.out.println("Exactly one parameter expected");
@ -69,6 +70,11 @@ public class UploadChanges {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
@Override
public String toString() {
return "Runnable for burn";
}
}); });
} }

View File

@ -9,17 +9,18 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
public class ConnectionWatchdog { public class ConnectionWatchdog {
static final Timer reconnectTimer = new Timer(Timeouts.RESTART_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LinkManager.restart();
reconnectTimer.restart();
}
});
private ConnectionWatchdog() { private ConnectionWatchdog() {
} }
public static void start() { public static void start() {
final Timer reconnectTimer = new Timer(Timeouts.RESTART_DELAY, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
LinkManager.restart();
}
});
reconnectTimer.restart(); reconnectTimer.restart();
LinkManager.engineState.timeListeners.add(new EngineTimeListener() { LinkManager.engineState.timeListeners.add(new EngineTimeListener() {