Skip to content

Commit

Permalink
* Initial MySensors proof of concept module
Browse files Browse the repository at this point in the history
Signed-off-by: Piotr Sobiech <[email protected]>
  • Loading branch information
psobiech committed Feb 7, 2024
1 parent 86aaf3c commit 5906e8f
Show file tree
Hide file tree
Showing 14 changed files with 1,244 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ COPY modules/lib/pom.xml modules/lib/
COPY modules/parsers/pom.xml modules/parsers/
COPY modules/tftp/pom.xml modules/tftp/
COPY modules/client/pom.xml modules/client/
COPY modules/mysensors/pom.xml modules/mysensors/
COPY modules/vclu/pom.xml modules/vclu/

COPY assembly/jar-with-dependencies.xml assembly/

# https://issues.apache.org/jira/browse/MDEP-689
#RUN mvn -B -T 4 dependency:go-offline
RUN mvn -B -T 4 -pl '!modules/client' compile -Dorg.slf4j.simpleLogger.defaultLogLevel=ERROR -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true
RUN mvn -B -T 4 -pl '!modules/client,!modules/mysensors' compile -Dorg.slf4j.simpleLogger.defaultLogLevel=ERROR -Dmaven.test.skip=true -Dmaven.site.skip=true -Dmaven.source.skip=true -Dmaven.javadoc.skip=true

FROM app-deps AS app-build

Expand Down
2 changes: 1 addition & 1 deletion badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ public final class FileUtil {

private static final TemporaryFileTracker FILE_TRACKER = new TemporaryFileTracker();

public static final String CR = Character.toString(0x0D);
public static final byte CR_CODE_POINT = 0x0D;

public static final String LF = Character.toString(0x0A);
public static final String CR = Character.toString(CR_CODE_POINT);

public static final byte LF_CODE_POINT = 0x0A;

public static final String LF = Character.toString(LF_CODE_POINT);

public static final String CRLF = CR + LF;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.time.Duration;
Expand All @@ -39,7 +42,7 @@
import pl.psobiech.opengr8on.exceptions.UnexpectedException;

/**
* Common socket operations. Currently only UDP.
* Common socket operations.
*/
public class SocketUtil {
/**
Expand All @@ -59,6 +62,12 @@ private SocketUtil() {
// NOP
}

public static TCPClientSocket tcpClient(InetAddress address, int port) {
return new TCPClientSocket(
address, port
);
}

public static UDPSocket udpListener(InetAddress address, int port) {
return new UDPSocket(
address, port, false
Expand All @@ -71,6 +80,150 @@ public static UDPSocket udpRandomPort(InetAddress address) {
);
}

/**
* TCP socket wrapper
*/
public static class TCPClientSocket implements Closeable {
/**
* Local network address
*/
private final InetAddress address;

/**
* Local port
*/
private final int port;

/**
* Socket access lock
*/
private final ReentrantLock socketLock = new ReentrantLock();

/**
* Raw network socket
*/
private Socket socket;

/**
* @param address local address to bind on
* @param port port to listen on
*/
public TCPClientSocket(InetAddress address, int port) {
this.address = address;
this.port = port;
}

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

try {
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer);
outputStream.flush();
} catch (SocketException e) {
// TODO: proper retry / broken pipe handling
disconnect();
ensureConnected();

try {
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(buffer);
outputStream.flush();
} catch (Exception e2) {
throw e;
}
}
}

/**
* Disconnects the socket
*/
public void disconnect() {
socketLock.lock();
try {
// normal connect does not work for broken connections, we need to recreate socket
close();
open();
} finally {
socketLock.unlock();
}
}

/**
* Open the socket
*/
public void open() {
socketLock.lock();
try {
this.socket = new Socket();
this.socket.setTcpNoDelay(true);
this.socket.setKeepAlive(true);
this.socket.setSoTimeout(DEFAULT_TIMEOUT_MILLISECONDS);
} catch (SocketException e) {
if (UncheckedInterruptedException.wasSocketInterrupted(e)) {
throw new UncheckedInterruptedException(e);
}

throw new UnexpectedException(e);
} finally {
socketLock.unlock();
}
}

/**
* @return current local address
*/
public InetAddress getLocalAddress() {
socketLock.lock();
try {
return socket.getLocalAddress();
} finally {
socketLock.unlock();
}
}

public InputStream getInputStream() throws IOException {
socketLock.lock();
try {
ensureConnected();

return socket.getInputStream();
} finally {
socketLock.unlock();
}
}

private void ensureConnected() throws IOException {
socketLock.lock();
try {
if (!socket.isConnected()) {
connect();
}
} finally {
socketLock.unlock();
}
}

public void connect() throws IOException {
socketLock.lock();
try {
this.socket.connect(new InetSocketAddress(address, port), DEFAULT_TIMEOUT_MILLISECONDS);
} finally {
socketLock.unlock();
}
}

@Override
public void close() {
socketLock.lock();
try {
IOUtil.closeQuietly(this.socket);
} finally {
socketLock.unlock();
}
}
}

/**
* UDP socket wrapper
*/
Expand Down Expand Up @@ -128,7 +281,11 @@ public void open() {
this.socket = new DatagramSocket(new InetSocketAddress(address, port));
this.socket.setBroadcast(broadcast);
this.socket.setSoTimeout(DEFAULT_TIMEOUT_MILLISECONDS);
} catch (IOException e) {
} catch (SocketException e) {
if (UncheckedInterruptedException.wasSocketInterrupted(e)) {
throw new UncheckedInterruptedException(e);
}

throw new UnexpectedException(e);
} finally {
socketLock.unlock();
Expand Down
70 changes: 70 additions & 0 deletions modules/mysensors/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ OpenGr8on, open source extensions to systems based on Grenton devices
~ Copyright (C) 2023 Piotr Sobiech
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU Affero General Public License as
~ published by the Free Software Foundation, either version 3 of the
~ License, or (at your option) any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU Affero General Public License for more details.
~
~ You should have received a copy of the GNU Affero General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>pl.psobiech.opengr8on</groupId>
<artifactId>parent</artifactId>
<version>0.5.0-SNAPSHOT</version>

<relativePath>../parent</relativePath>
</parent>

<artifactId>mysensors</artifactId>

<dependencies>
<dependency>
<groupId>pl.psobiech.opengr8on</groupId>
<artifactId>common</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>../</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>LICENSE.txt</include>
<include>THIRD-PARTY.txt</include>
</includes>
</resource>
</resources>
</build>
</project>
Loading

0 comments on commit 5906e8f

Please sign in to comment.