Skip to content

Commit

Permalink
Merge pull request #22 from 12urenloop/beacon_comm
Browse files Browse the repository at this point in the history
Beacon communication
  • Loading branch information
mcbloch authored Nov 7, 2019
2 parents 4a1c44d + 5d5e5ef commit 7125cbb
Show file tree
Hide file tree
Showing 13 changed files with 610 additions and 104 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ file("$rootProject.projectDir/src/main/resources/telraam/testConfig.properties")
task migrateTestingDatabase(type: FlywayMigrateTask) {
url = testProps.getProperty("DB_URL")
baselineOnMigrate = true
}
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.logging.level=info
5 changes: 2 additions & 3 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Sun Nov 03 23:02:12 CET 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
200 changes: 100 additions & 100 deletions gradlew.bat
Original file line number Diff line number Diff line change
@@ -1,100 +1,100 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega
57 changes: 57 additions & 0 deletions src/main/java/telraam/beacon/Beacon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package telraam.beacon;

import java.io.IOException;
import java.io.InputStream;
import java.io.EOFException;
import java.net.Socket;

/**
* Beacon is socket wrapper that listens to the sockets
* and emits BeaconMessages when enough bytes are read.
*
* Beacons are closed at the first Exception encountered.
* This could be changed if need be.
*
* @author Arthur Vercruysse
*/
public class Beacon extends EventGenerator<BeaconMessage> implements Runnable {
private Socket s;
private int messageSize = BeaconMessage.MESSAGESIZE;

public Beacon(Socket socket, Callback<Void, Event<BeaconMessage>> h) {
super(h);

this.s = socket;

new Thread(this).start();
}

public void run() {
this.connect();

byte[] buf = new byte[messageSize];
int at = 0;
InputStream is;

try {
is = s.getInputStream();
} catch (IOException e) {
error(e);
return;
}

try {
while (true) {
int c = is.read(buf, at, messageSize - at);
if (c < 0) throw new EOFException();
at += c;
if (at == messageSize) {
this.data(new BeaconMessage(buf));
at = 0;
}
}
} catch (IOException e) {
exit();
}
}
}
31 changes: 31 additions & 0 deletions src/main/java/telraam/beacon/BeaconAggregator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package telraam.beacon;

import java.io.IOException;

/**
* BeaconAggregator is the main class to handle aggregate BeaconMessages.
* Register listeners to data, errors, connects and disconnects.
*
* If port is negative no server is started (should only be used in tests).
*
* @author Arthur Vercruysse
*/
public class BeaconAggregator extends TCPFactory<BeaconMessage> implements Callback<Void, Event<BeaconMessage>> {

public BeaconAggregator(int port) throws IOException {
// Does not work, java can't handle cool code
// super((s) -> new Beacon(s, this), port);
super(port);
super.creator = (s) -> {
new Beacon(s, this);
return null;
};
}

public Void handle(Event<BeaconMessage> event) {
// this is the handler for event.
// Sending the data to the correct handlers set by TCPFactory
event.handle(this);
return null;
}
}
18 changes: 18 additions & 0 deletions src/main/java/telraam/beacon/BeaconMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package telraam.beacon;

/**
* BeaconMessage is the representation of what is received from a beacon.
* This should parse the incoming byte[].
*
* @author Arthur Vercruysse
*/
public class BeaconMessage {
public static final int MESSAGESIZE = 10;

public byte[] data;

// DO NOT STORE THIS DATA, IT WILL BE OVERWRITTEN
public BeaconMessage(byte[] data) {
this.data = data;
}
}
10 changes: 10 additions & 0 deletions src/main/java/telraam/beacon/Callback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package telraam.beacon;

/**
* Stupid interface for callbacks. You mind if I request Callback<Void, Void>?
*
* @author Arthur Vercruysse
*/
public interface Callback<Output, Input> {
public Output handle(Input value);
}
Loading

0 comments on commit 7125cbb

Please sign in to comment.