auto-sync

This commit is contained in:
rusEfi 2016-01-31 18:03:29 -05:00
parent 4813d65f7a
commit 05ec1f3a7f
1 changed files with 11 additions and 3 deletions

View File

@ -18,12 +18,14 @@ public class TcpConnector implements LinkConnector {
public final static int DEFAULT_PORT = 29001; public final static int DEFAULT_PORT = 29001;
public static final String LOCALHOST = "localhost"; public static final String LOCALHOST = "localhost";
private final int port; private final int port;
private final String hostname;
// private IoStream ioStream; // private IoStream ioStream;
private BinaryProtocol bp; private BinaryProtocol bp;
public TcpConnector(String port) { public TcpConnector(String port) {
try { try {
this.port = getTcpPort(port); this.port = getTcpPort(port);
this.hostname = getHostname(port);
} catch (InvalidTcpPort e) { } catch (InvalidTcpPort e) {
throw new IllegalStateException("Unexpected", e); throw new IllegalStateException("Unexpected", e);
} }
@ -56,18 +58,24 @@ public class TcpConnector implements LinkConnector {
} }
return confirmation.substring(0, length); return confirmation.substring(0, length);
} }
*/ */
static class InvalidTcpPort extends Exception { static class InvalidTcpPort extends Exception {
} }
public static int getTcpPort(String port) throws InvalidTcpPort { public static int getTcpPort(String port) throws InvalidTcpPort {
try { try {
return Integer.parseInt(port); String[] portParts = port.split(":");
int index = (portParts.length == 1 ? 0 : 1);
return Integer.parseInt(portParts[index]);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw new InvalidTcpPort(); throw new InvalidTcpPort();
} }
} }
private static String getHostname(String port) {
String[] portParts = port.split(":");
return (portParts.length == 1 ? LOCALHOST : portParts[0].length() > 0 ? portParts[0] : LOCALHOST);
}
/** /**
* this implementation is blocking * this implementation is blocking
*/ */
@ -77,7 +85,7 @@ public class TcpConnector implements LinkConnector {
OutputStream os; OutputStream os;
BufferedInputStream stream; BufferedInputStream stream;
try { try {
Socket socket = new Socket(LOCALHOST, port); Socket socket = new Socket(hostname, port);
os = socket.getOutputStream(); os = socket.getOutputStream();
stream = new BufferedInputStream(socket.getInputStream()); stream = new BufferedInputStream(socket.getInputStream());
// ioStream = new TcpIoStream(os, stream); // ioStream = new TcpIoStream(os, stream);