Skip to content

Commit

Permalink
* Initial MySensors proof of concept module #2
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Sobiech <[email protected]>
  • Loading branch information
psobiech committed Feb 9, 2024
1 parent 5906e8f commit dade3c5
Show file tree
Hide file tree
Showing 11 changed files with 582 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantLock;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.psobiech.opengr8on.exceptions.UncheckedInterruptedException;
import pl.psobiech.opengr8on.exceptions.UnexpectedException;

Expand Down Expand Up @@ -84,6 +86,8 @@ public static UDPSocket udpRandomPort(InetAddress address) {
* TCP socket wrapper
*/
public static class TCPClientSocket implements Closeable {
private static final Logger LOGGER = LoggerFactory.getLogger(TCPClientSocket.class);

/**
* Local network address
*/
Expand Down Expand Up @@ -113,23 +117,35 @@ public TCPClientSocket(InetAddress address, int port) {
this.port = port;
}

public void send(byte[] buffer) throws IOException {
public void send(byte[]... buffers) throws IOException {
ensureConnected();

try {
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer);
for (byte[] buffer : buffers) {
outputStream.write(buffer);
}

outputStream.flush();
} catch (SocketException e) {
if (UncheckedInterruptedException.wasSocketInterrupted(e)) {
throw new UncheckedInterruptedException(e);
}

// TODO: proper retry / broken pipe handling
disconnect();
ensureConnected();

try {
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer);
for (byte[] buffer : buffers) {
outputStream.write(buffer);
}

outputStream.flush();
} catch (Exception e2) {
LOGGER.error(e2.getMessage(), e2);

throw e;
}
}
Expand Down Expand Up @@ -207,7 +223,7 @@ private void ensureConnected() throws IOException {
public void connect() throws IOException {
socketLock.lock();
try {
this.socket.connect(new InetSocketAddress(address, port), DEFAULT_TIMEOUT_MILLISECONDS);
socket.connect(new InetSocketAddress(address, port), DEFAULT_TIMEOUT_MILLISECONDS);
} finally {
socketLock.unlock();
}
Expand All @@ -217,7 +233,7 @@ public void connect() throws IOException {
public void close() {
socketLock.lock();
try {
IOUtil.closeQuietly(this.socket);
IOUtil.closeQuietly(socket);
} finally {
socketLock.unlock();
}
Expand Down
16 changes: 16 additions & 0 deletions modules/common/src/main/java/pl/psobiech/opengr8on/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

import pl.psobiech.opengr8on.exceptions.UncheckedInterruptedException;

public final class Util {
private static final long NANOS_IN_MILLISECOND = TimeUnit.MILLISECONDS.toNanos(1);

private Util() {
// NOP
}
Expand Down Expand Up @@ -176,4 +181,15 @@ public static <V1, V2> String stringifyMap(

return sb.toString();
}

public static void sleepNanos(long nanoSeconds) {
final long millis = nanoSeconds / NANOS_IN_MILLISECOND;
final int nanos = (int) (nanoSeconds % NANOS_IN_MILLISECOND);

try {
Thread.sleep(millis, nanos);
} catch (InterruptedException e) {
throw new UncheckedInterruptedException(e);
}
}
}
Loading

0 comments on commit dade3c5

Please sign in to comment.