Move all files from java subdirectory to project root
This commit is contained in:
3
core.networking/build.gradle
Normal file
3
core.networking/build.gradle
Normal file
@@ -0,0 +1,3 @@
|
||||
dependencies {
|
||||
compile project(':core.worker')
|
||||
}
|
||||
BIN
core.networking/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
core.networking/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
6
core.networking/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
6
core.networking/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
#Mon Jul 11 23:15:34 BST 2016
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14-bin.zip
|
||||
164
core.networking/gradlew
vendored
Normal file
164
core.networking/gradlew
vendored
Normal file
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
##############################################################################
|
||||
##
|
||||
## Gradle start up script for UN*X
|
||||
##
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
# Resolve links: $0 may be a link
|
||||
PRG="$0"
|
||||
# Need this for relative symlinks.
|
||||
while [ -h "$PRG" ] ; do
|
||||
ls=`ls -ld "$PRG"`
|
||||
link=`expr "$ls" : '.*-> \(.*\)$'`
|
||||
if expr "$link" : '/.*' > /dev/null; then
|
||||
PRG="$link"
|
||||
else
|
||||
PRG=`dirname "$PRG"`"/$link"
|
||||
fi
|
||||
done
|
||||
SAVED="`pwd`"
|
||||
cd "`dirname \"$PRG\"`/" >/dev/null
|
||||
APP_HOME="`pwd -P`"
|
||||
cd "$SAVED" >/dev/null
|
||||
|
||||
APP_NAME="Gradle"
|
||||
APP_BASE_NAME=`basename "$0"`
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS=""
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD="maximum"
|
||||
|
||||
warn ( ) {
|
||||
echo "$*"
|
||||
}
|
||||
|
||||
die ( ) {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
}
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "`uname`" in
|
||||
CYGWIN* )
|
||||
cygwin=true
|
||||
;;
|
||||
Darwin* )
|
||||
darwin=true
|
||||
;;
|
||||
MINGW* )
|
||||
msys=true
|
||||
;;
|
||||
NONSTOP* )
|
||||
nonstop=true
|
||||
;;
|
||||
esac
|
||||
|
||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD="$JAVA_HOME/jre/sh/java"
|
||||
else
|
||||
JAVACMD="$JAVA_HOME/bin/java"
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD="java"
|
||||
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
|
||||
MAX_FD_LIMIT=`ulimit -H -n`
|
||||
if [ $? -eq 0 ] ; then
|
||||
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
|
||||
MAX_FD="$MAX_FD_LIMIT"
|
||||
fi
|
||||
ulimit -n $MAX_FD
|
||||
if [ $? -ne 0 ] ; then
|
||||
warn "Could not set maximum file descriptor limit: $MAX_FD"
|
||||
fi
|
||||
else
|
||||
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
|
||||
fi
|
||||
fi
|
||||
|
||||
# For Darwin, add options to specify how the application appears in the dock
|
||||
if $darwin; then
|
||||
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
|
||||
fi
|
||||
|
||||
# For Cygwin, switch paths to Windows format before running java
|
||||
if $cygwin ; then
|
||||
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
|
||||
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
|
||||
JAVACMD=`cygpath --unix "$JAVACMD"`
|
||||
|
||||
# We build the pattern for arguments to be converted via cygpath
|
||||
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
|
||||
SEP=""
|
||||
for dir in $ROOTDIRSRAW ; do
|
||||
ROOTDIRS="$ROOTDIRS$SEP$dir"
|
||||
SEP="|"
|
||||
done
|
||||
OURCYGPATTERN="(^($ROOTDIRS))"
|
||||
# Add a user-defined pattern to the cygpath arguments
|
||||
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
|
||||
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
|
||||
fi
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
i=0
|
||||
for arg in "$@" ; do
|
||||
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
|
||||
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
|
||||
|
||||
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
|
||||
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
|
||||
else
|
||||
eval `echo args$i`="\"$arg\""
|
||||
fi
|
||||
i=$((i+1))
|
||||
done
|
||||
case $i in
|
||||
(0) set -- ;;
|
||||
(1) set -- "$args0" ;;
|
||||
(2) set -- "$args0" "$args1" ;;
|
||||
(3) set -- "$args0" "$args1" "$args2" ;;
|
||||
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
|
||||
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
|
||||
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
|
||||
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
|
||||
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
|
||||
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
|
||||
function splitJvmOpts() {
|
||||
JVM_OPTS=("$@")
|
||||
}
|
||||
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
|
||||
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
|
||||
|
||||
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
|
||||
90
core.networking/gradlew.bat
vendored
Normal file
90
core.networking/gradlew.bat
vendored
Normal file
@@ -0,0 +1,90 @@
|
||||
@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=
|
||||
|
||||
@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
|
||||
if "%@eval[2+2]" == "4" goto 4NT_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=%*
|
||||
goto execute
|
||||
|
||||
:4NT_args
|
||||
@rem Get arguments from the 4NT Shell from JP Software
|
||||
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
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.exception;
|
||||
|
||||
public class LoaderException extends Exception {
|
||||
protected static final long serialVersionUID = 1L;
|
||||
|
||||
public LoaderException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
162
core.networking/src/main/java/base/loader/AbstractLoader.java
Normal file
162
core.networking/src/main/java/base/loader/AbstractLoader.java
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.loader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.picocontainer.DefaultPicoContainer;
|
||||
import org.picocontainer.MutablePicoContainer;
|
||||
import org.picocontainer.Parameter;
|
||||
import org.picocontainer.parameters.ConstantParameter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Forwarder;
|
||||
import base.Sender;
|
||||
import base.exception.LoaderException;
|
||||
import base.server.datagram.UdpSender;
|
||||
import base.server.forwarder.UdpDuplexClientForwarder;
|
||||
import base.server.forwarder.UdpDuplexServerForwarder;
|
||||
|
||||
public class AbstractLoader<T> {
|
||||
protected static final String PROPERTIES_FILE = "loader.properties";
|
||||
protected static final Properties SERVER = null;
|
||||
|
||||
protected Logger logger = LoggerFactory.getLogger(AbstractLoader.class);
|
||||
protected MutablePicoContainer pico;
|
||||
|
||||
public AbstractLoader() {
|
||||
/* Initialise container */
|
||||
pico = new DefaultPicoContainer();
|
||||
}
|
||||
|
||||
public AbstractLoader(Properties properties) {
|
||||
this();
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public static <T> T getLoader() throws LoaderException {
|
||||
return (T) new AbstractLoader(readProperties(PROPERTIES_FILE));
|
||||
}
|
||||
|
||||
public static Properties readProperties(String propertiesFile) throws LoaderException {
|
||||
/* Read properties file */
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(AbstractLoader.class.getClassLoader().getResourceAsStream(propertiesFile));
|
||||
} catch (IOException e) {
|
||||
throw new LoaderException("Faield to read properties file: " + PROPERTIES_FILE);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
protected Class<?> getSenderClass(String protocol, String implementation) throws LoaderException {
|
||||
switch (protocol) {
|
||||
case "tcp":
|
||||
switch (implementation) {
|
||||
case "channel":
|
||||
return base.server.channel.TcpClient.class;
|
||||
default:
|
||||
case "socket":
|
||||
return base.server.socket.TcpClient.class;
|
||||
}
|
||||
case "udp":
|
||||
return UdpSender.class;
|
||||
}
|
||||
throw new LoaderException("Failed to determine <Sender>");
|
||||
}
|
||||
|
||||
protected Class<?> getClientForwarderClass(String protocol, String implementation) throws LoaderException {
|
||||
switch (protocol) {
|
||||
case "tcp":
|
||||
switch (implementation) {
|
||||
case "channel":
|
||||
return base.server.forwarder.TcpClientChannelForwarder.class;
|
||||
default:
|
||||
case "socket":
|
||||
return base.server.forwarder.TcpClientSocketForwarder.class;
|
||||
}
|
||||
case "udp":
|
||||
return UdpDuplexClientForwarder.class;
|
||||
}
|
||||
throw new LoaderException("Failed to determine <Forwarder>");
|
||||
}
|
||||
|
||||
protected Class<?> getServerForwarderClass(String protocol, String implementation) throws LoaderException {
|
||||
switch (protocol) {
|
||||
case "tcp":
|
||||
switch (implementation) {
|
||||
case "channel":
|
||||
return base.server.forwarder.TcpChannelServerForwarder.class;
|
||||
default:
|
||||
case "socket":
|
||||
return base.server.forwarder.TcpSocketServerForwarder.class;
|
||||
}
|
||||
case "udp":
|
||||
return UdpDuplexServerForwarder.class;
|
||||
}
|
||||
throw new LoaderException("Failed to determine <Forwarder>");
|
||||
}
|
||||
|
||||
protected void addClientSender(String protocol, String implementation, String host, int port) throws LoaderException {
|
||||
Class<?> senderClass = getSenderClass(protocol, implementation);
|
||||
logger.debug("Adding " + senderClass);
|
||||
pico.addComponent(Sender.class, senderClass, new Parameter[]{
|
||||
new ConstantParameter(host),
|
||||
new ConstantParameter(port)});
|
||||
}
|
||||
|
||||
protected void addServerSender(String protocol, String implementation, int port) throws LoaderException {
|
||||
Class<?> senderClass = getSenderClass(protocol, implementation);
|
||||
logger.debug("Adding " + senderClass);
|
||||
pico.addComponent(Sender.class, senderClass, new Parameter[]{
|
||||
new ConstantParameter(port)});
|
||||
}
|
||||
|
||||
protected void addClientForwarder(String protocol, String implementation, String host, int port) throws LoaderException {
|
||||
Class<?> forwarderClass = getClientForwarderClass(protocol, implementation);
|
||||
logger.debug("Adding " + forwarderClass);
|
||||
pico.addComponent(Forwarder.class, forwarderClass, new Parameter[]{
|
||||
new ConstantParameter(host),
|
||||
new ConstantParameter(port)});
|
||||
}
|
||||
|
||||
protected void addClientDuplex(String protocol, String implementation, String host, int port) throws LoaderException {
|
||||
Class<?> duplexClass = getClientForwarderClass(protocol, implementation);
|
||||
logger.debug("Adding " + duplexClass);
|
||||
pico.addComponent(Duplex.class, duplexClass, new Parameter[]{
|
||||
new ConstantParameter(host),
|
||||
new ConstantParameter(port)});
|
||||
|
||||
}
|
||||
|
||||
protected void addServerForwarder(String protocol, String implementation, int port) throws LoaderException {
|
||||
Class<?> forwarderClass = getServerForwarderClass(protocol, implementation);
|
||||
logger.debug("Adding " + forwarderClass);
|
||||
pico.addComponent(Forwarder.class, forwarderClass, new Parameter[]{
|
||||
new ConstantParameter(port)});
|
||||
}
|
||||
|
||||
protected void addServerDuplex(String protocol, String implementation, int port) throws LoaderException {
|
||||
Class<?> duplexClass = getServerForwarderClass(protocol, implementation);
|
||||
logger.debug("Adding " + duplexClass);
|
||||
pico.addComponent(Duplex.class, duplexClass, new Parameter[]{
|
||||
new ConstantParameter(port)});
|
||||
}
|
||||
}
|
||||
153
core.networking/src/main/java/base/server/channel/TcpClient.java
Normal file
153
core.networking/src/main/java/base/server/channel/TcpClient.java
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Listen;
|
||||
import base.work.Work;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class TcpClient extends Work implements Sender {
|
||||
protected static final String HOST = "localhost";
|
||||
protected static final int BUFFER_SIZE = 1024;
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected SocketChannel socketChannel;
|
||||
protected Selector selector;
|
||||
protected ArrayList<Listen<byte[]>> listenList = new ArrayList<Listen<byte[]>>();
|
||||
|
||||
public TcpClient(int port) {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public TcpClient(String host, int port) {
|
||||
this(host, port, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public TcpClient(String host, int port, int bufferSize) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
System.out.println("Client: Activate!");
|
||||
try {
|
||||
InetSocketAddress hostAddress = new InetSocketAddress(host, port);
|
||||
socketChannel = SocketChannel.open(hostAddress);
|
||||
socketChannel.configureBlocking(false);
|
||||
while (!socketChannel.finishConnect()) {
|
||||
sleep(Worker.SLEEP);
|
||||
}
|
||||
selector = Selector.open();
|
||||
socketChannel.register(selector, SelectionKey.OP_READ);
|
||||
synchronized (host) {
|
||||
host.notifyAll();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
System.out.println("Client: Deactivate!");
|
||||
try {
|
||||
selector.close();
|
||||
socketChannel.close();
|
||||
} catch (IOException e) {
|
||||
throw new DeactivateException();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
super.stop();
|
||||
if (selector != null) {
|
||||
selector.wakeup();
|
||||
}
|
||||
}
|
||||
|
||||
public final void work() {
|
||||
try {
|
||||
logger.debug("Client: Waiting for select... ");
|
||||
logger.debug("Client: Number of selected keys: " + selector.select());
|
||||
Set<SelectionKey> selectionKeySet = selector.selectedKeys();
|
||||
Iterator<SelectionKey> selectionKeyIterator = selectionKeySet.iterator();
|
||||
|
||||
while (selectionKeyIterator.hasNext()) {
|
||||
SelectionKey selectionKey = selectionKeyIterator.next();
|
||||
if (selectionKey.isReadable()) {
|
||||
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
|
||||
socketChannel.read(byteBuffer);
|
||||
byte[] buffer = byteBuffer.array();
|
||||
input(buffer);
|
||||
} else if (selectionKey.isWritable()) {
|
||||
byte[] buffer;
|
||||
buffer = (byte[]) selectionKey.attachment();
|
||||
ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
|
||||
socketChannel.write(byteBuffer);
|
||||
//selectionKey.cancel();
|
||||
socketChannel.register(selector, SelectionKey.OP_READ);
|
||||
}
|
||||
selectionKeyIterator.remove();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
if (selector == null) {
|
||||
try {
|
||||
synchronized (host) {
|
||||
host.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
selector.wakeup();
|
||||
socketChannel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, buffer);
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
socketChannel.close();
|
||||
}
|
||||
|
||||
/*public void register(Listen<byte[]> listen) {
|
||||
listenList.add(listen);
|
||||
}
|
||||
|
||||
public void remove(Listen<byte[]> listen) {
|
||||
listenList.remove(listen);
|
||||
}*/
|
||||
}
|
||||
174
core.networking/src/main/java/base/server/channel/TcpServer.java
Normal file
174
core.networking/src/main/java/base/server/channel/TcpServer.java
Normal file
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.BindException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.server.channel.TcpServerClient;
|
||||
import base.work.Work;
|
||||
|
||||
public class TcpServer extends Work implements Sender {
|
||||
protected static final Class<?> CLIENT_CLASS = TcpServerClient.class;
|
||||
protected static final int BUFFER_SIZE = 1024;
|
||||
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected Constructor<?> clientConstructor;
|
||||
protected Selector selector;
|
||||
protected ServerSocketChannel serverSocket;
|
||||
protected ArrayList<TcpServerClient> clientList;
|
||||
|
||||
public TcpServer(int port) {
|
||||
this(port, CLIENT_CLASS);
|
||||
}
|
||||
|
||||
public TcpServer(int port, Class<?> clientClass) {
|
||||
this(port, clientClass, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public TcpServer(int port, Class<?> clientClass, int bufferSize) {
|
||||
this.port = port;
|
||||
this.bufferSize = bufferSize;
|
||||
try {
|
||||
// Allow dependency injection, constructor arguments
|
||||
clientConstructor = Class.forName(clientClass.getName()).getConstructor(TcpServer.class, SocketChannel.class, Integer.class);
|
||||
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
|
||||
logger.error("Failed to initialise client constructor", e);
|
||||
}
|
||||
clientList = new ArrayList<TcpServerClient>();
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
System.out.println("Server: Activate!");
|
||||
try {
|
||||
// Get selector
|
||||
selector = Selector.open();
|
||||
|
||||
// Get server socket channel and register with selector
|
||||
serverSocket = ServerSocketChannel.open();
|
||||
InetSocketAddress hostAddress = new InetSocketAddress(port);
|
||||
serverSocket.bind(hostAddress);
|
||||
serverSocket.configureBlocking(false);
|
||||
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
|
||||
synchronized (clientConstructor) {
|
||||
clientConstructor.notifyAll();
|
||||
}
|
||||
return;
|
||||
} catch (BindException e) {
|
||||
logger.error("Address already in use", e);
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
throw new ActivateException();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
System.out.println("Server: Deactivate!");
|
||||
try {
|
||||
selector.close();
|
||||
serverSocket.close();
|
||||
} catch (IOException e) {
|
||||
throw new DeactivateException();
|
||||
} finally {
|
||||
for (TcpServerClient client : clientList) {
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
super.stop();
|
||||
if (selector != null) {
|
||||
selector.wakeup();
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
System.out.println("Server: Waiting for select... ");
|
||||
System.out.println("Server: Number of selected keys: " + selector.select());
|
||||
|
||||
Set<SelectionKey> selectionKeySet = selector.selectedKeys();
|
||||
Iterator<SelectionKey> selectionKeyIterator = selectionKeySet.iterator();
|
||||
|
||||
while (selectionKeyIterator.hasNext()) {
|
||||
SelectionKey selectionKey = selectionKeyIterator.next();
|
||||
if (selectionKey.isAcceptable()) {
|
||||
// Accept the new client connection
|
||||
SocketChannel socketChannel = serverSocket.accept();
|
||||
socketChannel.configureBlocking(false);
|
||||
|
||||
// Add the new connection to the selector
|
||||
TcpServerClient client = (TcpServerClient) clientConstructor.newInstance(this, socketChannel, bufferSize);
|
||||
clientList.add(client);
|
||||
socketChannel.register(selector, SelectionKey.OP_READ, client);
|
||||
//initClient(client);
|
||||
System.out.println("Accepted new connection from client: " + socketChannel);
|
||||
} else if (selectionKey.isReadable()) {
|
||||
// Read the data from client
|
||||
TcpServerClient serverClient = (TcpServerClient) selectionKey.attachment();
|
||||
serverClient.readable();
|
||||
} else if (selectionKey.isWritable()) {
|
||||
// Write to client?
|
||||
}
|
||||
selectionKeyIterator.remove();
|
||||
}
|
||||
}/* catch (IOException e) {} catch (InstantiationException e) {
|
||||
logger.error("", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
logger.error("", e);
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.error("", e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logger.error("", e);
|
||||
} */catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected void initClient(TcpServerClient client) {
|
||||
try {
|
||||
client.write(ByteBuffer.wrap(new String("Hi there!").getBytes()));
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
logger.debug("Number of clients = " + clientList.size());
|
||||
for (TcpServerClient client : clientList) {
|
||||
// Should be dealt with in clients own thread
|
||||
client.send(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.channel;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
import base.Sender;
|
||||
import base.work.Listen;
|
||||
|
||||
public class TcpServerClient extends Listen<byte[]> implements Sender {
|
||||
protected static final int BUFFER_SIZE = 1024;
|
||||
|
||||
protected TcpServer server;
|
||||
protected SocketChannel socketChannel;
|
||||
protected int bufferSize;
|
||||
protected ByteBuffer byteBuffer;
|
||||
|
||||
public TcpServerClient(TcpServer server, SocketChannel socketChannel) {
|
||||
this(server, socketChannel, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public TcpServerClient(TcpServer server, SocketChannel socketChannel, Integer bufferSize) {
|
||||
super();
|
||||
this.server = server;
|
||||
this.socketChannel = socketChannel;
|
||||
this.bufferSize = bufferSize;
|
||||
byteBuffer = ByteBuffer.allocate(bufferSize);
|
||||
}
|
||||
|
||||
public void write(ByteBuffer byteBuffer) throws IOException {
|
||||
socketChannel.write(byteBuffer);
|
||||
}
|
||||
|
||||
public void readable() throws IOException {
|
||||
int read;
|
||||
while (( read = socketChannel.read(byteBuffer)) > 0) {
|
||||
byteBuffer.flip();
|
||||
byte[] buffer = byteBuffer.array();
|
||||
input(buffer);
|
||||
byteBuffer.clear();
|
||||
}
|
||||
if (read < 0) {
|
||||
socketChannel.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
server.input(this, buffer);
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
write(ByteBuffer.wrap(buffer));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class AbstractUdpClient extends Work {
|
||||
protected static final int BUFFER_SIZE = 2048;
|
||||
|
||||
protected int bufferSize;
|
||||
protected MulticastSocket socket;
|
||||
protected DatagramPacket datagramPacket;
|
||||
|
||||
public AbstractUdpClient() {}
|
||||
|
||||
public AbstractUdpClient(MulticastSocket socket) {
|
||||
this(socket, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public AbstractUdpClient(MulticastSocket socket, int bufferSize) {
|
||||
this.socket = socket;
|
||||
this.bufferSize = bufferSize;
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
datagramPacket = new DatagramPacket(buffer, buffer.length);
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
System.out.println("iets ontvangen!!!!!");
|
||||
buffer = packet.getData();
|
||||
input(buffer);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
socket.close();
|
||||
super.stop();
|
||||
}
|
||||
|
||||
protected abstract void input(byte[] buffer);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
public class UdpDuplexAutoClient extends UdpDuplexClient {
|
||||
public UdpDuplexAutoClient(int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(HOST, bindPort, null, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexAutoClient(String bindHost, int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(bindHost, bindPort, null, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexAutoClient(String bindHost, int bindPort, int sendPort, int bufferSize) throws UnknownHostException {
|
||||
super(bindHost, bindPort, null, sendPort, bufferSize);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import base.Sender;
|
||||
|
||||
public class UdpDuplexClient extends UdpMulticastClient implements Sender {
|
||||
protected int sendPort;
|
||||
protected Sender sender;
|
||||
|
||||
public UdpDuplexClient(int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
this(HOST, bindPort, sendHost, sendPort);
|
||||
}
|
||||
|
||||
public UdpDuplexClient(String bindHost, int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
this(bindHost, bindPort, sendHost, sendPort, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public UdpDuplexClient(String bindHost, int bindPort, String sendHost, int sendPort, int bufferSize) throws UnknownHostException {
|
||||
super(bindHost, bindPort, bufferSize);
|
||||
this.sendPort = sendPort;
|
||||
if (sendHost != null) {
|
||||
sender = new UdpSender(sendHost, sendPort);
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
socket.receive(packet);
|
||||
buffer = packet.getData();
|
||||
System.out.println("Receive from " + packet.getAddress().getHostAddress());
|
||||
if (sender == null) {
|
||||
String sendHost = packet.getAddress().getHostAddress();
|
||||
sender = new UdpSender(sendHost, sendPort);
|
||||
}
|
||||
input(buffer);
|
||||
} catch (IOException e) {}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
if (sender != null) {
|
||||
sender.send(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.work.Listen;
|
||||
|
||||
public class UdpDuplexHelper extends AbstractUdpClient {
|
||||
protected Listen<byte[]> listen;
|
||||
|
||||
public UdpDuplexHelper(Listen<byte[]> listen, MulticastSocket socket) {
|
||||
super(socket);
|
||||
this.listen = listen;
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
System.out.println("jajajaja");
|
||||
listen.add(buffer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
|
||||
public class UdpDuplexServer extends UdpMulticastServer {
|
||||
protected int bindPort;
|
||||
protected UdpDuplexHelper helper;
|
||||
|
||||
public UdpDuplexServer(int sendPort, int bindPort) {
|
||||
super(sendPort);
|
||||
this.bindPort = bindPort;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket(bindPort);
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
helper = new UdpDuplexHelper(this, socket);
|
||||
helper.start();
|
||||
} catch (IOException e) {
|
||||
throw new ActivateException();
|
||||
}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
helper.stop();
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
if (socket == null) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
System.out.println("Send to " + host + " " + port);
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
|
||||
socket.send(packet);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
|
||||
public class UdpMulticastClient extends AbstractUdpClient {
|
||||
protected static final String HOST = "239.255.255.255";
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
|
||||
public UdpMulticastClient(int port) {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public UdpMulticastClient(String host, int port) {
|
||||
this(host, port, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public UdpMulticastClient(String host, int port, int bufferSize) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.bufferSize = BUFFER_SIZE;
|
||||
System.out.println("Client bind: " + host + " " + port);
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket(port);
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
socket.joinGroup(group);
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.MulticastSocket;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Listen;
|
||||
import base.worker.Worker;
|
||||
|
||||
public class UdpMulticastServer extends Listen<byte[]> implements Sender {
|
||||
protected static final String HOST = "239.255.255.255";
|
||||
protected static final int BUFFER_SIZE = 2048;
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
protected MulticastSocket socket;
|
||||
|
||||
public UdpMulticastServer(int port) {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public UdpMulticastServer(String host, int port) {
|
||||
super(Worker.Type.BACKGROUND);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
System.out.println("Server send: " + host + " " + port);
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new MulticastSocket();
|
||||
} catch (IOException e) {
|
||||
throw new ActivateException();
|
||||
}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
socket.close();
|
||||
super.deactivate();
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
try {
|
||||
InetAddress group = InetAddress.getByName(host);
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, group, port);
|
||||
socket.send(packet);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
add(buffer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.Sender;
|
||||
|
||||
public class UdpSender implements Sender {
|
||||
protected static final String HOST = "localhost";
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected DatagramSocket datagramSocket;
|
||||
protected InetAddress inetAddress;
|
||||
protected int port;
|
||||
|
||||
public UdpSender(int port) throws UnknownHostException {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public UdpSender(String host, int port) throws UnknownHostException {
|
||||
System.out.println("Sender use: " + host + " " + port);
|
||||
inetAddress = InetAddress.getByName(host);
|
||||
this.port = port;
|
||||
try {
|
||||
datagramSocket = new DatagramSocket();
|
||||
} catch (SocketException e) {
|
||||
logger.error("Failed to create socket", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {}
|
||||
|
||||
public void stop() {
|
||||
datagramSocket.close();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
stop();
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) {
|
||||
try {
|
||||
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length, inetAddress, port);
|
||||
datagramSocket.send(datagramPacket);
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to send buffer", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.datagram;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.SocketException;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.work.Work;
|
||||
|
||||
public class UdpServer extends Work {
|
||||
protected static final int BUFFER_SIZE = 1024;
|
||||
protected static final int TIMEOUT = 1000;
|
||||
|
||||
protected int port;
|
||||
protected int bufferSize;
|
||||
protected DatagramSocket diagramSocket;
|
||||
|
||||
public UdpServer(int port) {
|
||||
this(port, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public UdpServer(int port, int bufferSize) {
|
||||
super();
|
||||
this.port = port;
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
logger.debug("Starting datagram socket on port " + port);
|
||||
diagramSocket = new DatagramSocket(port);
|
||||
diagramSocket.setSoTimeout(TIMEOUT);
|
||||
super.activate();
|
||||
} catch (SocketException e) {
|
||||
logger.error("Failed to initialize socket", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
super.stop();
|
||||
if (diagramSocket != null) {
|
||||
diagramSocket.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length);
|
||||
try {
|
||||
diagramSocket.receive(datagramPacket);
|
||||
} catch (SocketException e) {
|
||||
stop();
|
||||
} catch (IOException e) {
|
||||
logger.error("Failed to receive packet", e);
|
||||
stop();
|
||||
return;
|
||||
}
|
||||
input(buffer);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.channel.TcpServer;
|
||||
import base.server.channel.TcpServerClient;
|
||||
|
||||
public class TcpChannelServerForwarder extends TcpServer implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public TcpChannelServerForwarder(int port) {
|
||||
super(port);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.socket.TcpClient;
|
||||
import base.server.socket.TcpServerClient;
|
||||
|
||||
public class TcpClientChannelForwarder extends TcpClient implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public TcpClientChannelForwarder(String host, int port) {
|
||||
super(host, port);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.channel.TcpClient;
|
||||
import base.server.channel.TcpServerClient;
|
||||
|
||||
public class TcpClientSocketForwarder extends TcpClient implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public TcpClientSocketForwarder(String host, int port) {
|
||||
super(host, port);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.socket.TcpServer;
|
||||
import base.server.socket.TcpServerClient;
|
||||
|
||||
public class TcpSocketServerForwarder extends TcpServer implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public TcpSocketServerForwarder(int port) {
|
||||
super(port);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.datagram.UdpDuplexClient;
|
||||
|
||||
public class UdpDuplexClientForwarder extends UdpDuplexClient implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public UdpDuplexClientForwarder(String bindHost, int bindPort, String sendHost, int sendPort) throws UnknownHostException {
|
||||
super(bindHost, bindPort, sendHost, sendPort);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Duplex;
|
||||
import base.Receiver;
|
||||
import base.server.datagram.UdpDuplexServer;
|
||||
|
||||
public class UdpDuplexServerForwarder extends UdpDuplexServer implements Duplex {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public UdpDuplexServerForwarder(int port, int listenPort) {
|
||||
super(port, listenPort);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.forwarder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Forwarder;
|
||||
import base.Receiver;
|
||||
import base.server.datagram.UdpServer;
|
||||
|
||||
public class UdpServerForwarder extends UdpServer implements Forwarder {
|
||||
protected ArrayList<Receiver> receiverList;
|
||||
|
||||
public UdpServerForwarder(int port) {
|
||||
super(port);
|
||||
receiverList = new ArrayList<Receiver>();
|
||||
}
|
||||
|
||||
public void register(Receiver receiver) {
|
||||
receiverList.add(receiver);
|
||||
}
|
||||
|
||||
public void remove(Receiver receiver) {
|
||||
receiverList.remove(receiver);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
for (Receiver receiver: receiverList) {
|
||||
receiver.receive(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.receiver;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import base.Control;
|
||||
import base.Forwarder;
|
||||
import base.Receiver;
|
||||
|
||||
public abstract class AbstractReceiver implements Receiver, Control {
|
||||
protected Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected Forwarder forwarder;
|
||||
|
||||
public AbstractReceiver(Forwarder forwarder) {
|
||||
this.forwarder = forwarder;
|
||||
forwarder.register(this);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
forwarder.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
forwarder.stop();
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
forwarder.exit();
|
||||
}
|
||||
|
||||
abstract public void receive(byte[] buffer);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Work;
|
||||
|
||||
public abstract class AbstractTcpClient extends Work implements Sender {
|
||||
protected static final int BUFFER_SIZE = 1024;
|
||||
|
||||
protected Object object = new Object();
|
||||
protected int bufferSize;
|
||||
protected Socket socket;
|
||||
protected InputStream inputStream;
|
||||
protected OutputStream outputStream;
|
||||
|
||||
public AbstractTcpClient(Integer bufferSize) {
|
||||
this.bufferSize = bufferSize;
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
return super.active() && socket.isConnected();
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
super.deactivate();
|
||||
try {
|
||||
inputStream.close();
|
||||
outputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
super.exit();
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
try {
|
||||
while (inputStream.read(buffer) > 0) {
|
||||
input(buffer);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void input(byte[] buffer);
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
if (outputStream == null) {
|
||||
try {
|
||||
synchronized (object) {
|
||||
object.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {}
|
||||
}
|
||||
outputStream.write(buffer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.ActivateException;
|
||||
|
||||
public class TcpClient extends AbstractTcpClient implements Sender {
|
||||
protected static final String HOST = "localhost";
|
||||
|
||||
protected String host;
|
||||
protected int port;
|
||||
|
||||
public TcpClient(int port) {
|
||||
this(HOST, port);
|
||||
}
|
||||
|
||||
public TcpClient(String host, int port) {
|
||||
this(host, port, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public TcpClient(String host, int port, int bufferSize) {
|
||||
super(bufferSize);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
socket = new Socket(host, port);
|
||||
inputStream = socket.getInputStream();
|
||||
outputStream = socket.getOutputStream();
|
||||
synchronized (object) {
|
||||
object.notifyAll();
|
||||
}
|
||||
} catch (UnknownHostException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
super.activate();
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {}
|
||||
}
|
||||
102
core.networking/src/main/java/base/server/socket/TcpServer.java
Normal file
102
core.networking/src/main/java/base/server/socket/TcpServer.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import base.Sender;
|
||||
import base.exception.worker.ActivateException;
|
||||
import base.exception.worker.DeactivateException;
|
||||
import base.work.Work;
|
||||
|
||||
public class TcpServer extends Work implements Sender {
|
||||
protected static final Class<?> CLIENT_CLASS = TcpServerClient.class;
|
||||
|
||||
protected int port;
|
||||
protected ServerSocket serverSocket;
|
||||
protected Constructor<?> clientConstructor;
|
||||
protected ArrayList<TcpServerClient> clientList;
|
||||
|
||||
public TcpServer(int port) {
|
||||
this(port, CLIENT_CLASS);
|
||||
}
|
||||
|
||||
public TcpServer(int port, Class<?> clientClass) {
|
||||
this.port = port;
|
||||
try {
|
||||
clientConstructor = Class.forName(clientClass.getName()).getConstructor(TcpServer.class, Socket.class);
|
||||
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException e) {
|
||||
logger.error("Failed to initialise client constructor");
|
||||
}
|
||||
clientList = new ArrayList<TcpServerClient>();
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
serverSocket = new ServerSocket(port);
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
}
|
||||
|
||||
public void deactivate() throws DeactivateException {
|
||||
for (TcpServerClient client : clientList) {
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
|
||||
public void exit() {
|
||||
super.exit();
|
||||
try {
|
||||
serverSocket.close();
|
||||
for (TcpServerClient client : clientList) {
|
||||
client.exit();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
Socket socket = serverSocket.accept();
|
||||
TcpServerClient client = (TcpServerClient) clientConstructor.newInstance(this, socket);
|
||||
clientList.add(client);
|
||||
client.start();
|
||||
System.out.println("Accepted new connection from client: " + socket);
|
||||
} catch (IOException e) {
|
||||
stop();
|
||||
} catch (Exception e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(byte[] buffer) throws IOException {
|
||||
logger.debug("Number of clients = " + clientList.size());
|
||||
for (TcpServerClient client : clientList) {
|
||||
// Should be dealt with in clients own thread?
|
||||
client.send(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package base.server.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Socket;
|
||||
|
||||
import base.exception.worker.ActivateException;
|
||||
|
||||
public class TcpServerClient extends AbstractTcpClient {
|
||||
private TcpServer server;
|
||||
|
||||
public TcpServerClient(TcpServer server, Socket socket) {
|
||||
this(server, socket, BUFFER_SIZE);
|
||||
}
|
||||
|
||||
public TcpServerClient(TcpServer server, Socket socket, Integer bufferSize) {
|
||||
super(bufferSize);
|
||||
this.server = server;
|
||||
this.socket = socket;
|
||||
}
|
||||
|
||||
public void activate() throws ActivateException {
|
||||
try {
|
||||
inputStream = socket.getInputStream();
|
||||
outputStream = socket.getOutputStream();
|
||||
synchronized (object) {
|
||||
object.notifyAll();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("", e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
server.input(this, buffer);
|
||||
}
|
||||
}
|
||||
36
core.networking/src/main/java/junit/AllTests.java
Normal file
36
core.networking/src/main/java/junit/AllTests.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package junit;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
import networking.TestUdpMulticastCommunication;
|
||||
import networking.TestUdpUnicastCommunication;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
TestTcpSocketCommunication.class,
|
||||
TestTcpChannelCommunication.class,
|
||||
TestUdpUnicastCommunication.class,
|
||||
TestUdpMulticastCommunication.class,
|
||||
TestUdpDuplexCommunication.class
|
||||
})
|
||||
|
||||
public class AllTests {}
|
||||
// Should test start()/stop() of components, check implementation
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.channel.TcpClient;
|
||||
import base.server.channel.TcpServer;
|
||||
import base.server.channel.TcpServerClient;
|
||||
|
||||
public class TestTcpChannelCommunication {
|
||||
protected TestTcpServer server;
|
||||
protected TestTcpClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestTcpServer(1234);
|
||||
server.start();
|
||||
client = new TestTcpClient(1234);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendClientToServer() throws Exception {
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendServerToClient() throws Exception {
|
||||
// If client can send, connection has been established
|
||||
client.send("init".getBytes());
|
||||
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestTcpServer extends TcpServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpServer(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestTcpClient extends TcpClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpClient(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.socket.TcpClient;
|
||||
import base.server.socket.TcpServer;
|
||||
import base.server.socket.TcpServerClient;
|
||||
|
||||
public class TestTcpSocketCommunication {
|
||||
protected TestTcpServer server;
|
||||
protected TestTcpClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestTcpServer(1234);
|
||||
server.start();
|
||||
client = new TestTcpClient(1234);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendClientToServer() throws Exception {
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendServerToClient() throws Exception {
|
||||
// If client can send, connection has been established
|
||||
client.send("init".getBytes());
|
||||
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestTcpServer extends TcpServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpServer(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestTcpClient extends TcpClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpClient(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Copyright (C) 2016 Rik Veenboer <rik.veenboer@gmail.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package junit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.datagram.UdpDuplexAutoClient;
|
||||
import base.server.datagram.UdpDuplexServer;
|
||||
|
||||
public class TestUdpDuplexCommunication {
|
||||
protected TestUdpDuplexServer server;
|
||||
protected TestUdpDuplexClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestUdpDuplexServer(1234, 1235);
|
||||
server.start();
|
||||
client = new TestUdpDuplexClient(1234, 1235);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServerToClientCommunication() throws Exception {
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientToServerCommunication() throws Exception {
|
||||
// Let client discover server address
|
||||
testServerToClientCommunication();
|
||||
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
public class TestUdpDuplexServer extends UdpDuplexServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexServer(int sendPort, int bindPort) {
|
||||
super(sendPort, bindPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestUdpDuplexClient extends UdpDuplexAutoClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexClient(int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(bindPort, sendPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
17
core.networking/src/test/java/networking/AllTests.java
Normal file
17
core.networking/src/test/java/networking/AllTests.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package networking;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
@RunWith(Suite.class)
|
||||
@SuiteClasses({
|
||||
TestTcpSocketCommunication.class,
|
||||
TestTcpChannelCommunication.class,
|
||||
TestUdpUnicastCommunication.class,
|
||||
TestUdpMulticastCommunication.class,
|
||||
TestUdpDuplexCommunication.class
|
||||
})
|
||||
|
||||
public class AllTests {}
|
||||
// Should test start()/stop() of components, check implementation
|
||||
@@ -0,0 +1,94 @@
|
||||
package networking;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.channel.TcpClient;
|
||||
import base.server.channel.TcpServer;
|
||||
import base.server.channel.TcpServerClient;
|
||||
|
||||
public class TestTcpChannelCommunication {
|
||||
protected TestTcpServer server;
|
||||
protected TestTcpClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestTcpServer(1234);
|
||||
server.start();
|
||||
client = new TestTcpClient(1234);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendClientToServer() throws Exception {
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendServerToClient() throws Exception {
|
||||
// If client can send, connection has been established
|
||||
client.send("init".getBytes());
|
||||
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestTcpServer extends TcpServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpServer(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestTcpClient extends TcpClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpClient(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package networking;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.socket.TcpClient;
|
||||
import base.server.socket.TcpServer;
|
||||
import base.server.socket.TcpServerClient;
|
||||
|
||||
public class TestTcpSocketCommunication {
|
||||
protected TestTcpServer server;
|
||||
protected TestTcpClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestTcpServer(1234);
|
||||
server.start();
|
||||
client = new TestTcpClient(1234);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendClientToServer() throws Exception {
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendServerToClient() throws Exception {
|
||||
// If client can send, connection has been established
|
||||
client.send("init".getBytes());
|
||||
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestTcpServer extends TcpServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpServer(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public void input(TcpServerClient client, byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestTcpClient extends TcpClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestTcpClient(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
protected void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package networking;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.datagram.UdpDuplexAutoClient;
|
||||
import base.server.datagram.UdpDuplexServer;
|
||||
|
||||
public class TestUdpDuplexCommunication {
|
||||
protected TestUdpDuplexServer server;
|
||||
protected TestUdpDuplexClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestUdpDuplexServer(1234, 1235);
|
||||
server.start();
|
||||
client = new TestUdpDuplexClient(1234, 1235);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServerToClientCommunication() throws Exception {
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClientToServerCommunication() throws Exception {
|
||||
// Let client discover server address
|
||||
testServerToClientCommunication();
|
||||
|
||||
String message = "test";
|
||||
client.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
public class TestUdpDuplexServer extends UdpDuplexServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexServer(int sendPort, int bindPort) {
|
||||
super(sendPort, bindPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class TestUdpDuplexClient extends UdpDuplexAutoClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpDuplexClient(int bindPort, int sendPort) throws UnknownHostException {
|
||||
super(bindPort, sendPort);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package networking;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.datagram.UdpMulticastClient;
|
||||
import base.server.datagram.UdpMulticastServer;
|
||||
|
||||
public class TestUdpMulticastCommunication {
|
||||
protected UdpMulticastServer server;
|
||||
protected TestUdpMulticastClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new UdpMulticastServer(1234);
|
||||
server.start();
|
||||
client = new TestUdpMulticastClient(1234);
|
||||
client.start();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.exit();
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (client.active() || server.active()) {
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testServerToClientCommunication() throws Exception {
|
||||
String message = "test";
|
||||
server.send(message.getBytes());
|
||||
System.err.println("send");
|
||||
synchronized (client) {
|
||||
client.wait(2000);
|
||||
}
|
||||
byte[] buffer = client.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestUdpMulticastClient extends UdpMulticastClient {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpMulticastClient(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
public void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package networking;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import base.server.datagram.UdpSender;
|
||||
import base.server.datagram.UdpServer;
|
||||
|
||||
public class TestUdpUnicastCommunication {
|
||||
protected TestUdpServer server;
|
||||
protected UdpSender sender;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
server = new TestUdpServer(1234);
|
||||
server.start();
|
||||
sender = new UdpSender(1234);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
server.exit();
|
||||
|
||||
// Should add blocking stop and exit to worker
|
||||
while (server.active()) {
|
||||
Thread.sleep(100);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendClientToServer() throws Exception {
|
||||
String message = "test";
|
||||
sender.send(message.getBytes());
|
||||
synchronized (server) {
|
||||
server.wait(2000);
|
||||
}
|
||||
byte[] buffer = server.buffer;
|
||||
assertNotNull("Received input", buffer);
|
||||
assertEquals("Message intact", message, new String(buffer).trim());
|
||||
}
|
||||
|
||||
class TestUdpServer extends UdpServer {
|
||||
public byte[] buffer;
|
||||
|
||||
public TestUdpServer(int port) {
|
||||
super(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void input(byte[] buffer) {
|
||||
this.buffer = buffer;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user