From a2b835262c690b8ca348a306cec297e90601e535 Mon Sep 17 00:00:00 2001 From: roman Date: Sat, 15 Oct 2016 00:35:33 +0300 Subject: [PATCH 1/2] ftp init --- build.gradle | 14 ++ gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 164 ++++++++++++++++++ gradlew.bat | 90 ++++++++++ settings.gradle | 2 + .../com/github/nizshee/client/Client.java | 41 +++++ .../com/github/nizshee/client/ClientMain.java | 27 +++ .../com/github/nizshee/server/Server.java | 71 ++++++++ .../com/github/nizshee/server/ServerMain.java | 40 +++++ .../nizshee/server/ServerMethodWrapper.java | 24 +++ .../com/github/nizshee/shared/GetMethod.java | 48 +++++ .../com/github/nizshee/shared/ListMethod.java | 57 ++++++ .../com/github/nizshee/shared/Method.java | 18 ++ .../com/github/nizshee/shared/RemoteFile.java | 28 +++ .../com/github/nizshee/ClientServerTest.java | 70 ++++++++ .../github/nizshee/shared/GetMethodTest.java | 59 +++++++ .../github/nizshee/shared/ListMethodTest.java | 64 +++++++ src/test/resources/test/a | 1 + src/test/resources/test/b | 1 + 19 files changed, 825 insertions(+) create mode 100644 build.gradle create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 src/main/java/com/github/nizshee/client/Client.java create mode 100644 src/main/java/com/github/nizshee/client/ClientMain.java create mode 100644 src/main/java/com/github/nizshee/server/Server.java create mode 100644 src/main/java/com/github/nizshee/server/ServerMain.java create mode 100644 src/main/java/com/github/nizshee/server/ServerMethodWrapper.java create mode 100644 src/main/java/com/github/nizshee/shared/GetMethod.java create mode 100644 src/main/java/com/github/nizshee/shared/ListMethod.java create mode 100644 src/main/java/com/github/nizshee/shared/Method.java create mode 100644 src/main/java/com/github/nizshee/shared/RemoteFile.java create mode 100644 src/test/java/com/github/nizshee/ClientServerTest.java create mode 100644 src/test/java/com/github/nizshee/shared/GetMethodTest.java create mode 100644 src/test/java/com/github/nizshee/shared/ListMethodTest.java create mode 100644 src/test/resources/test/a create mode 100644 src/test/resources/test/b diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..f74504a --- /dev/null +++ b/build.gradle @@ -0,0 +1,14 @@ +group 'com.githun.nizshee' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..e582b84 --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Oct 12 12:15:41 MSK 2016 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-bin.zip diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..27309d9 --- /dev/null +++ b/gradlew @@ -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 "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..f6d5974 --- /dev/null +++ b/gradlew.bat @@ -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 diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..f805b28 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'hw12' + diff --git a/src/main/java/com/github/nizshee/client/Client.java b/src/main/java/com/github/nizshee/client/Client.java new file mode 100644 index 0000000..67caeea --- /dev/null +++ b/src/main/java/com/github/nizshee/client/Client.java @@ -0,0 +1,41 @@ +package com.github.nizshee.client; + + +import com.github.nizshee.shared.GetMethod; +import com.github.nizshee.shared.ListMethod; +import com.github.nizshee.shared.Method; +import com.github.nizshee.shared.RemoteFile; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.List; + +public class Client { + + private final String host; + private final int port; + + public Client(String host, int port) throws IOException { + this.host = host; + this.port = port; + } + + List getList(String name) throws IOException { + return getValue(new Socket(host, port), 1, new ListMethod(), name); + } + + byte[] getFile(String name) throws IOException { + return getValue(new Socket(host, port), 2, new GetMethod(), name); + } + + public static Result getValue(Socket socket, int code, Method method, Value value) + throws IOException { + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + dos.writeInt(code); + method.writeValue(dos, value); + return method.readResult(dis); + } +} diff --git a/src/main/java/com/github/nizshee/client/ClientMain.java b/src/main/java/com/github/nizshee/client/ClientMain.java new file mode 100644 index 0000000..6c6c4c2 --- /dev/null +++ b/src/main/java/com/github/nizshee/client/ClientMain.java @@ -0,0 +1,27 @@ +package com.github.nizshee.client; + + +import com.github.nizshee.shared.RemoteFile; + +import java.util.List; + +public class ClientMain { + + public static void main(String[] args) throws Exception { + Client client = new Client("localhost", 8080); + + if (args.length == 2 && args[0].equals("list")) { + List list = client.getList(args[1]); + for (RemoteFile file: list) { + System.out.println(file); + } + } else if (args.length == 2 && args[0].equals("get")) { + byte[] bytes = client.getFile(args[1]); + System.out.println(bytes.length); + for (byte b: bytes) { + System.out.print(b); + } + System.out.println(); + } + } +} diff --git a/src/main/java/com/github/nizshee/server/Server.java b/src/main/java/com/github/nizshee/server/Server.java new file mode 100644 index 0000000..3fe3f9c --- /dev/null +++ b/src/main/java/com/github/nizshee/server/Server.java @@ -0,0 +1,71 @@ +package com.github.nizshee.server; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.*; +import java.util.Map; + +public class Server { + + private final InetAddress host; + private final Integer port; + private ServerSocket socket = null; + + public Server(InetAddress host, Integer port) { + this.host = host; + this.port = port; + } + + public void start(Map handlers) throws IOException { + if (socket != null) throw new IOException("Server already running."); + socket = new ServerSocket(port, 50, host); + Thread thread = new Thread(new ServerRunnable(socket, handlers)); + thread.start(); + } + + public void startSync(Map handlers) throws IOException { + if (socket != null) throw new IOException("Server already running."); + socket = new ServerSocket(port, 50, host); + (new ServerRunnable(socket, handlers)).run(); + } + + public void stop() throws IOException { + socket.close(); + socket = null; + } + + private class ServerRunnable implements Runnable { + + private final ServerSocket serverSocket; + private final Map handlers; + + ServerRunnable(ServerSocket serverSocket, Map handlers) { + this.serverSocket = serverSocket; + this.handlers = handlers; + } + + public void run() { + try { + System.out.println("Server started."); + //noinspection InfiniteLoopStatement + while (true) { + Socket socket = serverSocket.accept(); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + Integer key = dis.readInt(); + if (handlers.containsKey(key)) { + ServerMethodWrapper wrapper = handlers.get(key); + wrapper.handle(dis, dos); + } + socket.close(); + } + } catch (SocketException ignore) { + System.out.println("Server stopped."); + } catch (IOException e) { + System.err.println("Can't accept connection."); + } + } + } +} diff --git a/src/main/java/com/github/nizshee/server/ServerMain.java b/src/main/java/com/github/nizshee/server/ServerMain.java new file mode 100644 index 0000000..d1b99d9 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/ServerMain.java @@ -0,0 +1,40 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.GetMethod; +import com.github.nizshee.shared.ListMethod; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.InetAddress; +import java.net.Socket; +import java.util.HashMap; +import java.util.Map; + +@SuppressWarnings("all") +public class ServerMain { + + private final static Map handlers; + + static { + handlers = new HashMap<>(); + handlers.put(1, new ServerMethodWrapper(new ListMethod())); + handlers.put(2, new ServerMethodWrapper(new GetMethod())); + } + + public static void main(String[] args) throws Exception { + Server server = new Server(InetAddress.getByName("localhost"), 8080); + server.startSync(handlers); +// server.start(handlers); +// Thread.sleep(500); +// Socket socket = new Socket("localhost", 8080); +// DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); +// DataInputStream dis = new DataInputStream(socket.getInputStream()); +// dos.writeInt(1); +// dos.writeUTF("/home/roman"); +// Thread.sleep(500); +// int result = dis.readInt(); +// System.out.println(result + 1); +// server.stop(); + } +} diff --git a/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java b/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java new file mode 100644 index 0000000..483f186 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java @@ -0,0 +1,24 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.Method; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class ServerMethodWrapper { + + private final Method method; + + public ServerMethodWrapper(Method method) { + this.method = method; + } + + public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { + Value value = method.readValue(dis); + Result result = method.apply(value); + method.writeResult(dos, result); + } + +} diff --git a/src/main/java/com/github/nizshee/shared/GetMethod.java b/src/main/java/com/github/nizshee/shared/GetMethod.java new file mode 100644 index 0000000..87813b1 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/GetMethod.java @@ -0,0 +1,48 @@ +package com.github.nizshee.shared; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class GetMethod implements Method { + + @Override + public byte[] apply(String s) { + try { + return Files.readAllBytes(Paths.get(s)); + } catch (IOException ignore) { + return new byte[0]; + } + } + + @Override + public void writeValue(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readValue(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + + @Override + public void writeResult(DataOutputStream dos, byte[] bytes) throws IOException { + dos.writeInt(bytes.length); + dos.write(bytes, 0, bytes.length); + for (Byte aByte : bytes) { + dos.writeByte(aByte); + } + } + + @Override + public byte[] readResult(DataInputStream dis) throws IOException { + int length = dis.readInt(); + byte[] bytes = new byte[length]; + int read = dis.read(bytes, 0, length); + if (read != length) throw new IOException("Bad read,"); + return bytes; + } +} diff --git a/src/main/java/com/github/nizshee/shared/ListMethod.java b/src/main/java/com/github/nizshee/shared/ListMethod.java new file mode 100644 index 0000000..6348e33 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ListMethod.java @@ -0,0 +1,57 @@ +package com.github.nizshee.shared; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +public class ListMethod implements Method> { + + @Override + public List apply(String name) { + Path dir = Paths.get(name); + File[] files = dir.toFile().listFiles(); + if (files == null) return Collections.emptyList(); + List list = new LinkedList<>(); + for (File file: files) { + list.add(new RemoteFile(file.getName(), file.isDirectory())); + } + return list; + } + + @Override + public void writeValue(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readValue(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + + @Override + public void writeResult(DataOutputStream dos, List remoteFiles) throws IOException { + dos.writeInt(remoteFiles.size()); + for (RemoteFile file: remoteFiles) { + dos.writeUTF(file.name); + dos.writeBoolean(file.isDirectory); + } + } + + @Override + public List readResult(DataInputStream dis) throws IOException { + int size = dis.readInt(); + List list = new LinkedList<>(); + for (int i = 0; i < size; ++i) { + String name = dis.readUTF(); + boolean isDirectory = dis.readBoolean(); + list.add(new RemoteFile(name, isDirectory)); + } + return list; + } +} diff --git a/src/main/java/com/github/nizshee/shared/Method.java b/src/main/java/com/github/nizshee/shared/Method.java new file mode 100644 index 0000000..cadfc68 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/Method.java @@ -0,0 +1,18 @@ +package com.github.nizshee.shared; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface Method { + Result apply(Value value); + + void writeValue(DataOutputStream dos, Value value) throws IOException; + + Value readValue(DataInputStream dis) throws IOException; + + void writeResult(DataOutputStream dos, Result result) throws IOException; + + Result readResult(DataInputStream dis) throws IOException; +} diff --git a/src/main/java/com/github/nizshee/shared/RemoteFile.java b/src/main/java/com/github/nizshee/shared/RemoteFile.java new file mode 100644 index 0000000..52fa310 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/RemoteFile.java @@ -0,0 +1,28 @@ +package com.github.nizshee.shared; + + +public class RemoteFile { + + public final boolean isDirectory; + public final String name; + + public RemoteFile(String name, boolean isDirectory) { + this.isDirectory = isDirectory; + this.name = name; + } + + @Override + public int hashCode() { + return Boolean.hashCode(isDirectory) + name.hashCode() % 34; + } + + @Override + public boolean equals(Object o) { + return o instanceof RemoteFile && ((RemoteFile) o).name.equals(name) && ((RemoteFile) o).isDirectory == isDirectory; + } + + @Override + public String toString() { + return name; + } +} diff --git a/src/test/java/com/github/nizshee/ClientServerTest.java b/src/test/java/com/github/nizshee/ClientServerTest.java new file mode 100644 index 0000000..6faecbc --- /dev/null +++ b/src/test/java/com/github/nizshee/ClientServerTest.java @@ -0,0 +1,70 @@ +package com.github.nizshee; + +import com.github.nizshee.client.Client; +import com.github.nizshee.server.Server; +import com.github.nizshee.server.ServerMethodWrapper; +import com.github.nizshee.shared.Method; +import org.junit.Test; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.InetAddress; +import java.net.Socket; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.junit.Assert.*; + +@SuppressWarnings("all") +public class ClientServerTest { + + private final static int code = 0; + private final static AtomicBoolean trigger = new AtomicBoolean(false); + public static final Method method = new Method() { + @Override + public String apply(String s) { + trigger.set(true); + return s + s; + } + + @Override + public void writeValue(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readValue(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + + @Override + public void writeResult(DataOutputStream dos, String s) throws IOException { + dos.writeUTF(s); + } + + @Override + public String readResult(DataInputStream dis) throws IOException { + return dis.readUTF(); + } + }; + private static final Map handlers; + static { + handlers = new HashMap<>(); + handlers.put(code, new ServerMethodWrapper(method)); + } + + @Test + public void clientServerTest() throws Exception { + Server server = new Server(InetAddress.getByName("localhost"), 8080); + server.start(handlers); + Thread.sleep(500); + Client client = new Client("localhost", 8080); + Socket socket = new Socket("localhost", 8080); + String result = Client.getValue(socket, code, method, "abcd"); + assertEquals("abcdabcd", result); + socket.close(); + server.stop(); + } +} diff --git a/src/test/java/com/github/nizshee/shared/GetMethodTest.java b/src/test/java/com/github/nizshee/shared/GetMethodTest.java new file mode 100644 index 0000000..7bafc1f --- /dev/null +++ b/src/test/java/com/github/nizshee/shared/GetMethodTest.java @@ -0,0 +1,59 @@ +package com.github.nizshee.shared; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; + +import static org.junit.Assert.*; + + +public class GetMethodTest { + + private final static GetMethod method = new GetMethod(); + + @SuppressWarnings("ConstantConditions") + @Test + public void applyTest() throws Exception { + byte[] bytes1 = method.apply(getClass().getClassLoader().getResource("test/a").getPath()); + byte[] bytes2 = method.apply(getClass().getClassLoader().getResource("test/b").getPath()); + + assertArrayEquals("123".getBytes(), bytes1); + assertArrayEquals("12345".getBytes(), bytes2); + } + + @Test + public void valueTest() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeValue(dos, "anotherTestString1"); + method.writeValue(dos, "anotherTestString2"); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertEquals("anotherTestString1", method.readValue(dis)); + assertEquals("anotherTestString2", method.readValue(dis)); + } + + @Test + public void resultTest() throws Exception { + byte[] bytes1 = {}; + byte[] bytes2 = {1, 2, 3, 4}; + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeResult(dos, bytes1); + method.writeResult(dos, bytes2); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertArrayEquals(bytes1, method.readResult(dis)); + assertArrayEquals(bytes2, method.readResult(dis)); + } +} diff --git a/src/test/java/com/github/nizshee/shared/ListMethodTest.java b/src/test/java/com/github/nizshee/shared/ListMethodTest.java new file mode 100644 index 0000000..dd96f0f --- /dev/null +++ b/src/test/java/com/github/nizshee/shared/ListMethodTest.java @@ -0,0 +1,64 @@ +package com.github.nizshee.shared; + +import org.junit.Test; + +import java.io.*; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import static org.junit.Assert.*; + +public class ListMethodTest { + + private final static ListMethod method = new ListMethod(); + + @Test + public void applyTest() throws Exception { + + @SuppressWarnings("ConstantConditions") + List result = method.apply(getClass().getClassLoader().getResource("test/").getPath()); + assertEquals(Arrays.asList( + new RemoteFile("a", false), + new RemoteFile("b", false), + new RemoteFile("c", true) + ), result); + + } + + @Test + public void valueTest() throws Exception { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeValue(dos, "testString1"); + method.writeValue(dos, "testString2"); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertEquals("testString1", method.readValue(dis)); + assertEquals("testString2", method.readValue(dis)); + } + + @Test + public void resultTest() throws Exception { + List list1 = new LinkedList<>(); + List list2 = Arrays.asList( + new RemoteFile("1", true), + new RemoteFile("2", false), + new RemoteFile("3", true) + ); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream dos = new DataOutputStream(bos); + + method.writeResult(dos, list1); + method.writeResult(dos, list2); + + ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); + DataInputStream dis = new DataInputStream(bis); + + assertEquals(list1, method.readResult(dis)); + assertEquals(list2, method.readResult(dis)); + } +} diff --git a/src/test/resources/test/a b/src/test/resources/test/a new file mode 100644 index 0000000..d800886 --- /dev/null +++ b/src/test/resources/test/a @@ -0,0 +1 @@ +123 \ No newline at end of file diff --git a/src/test/resources/test/b b/src/test/resources/test/b new file mode 100644 index 0000000..bd41cba --- /dev/null +++ b/src/test/resources/test/b @@ -0,0 +1 @@ +12345 \ No newline at end of file From f28dc7eb6bc200a0e137e4c28b78eef7a4737cb1 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 15 Nov 2016 16:22:39 +0300 Subject: [PATCH 2/2] fixed get --- .gitignore | 3 + .../com/github/nizshee/client/Client.java | 41 +++++++------ .../com/github/nizshee/client/ClientMain.java | 17 +++--- .../nizshee/server/CopyFileHandler.java | 27 +++++++++ .../com/github/nizshee/server/Handler.java | 12 ++++ .../github/nizshee/server/MethodWrapper.java | 23 ++++++++ .../com/github/nizshee/server/Server.java | 49 +++++++++------ .../com/github/nizshee/server/ServerMain.java | 25 ++------ .../nizshee/server/ServerMethodWrapper.java | 24 -------- .../com/github/nizshee/shared/GetMethod.java | 48 --------------- .../com/github/nizshee/shared/ListMethod.java | 9 ++- .../com/github/nizshee/shared/Method.java | 24 ++++++-- .../com/github/nizshee/shared/RemoteFile.java | 1 + .../shared/ServerNotRunningException.java | 8 +++ .../shared/ServerRunningException.java | 8 +++ .../com/github/nizshee/ClientServerTest.java | 17 +++--- .../github/nizshee/shared/GetMethodTest.java | 59 ------------------- .../github/nizshee/shared/ListMethodTest.java | 14 ++--- src/test/resources/test/c/c | 0 19 files changed, 189 insertions(+), 220 deletions(-) create mode 100644 src/main/java/com/github/nizshee/server/CopyFileHandler.java create mode 100644 src/main/java/com/github/nizshee/server/Handler.java create mode 100644 src/main/java/com/github/nizshee/server/MethodWrapper.java delete mode 100644 src/main/java/com/github/nizshee/server/ServerMethodWrapper.java delete mode 100644 src/main/java/com/github/nizshee/shared/GetMethod.java create mode 100644 src/main/java/com/github/nizshee/shared/ServerNotRunningException.java create mode 100644 src/main/java/com/github/nizshee/shared/ServerRunningException.java delete mode 100644 src/test/java/com/github/nizshee/shared/GetMethodTest.java create mode 100644 src/test/resources/test/c/c diff --git a/.gitignore b/.gitignore index 84170b2..7636016 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +testDir +testFile + *.class # Mobile Tools for Java (J2ME) diff --git a/src/main/java/com/github/nizshee/client/Client.java b/src/main/java/com/github/nizshee/client/Client.java index 67caeea..790d40e 100644 --- a/src/main/java/com/github/nizshee/client/Client.java +++ b/src/main/java/com/github/nizshee/client/Client.java @@ -1,17 +1,15 @@ package com.github.nizshee.client; -import com.github.nizshee.shared.GetMethod; import com.github.nizshee.shared.ListMethod; import com.github.nizshee.shared.Method; import com.github.nizshee.shared.RemoteFile; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; +import java.io.*; import java.net.Socket; import java.util.List; +@SuppressWarnings("all") public class Client { private final String host; @@ -22,20 +20,29 @@ public Client(String host, int port) throws IOException { this.port = port; } - List getList(String name) throws IOException { - return getValue(new Socket(host, port), 1, new ListMethod(), name); + public List getList(String name) throws IOException { + return Method.getValue(new Socket(host, port), 1, new ListMethod(), name); } - byte[] getFile(String name) throws IOException { - return getValue(new Socket(host, port), 2, new GetMethod(), name); - } - - public static Result getValue(Socket socket, int code, Method method, Value value) - throws IOException { - DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); - DataInputStream dis = new DataInputStream(socket.getInputStream()); - dos.writeInt(code); - method.writeValue(dos, value); - return method.readResult(dis); + public void getFile(String name) throws IOException { + try (Socket socket = new Socket(host, port)) { + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + dos.writeInt(2); + dos.writeUTF(name); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + long size = dis.readLong(); + byte[] buffer = new byte[1024]; + File file = new File(name); + try (FileOutputStream fos = new FileOutputStream(file)) { + long total = 0; + int partSize = 0; + while (partSize != -1 && total < size) { + fos.write(buffer, 0, (int) Math.min(size - total, partSize)); + total += partSize; + partSize = dis.read(buffer); + } + if (total != size) throw new IOException("Not enough bytes"); + } + } } } diff --git a/src/main/java/com/github/nizshee/client/ClientMain.java b/src/main/java/com/github/nizshee/client/ClientMain.java index 6c6c4c2..138d9cb 100644 --- a/src/main/java/com/github/nizshee/client/ClientMain.java +++ b/src/main/java/com/github/nizshee/client/ClientMain.java @@ -13,15 +13,18 @@ public static void main(String[] args) throws Exception { if (args.length == 2 && args[0].equals("list")) { List list = client.getList(args[1]); for (RemoteFile file: list) { - System.out.println(file); + System.out.print(file + " "); + if (file.isDirectory) { + System.out.println(""); + } else { + System.out.println(); + } } } else if (args.length == 2 && args[0].equals("get")) { - byte[] bytes = client.getFile(args[1]); - System.out.println(bytes.length); - for (byte b: bytes) { - System.out.print(b); - } - System.out.println(); + client.getFile(args[1]); + System.out.println("file downloaded"); + } else { + System.out.println("usage: list | get "); } } } diff --git a/src/main/java/com/github/nizshee/server/CopyFileHandler.java b/src/main/java/com/github/nizshee/server/CopyFileHandler.java new file mode 100644 index 0000000..3a80eda --- /dev/null +++ b/src/main/java/com/github/nizshee/server/CopyFileHandler.java @@ -0,0 +1,27 @@ +package com.github.nizshee.server; + + +import java.io.*; + + +@SuppressWarnings("all") +public class CopyFileHandler implements Handler { + @Override + public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { + String name = dis.readUTF(); + File file = new File(name); + try (FileInputStream fis = new FileInputStream(file)) { + if (!file.exists()) { + dos.writeLong(0); + return; + } + dos.writeLong(file.length()); + byte[] buffer = new byte[1024]; + int size = 0; + while (size != -1) { + dos.write(buffer, 0, size); + size = fis.read(buffer); + } + } + } +} diff --git a/src/main/java/com/github/nizshee/server/Handler.java b/src/main/java/com/github/nizshee/server/Handler.java new file mode 100644 index 0000000..b17a800 --- /dev/null +++ b/src/main/java/com/github/nizshee/server/Handler.java @@ -0,0 +1,12 @@ +package com.github.nizshee.server; + + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface Handler { + + void handle(DataInputStream dis, DataOutputStream dos) throws IOException; + +} diff --git a/src/main/java/com/github/nizshee/server/MethodWrapper.java b/src/main/java/com/github/nizshee/server/MethodWrapper.java new file mode 100644 index 0000000..1e5daeb --- /dev/null +++ b/src/main/java/com/github/nizshee/server/MethodWrapper.java @@ -0,0 +1,23 @@ +package com.github.nizshee.server; + + +import com.github.nizshee.shared.Method; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MethodWrapper implements Handler { + private final Method method; + + public MethodWrapper(Method method) { + this.method = method; + } + + @Override + public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { + Request request = method.readRequest(dis); + Response response = method.execute(request); + method.writeResponse(dos, response); + } +} diff --git a/src/main/java/com/github/nizshee/server/Server.java b/src/main/java/com/github/nizshee/server/Server.java index 3fe3f9c..d7189c0 100644 --- a/src/main/java/com/github/nizshee/server/Server.java +++ b/src/main/java/com/github/nizshee/server/Server.java @@ -1,6 +1,9 @@ package com.github.nizshee.server; +import com.github.nizshee.shared.ServerNotRunningException; +import com.github.nizshee.shared.ServerRunningException; + import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; @@ -18,30 +21,35 @@ public Server(InetAddress host, Integer port) { this.port = port; } - public void start(Map handlers) throws IOException { - if (socket != null) throw new IOException("Server already running."); + public synchronized void start(Map handlers) throws IOException, ServerRunningException { + if (socket != null) throw new ServerRunningException(); socket = new ServerSocket(port, 50, host); Thread thread = new Thread(new ServerRunnable(socket, handlers)); thread.start(); } - public void startSync(Map handlers) throws IOException { - if (socket != null) throw new IOException("Server already running."); - socket = new ServerSocket(port, 50, host); + @SuppressWarnings("all") + public void startSync(Map handlers) + throws IOException, ServerRunningException { + synchronized (this) { + if (socket != null) throw new ServerRunningException(); + socket = new ServerSocket(port, 50, host); + } (new ServerRunnable(socket, handlers)).run(); } - public void stop() throws IOException { + public synchronized void stop() throws IOException, ServerNotRunningException { + if (socket == null) throw new ServerNotRunningException(); socket.close(); socket = null; } - private class ServerRunnable implements Runnable { + private static class ServerRunnable implements Runnable { private final ServerSocket serverSocket; - private final Map handlers; + private final Map handlers; - ServerRunnable(ServerSocket serverSocket, Map handlers) { + ServerRunnable(ServerSocket serverSocket, Map handlers) { this.serverSocket = serverSocket; this.handlers = handlers; } @@ -52,19 +60,22 @@ public void run() { //noinspection InfiniteLoopStatement while (true) { Socket socket = serverSocket.accept(); - DataInputStream dis = new DataInputStream(socket.getInputStream()); - DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); - Integer key = dis.readInt(); - if (handlers.containsKey(key)) { - ServerMethodWrapper wrapper = handlers.get(key); - wrapper.handle(dis, dos); + try { + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + Integer key = dis.readInt(); + if (handlers.containsKey(key)) { + Handler wrapper = handlers.get(key); + wrapper.handle(dis, dos); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + socket.close(); } - socket.close(); } - } catch (SocketException ignore) { - System.out.println("Server stopped."); } catch (IOException e) { - System.err.println("Can't accept connection."); + System.err.println(e.getMessage()); } } } diff --git a/src/main/java/com/github/nizshee/server/ServerMain.java b/src/main/java/com/github/nizshee/server/ServerMain.java index d1b99d9..d55c772 100644 --- a/src/main/java/com/github/nizshee/server/ServerMain.java +++ b/src/main/java/com/github/nizshee/server/ServerMain.java @@ -1,40 +1,25 @@ package com.github.nizshee.server; -import com.github.nizshee.shared.GetMethod; import com.github.nizshee.shared.ListMethod; -import java.io.DataInputStream; -import java.io.DataOutputStream; import java.net.InetAddress; -import java.net.Socket; import java.util.HashMap; import java.util.Map; @SuppressWarnings("all") public class ServerMain { - private final static Map handlers; + private final static Map HANDLERS; static { - handlers = new HashMap<>(); - handlers.put(1, new ServerMethodWrapper(new ListMethod())); - handlers.put(2, new ServerMethodWrapper(new GetMethod())); + HANDLERS = new HashMap<>(); + HANDLERS.put(1, new MethodWrapper(new ListMethod())); + HANDLERS.put(2, new CopyFileHandler()); } public static void main(String[] args) throws Exception { Server server = new Server(InetAddress.getByName("localhost"), 8080); - server.startSync(handlers); -// server.start(handlers); -// Thread.sleep(500); -// Socket socket = new Socket("localhost", 8080); -// DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); -// DataInputStream dis = new DataInputStream(socket.getInputStream()); -// dos.writeInt(1); -// dos.writeUTF("/home/roman"); -// Thread.sleep(500); -// int result = dis.readInt(); -// System.out.println(result + 1); -// server.stop(); + server.startSync(HANDLERS); } } diff --git a/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java b/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java deleted file mode 100644 index 483f186..0000000 --- a/src/main/java/com/github/nizshee/server/ServerMethodWrapper.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.nizshee.server; - - -import com.github.nizshee.shared.Method; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public class ServerMethodWrapper { - - private final Method method; - - public ServerMethodWrapper(Method method) { - this.method = method; - } - - public void handle(DataInputStream dis, DataOutputStream dos) throws IOException { - Value value = method.readValue(dis); - Result result = method.apply(value); - method.writeResult(dos, result); - } - -} diff --git a/src/main/java/com/github/nizshee/shared/GetMethod.java b/src/main/java/com/github/nizshee/shared/GetMethod.java deleted file mode 100644 index 87813b1..0000000 --- a/src/main/java/com/github/nizshee/shared/GetMethod.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.github.nizshee.shared; - - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -public class GetMethod implements Method { - - @Override - public byte[] apply(String s) { - try { - return Files.readAllBytes(Paths.get(s)); - } catch (IOException ignore) { - return new byte[0]; - } - } - - @Override - public void writeValue(DataOutputStream dos, String s) throws IOException { - dos.writeUTF(s); - } - - @Override - public String readValue(DataInputStream dis) throws IOException { - return dis.readUTF(); - } - - @Override - public void writeResult(DataOutputStream dos, byte[] bytes) throws IOException { - dos.writeInt(bytes.length); - dos.write(bytes, 0, bytes.length); - for (Byte aByte : bytes) { - dos.writeByte(aByte); - } - } - - @Override - public byte[] readResult(DataInputStream dis) throws IOException { - int length = dis.readInt(); - byte[] bytes = new byte[length]; - int read = dis.read(bytes, 0, length); - if (read != length) throw new IOException("Bad read,"); - return bytes; - } -} diff --git a/src/main/java/com/github/nizshee/shared/ListMethod.java b/src/main/java/com/github/nizshee/shared/ListMethod.java index 6348e33..839e9c8 100644 --- a/src/main/java/com/github/nizshee/shared/ListMethod.java +++ b/src/main/java/com/github/nizshee/shared/ListMethod.java @@ -5,7 +5,6 @@ import java.io.DataOutputStream; import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; @@ -13,7 +12,7 @@ public class ListMethod implements Method> { @Override - public List apply(String name) { + public List execute(String name) { Path dir = Paths.get(name); File[] files = dir.toFile().listFiles(); if (files == null) return Collections.emptyList(); @@ -25,17 +24,17 @@ public List apply(String name) { } @Override - public void writeValue(DataOutputStream dos, String s) throws IOException { + public void writeRequest(DataOutputStream dos, String s) throws IOException { dos.writeUTF(s); } @Override - public String readValue(DataInputStream dis) throws IOException { + public String readRequest(DataInputStream dis) throws IOException { return dis.readUTF(); } @Override - public void writeResult(DataOutputStream dos, List remoteFiles) throws IOException { + public void writeResponse(DataOutputStream dos, List remoteFiles) throws IOException { dos.writeInt(remoteFiles.size()); for (RemoteFile file: remoteFiles) { dos.writeUTF(file.name); diff --git a/src/main/java/com/github/nizshee/shared/Method.java b/src/main/java/com/github/nizshee/shared/Method.java index cadfc68..4a6fd2a 100644 --- a/src/main/java/com/github/nizshee/shared/Method.java +++ b/src/main/java/com/github/nizshee/shared/Method.java @@ -4,15 +4,27 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.net.Socket; -public interface Method { - Result apply(Value value); +public interface Method { + Response execute(Request request); - void writeValue(DataOutputStream dos, Value value) throws IOException; + void writeRequest(DataOutputStream dos, Request request) throws IOException; - Value readValue(DataInputStream dis) throws IOException; + Request readRequest(DataInputStream dis) throws IOException; - void writeResult(DataOutputStream dos, Result result) throws IOException; + void writeResponse(DataOutputStream dos, Response response) throws IOException; - Result readResult(DataInputStream dis) throws IOException; + Response readResult(DataInputStream dis) throws IOException; + + static Response getValue(Socket socket, int code, Method method, Request request) + throws IOException { + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + dos.writeInt(code); + method.writeRequest(dos, request); + Response response = method.readResult(dis); + socket.close(); + return response; + } } diff --git a/src/main/java/com/github/nizshee/shared/RemoteFile.java b/src/main/java/com/github/nizshee/shared/RemoteFile.java index 52fa310..839ef60 100644 --- a/src/main/java/com/github/nizshee/shared/RemoteFile.java +++ b/src/main/java/com/github/nizshee/shared/RemoteFile.java @@ -1,6 +1,7 @@ package com.github.nizshee.shared; +@SuppressWarnings("all") public class RemoteFile { public final boolean isDirectory; diff --git a/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java b/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java new file mode 100644 index 0000000..581c8a2 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ServerNotRunningException.java @@ -0,0 +1,8 @@ +package com.github.nizshee.shared; + + +public class ServerNotRunningException extends Exception { + public ServerNotRunningException() { + super("Server not running."); + } +} diff --git a/src/main/java/com/github/nizshee/shared/ServerRunningException.java b/src/main/java/com/github/nizshee/shared/ServerRunningException.java new file mode 100644 index 0000000..0abd8e9 --- /dev/null +++ b/src/main/java/com/github/nizshee/shared/ServerRunningException.java @@ -0,0 +1,8 @@ +package com.github.nizshee.shared; + + +public class ServerRunningException extends Exception { + public ServerRunningException() { + super("Server already running."); + } +} diff --git a/src/test/java/com/github/nizshee/ClientServerTest.java b/src/test/java/com/github/nizshee/ClientServerTest.java index 6faecbc..4016896 100644 --- a/src/test/java/com/github/nizshee/ClientServerTest.java +++ b/src/test/java/com/github/nizshee/ClientServerTest.java @@ -1,8 +1,9 @@ package com.github.nizshee; import com.github.nizshee.client.Client; +import com.github.nizshee.server.MethodWrapper; import com.github.nizshee.server.Server; -import com.github.nizshee.server.ServerMethodWrapper; +import com.github.nizshee.server.Handler; import com.github.nizshee.shared.Method; import org.junit.Test; @@ -24,23 +25,23 @@ public class ClientServerTest { private final static AtomicBoolean trigger = new AtomicBoolean(false); public static final Method method = new Method() { @Override - public String apply(String s) { + public String execute(String s) { trigger.set(true); return s + s; } @Override - public void writeValue(DataOutputStream dos, String s) throws IOException { + public void writeRequest(DataOutputStream dos, String s) throws IOException { dos.writeUTF(s); } @Override - public String readValue(DataInputStream dis) throws IOException { + public String readRequest(DataInputStream dis) throws IOException { return dis.readUTF(); } @Override - public void writeResult(DataOutputStream dos, String s) throws IOException { + public void writeResponse(DataOutputStream dos, String s) throws IOException { dos.writeUTF(s); } @@ -49,10 +50,10 @@ public String readResult(DataInputStream dis) throws IOException { return dis.readUTF(); } }; - private static final Map handlers; + private static final Map handlers; static { handlers = new HashMap<>(); - handlers.put(code, new ServerMethodWrapper(method)); + handlers.put(code, new MethodWrapper(method)); } @Test @@ -62,7 +63,7 @@ public void clientServerTest() throws Exception { Thread.sleep(500); Client client = new Client("localhost", 8080); Socket socket = new Socket("localhost", 8080); - String result = Client.getValue(socket, code, method, "abcd"); + String result = Method.getValue(socket, code, method, "abcd"); assertEquals("abcdabcd", result); socket.close(); server.stop(); diff --git a/src/test/java/com/github/nizshee/shared/GetMethodTest.java b/src/test/java/com/github/nizshee/shared/GetMethodTest.java deleted file mode 100644 index 7bafc1f..0000000 --- a/src/test/java/com/github/nizshee/shared/GetMethodTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.github.nizshee.shared; - -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; - -import static org.junit.Assert.*; - - -public class GetMethodTest { - - private final static GetMethod method = new GetMethod(); - - @SuppressWarnings("ConstantConditions") - @Test - public void applyTest() throws Exception { - byte[] bytes1 = method.apply(getClass().getClassLoader().getResource("test/a").getPath()); - byte[] bytes2 = method.apply(getClass().getClassLoader().getResource("test/b").getPath()); - - assertArrayEquals("123".getBytes(), bytes1); - assertArrayEquals("12345".getBytes(), bytes2); - } - - @Test - public void valueTest() throws Exception { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(bos); - - method.writeValue(dos, "anotherTestString1"); - method.writeValue(dos, "anotherTestString2"); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - DataInputStream dis = new DataInputStream(bis); - - assertEquals("anotherTestString1", method.readValue(dis)); - assertEquals("anotherTestString2", method.readValue(dis)); - } - - @Test - public void resultTest() throws Exception { - byte[] bytes1 = {}; - byte[] bytes2 = {1, 2, 3, 4}; - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - DataOutputStream dos = new DataOutputStream(bos); - - method.writeResult(dos, bytes1); - method.writeResult(dos, bytes2); - - ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); - DataInputStream dis = new DataInputStream(bis); - - assertArrayEquals(bytes1, method.readResult(dis)); - assertArrayEquals(bytes2, method.readResult(dis)); - } -} diff --git a/src/test/java/com/github/nizshee/shared/ListMethodTest.java b/src/test/java/com/github/nizshee/shared/ListMethodTest.java index dd96f0f..452866e 100644 --- a/src/test/java/com/github/nizshee/shared/ListMethodTest.java +++ b/src/test/java/com/github/nizshee/shared/ListMethodTest.java @@ -17,7 +17,7 @@ public class ListMethodTest { public void applyTest() throws Exception { @SuppressWarnings("ConstantConditions") - List result = method.apply(getClass().getClassLoader().getResource("test/").getPath()); + List result = method.execute(getClass().getClassLoader().getResource("test/").getPath()); assertEquals(Arrays.asList( new RemoteFile("a", false), new RemoteFile("b", false), @@ -31,14 +31,14 @@ public void valueTest() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); - method.writeValue(dos, "testString1"); - method.writeValue(dos, "testString2"); + method.writeRequest(dos, "testString1"); + method.writeRequest(dos, "testString2"); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); - assertEquals("testString1", method.readValue(dis)); - assertEquals("testString2", method.readValue(dis)); + assertEquals("testString1", method.readRequest(dis)); + assertEquals("testString2", method.readRequest(dis)); } @Test @@ -52,8 +52,8 @@ public void resultTest() throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); - method.writeResult(dos, list1); - method.writeResult(dos, list2); + method.writeResponse(dos, list1); + method.writeResponse(dos, list2); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); DataInputStream dis = new DataInputStream(bis); diff --git a/src/test/resources/test/c/c b/src/test/resources/test/c/c new file mode 100644 index 0000000..e69de29