reducing complexity
This commit is contained in:
parent
0c0eefbb4f
commit
a0a163b8e5
|
@ -2,6 +2,7 @@ package com.rusefi.binaryprotocol;
|
||||||
|
|
||||||
import com.opensr5.Logger;
|
import com.opensr5.Logger;
|
||||||
import com.rusefi.io.IoStream;
|
import com.rusefi.io.IoStream;
|
||||||
|
import com.rusefi.io.LinkManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* At any given moment of time JVM manages one communication stream
|
* At any given moment of time JVM manages one communication stream
|
||||||
|
@ -26,6 +27,6 @@ public enum BinaryProtocolHolder {
|
||||||
}
|
}
|
||||||
|
|
||||||
public BinaryProtocol getCurrentStreamState() {
|
public BinaryProtocol getCurrentStreamState() {
|
||||||
return currentStream;
|
return LinkManager.connector.getBinaryProtocol();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.rusefi.io;
|
package com.rusefi.io;
|
||||||
|
|
||||||
|
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andrey Belomutskiy
|
* @author Andrey Belomutskiy
|
||||||
* 3/3/14
|
* 3/3/14
|
||||||
|
@ -18,6 +20,11 @@ public interface LinkConnector extends LinkDecoder {
|
||||||
public void restart() {
|
public void restart() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BinaryProtocol getBinaryProtocol() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String unpack(String packet) {
|
public String unpack(String packet) {
|
||||||
return LinkDecoder.TEXT_PROTOCOL_DECODER.unpack(packet);
|
return LinkDecoder.TEXT_PROTOCOL_DECODER.unpack(packet);
|
||||||
|
@ -30,4 +37,5 @@ public interface LinkConnector extends LinkDecoder {
|
||||||
|
|
||||||
void restart();
|
void restart();
|
||||||
|
|
||||||
|
BinaryProtocol getBinaryProtocol();
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,6 +126,7 @@ public class LinkManager {
|
||||||
ConnectionWatchdog.onDataArrived();
|
ConnectionWatchdog.onDataArrived();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
public static LinkConnector connector;
|
public static LinkConnector connector;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -25,14 +25,12 @@ public class PortHolder {
|
||||||
public ConnectionStateListener listener;
|
public ConnectionStateListener listener;
|
||||||
private final Object portLock = new Object();
|
private final Object portLock = new Object();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
private BinaryProtocol bp;
|
private BinaryProtocol bp;
|
||||||
|
|
||||||
protected PortHolder() {
|
protected PortHolder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private IoStream serialPort;
|
|
||||||
|
|
||||||
public String port;
|
public String port;
|
||||||
|
|
||||||
boolean connectAndReadConfiguration() {
|
boolean connectAndReadConfiguration() {
|
||||||
|
@ -43,12 +41,10 @@ public class PortHolder {
|
||||||
|
|
||||||
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
|
IoStream stream = SerialIoStreamJSerialComm.openPort(port);
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
this.serialPort = stream;
|
bp = BinaryProtocolHolder.getInstance().create(FileLog.LOGGER, stream);
|
||||||
portLock.notifyAll();
|
portLock.notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
bp = BinaryProtocolHolder.getInstance().create(FileLog.LOGGER, stream);
|
|
||||||
|
|
||||||
boolean result = bp.connectAndReadConfiguration(dataListener);
|
boolean result = bp.connectAndReadConfiguration(dataListener);
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
if (result) {
|
if (result) {
|
||||||
|
@ -62,10 +58,10 @@ public class PortHolder {
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
synchronized (portLock) {
|
synchronized (portLock) {
|
||||||
if (serialPort != null) {
|
if (bp != null) {
|
||||||
try {
|
try {
|
||||||
serialPort.close();
|
bp.close();
|
||||||
serialPort = null;
|
bp = null;
|
||||||
} finally {
|
} finally {
|
||||||
portLock.notifyAll();
|
portLock.notifyAll();
|
||||||
}
|
}
|
||||||
|
@ -73,6 +69,11 @@ public class PortHolder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public BinaryProtocol getBp() {
|
||||||
|
return bp;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this method blocks till a connection is available
|
* this method blocks till a connection is available
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package com.rusefi.io.serial;
|
package com.rusefi.io.serial;
|
||||||
|
|
||||||
import com.rusefi.FileLog;
|
import com.rusefi.FileLog;
|
||||||
|
import com.rusefi.binaryprotocol.BinaryProtocol;
|
||||||
import com.rusefi.core.MessagesCentral;
|
import com.rusefi.core.MessagesCentral;
|
||||||
import com.rusefi.io.ConnectionStateListener;
|
import com.rusefi.io.ConnectionStateListener;
|
||||||
import com.rusefi.io.LinkConnector;
|
import com.rusefi.io.LinkConnector;
|
||||||
|
@ -32,6 +33,11 @@ public class SerialConnector implements LinkConnector {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BinaryProtocol getBinaryProtocol() {
|
||||||
|
return portHolder.getBp();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void restart() {
|
public void restart() {
|
||||||
LinkManager.execute(new Runnable() {
|
LinkManager.execute(new Runnable() {
|
||||||
|
|
|
@ -37,6 +37,10 @@ public class TcpConnector implements LinkConnector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public BinaryProtocol getBinaryProtocol() {
|
||||||
|
return bp;
|
||||||
|
}
|
||||||
|
|
||||||
public static boolean isTcpPort(String port) {
|
public static boolean isTcpPort(String port) {
|
||||||
try {
|
try {
|
||||||
getTcpPort(port);
|
getTcpPort(port);
|
||||||
|
|
Loading…
Reference in New Issue