diff --git a/Lab5Client/build/tmp/compileJava/previous-compilation-data.bin b/Lab5Client/build/tmp/compileJava/previous-compilation-data.bin index 98c7542..a95fe0f 100644 Binary files a/Lab5Client/build/tmp/compileJava/previous-compilation-data.bin and b/Lab5Client/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/Lab5Core/build/docs/javadoc/allclasses-frame.html b/Lab5Core/build/docs/javadoc/allclasses-frame.html index 7172ad1..ab95041 100644 --- a/Lab5Core/build/docs/javadoc/allclasses-frame.html +++ b/Lab5Core/build/docs/javadoc/allclasses-frame.html @@ -23,7 +23,6 @@
  • Command
  • CommandLineHandler
  • CommandLineHandler.CommandLineNotInitialized
  • -
  • CommandLineHandler.SaveCommand
  • CommandRegistry
  • CommandRegistry.CommandAlreadyExistsException
  • CommandRegistry.CommandNotFoundException
  • diff --git a/Lab5Core/build/docs/javadoc/allclasses-noframe.html b/Lab5Core/build/docs/javadoc/allclasses-noframe.html index 974aea6..a526f3d 100644 --- a/Lab5Core/build/docs/javadoc/allclasses-noframe.html +++ b/Lab5Core/build/docs/javadoc/allclasses-noframe.html @@ -23,7 +23,6 @@
  • Command
  • CommandLineHandler
  • CommandLineHandler.CommandLineNotInitialized
  • -
  • CommandLineHandler.SaveCommand
  • CommandRegistry
  • CommandRegistry.CommandAlreadyExistsException
  • CommandRegistry.CommandNotFoundException
  • diff --git a/Lab5Core/build/docs/javadoc/common/commandline/package-frame.html b/Lab5Core/build/docs/javadoc/common/commandline/package-frame.html index b6b9ff4..3fa7660 100644 --- a/Lab5Core/build/docs/javadoc/common/commandline/package-frame.html +++ b/Lab5Core/build/docs/javadoc/common/commandline/package-frame.html @@ -18,7 +18,6 @@ diff --git a/Lab5Core/build/docs/javadoc/common/commandline/package-summary.html b/Lab5Core/build/docs/javadoc/common/commandline/package-summary.html index 128ca54..87ae181 100644 --- a/Lab5Core/build/docs/javadoc/common/commandline/package-summary.html +++ b/Lab5Core/build/docs/javadoc/common/commandline/package-summary.html @@ -107,10 +107,6 @@ на выполнение из регистра команд, используйте метод CommandLineHandler.start() для его запуска - -CommandLineHandler.SaveCommand -  - CommandRegistry diff --git a/Lab5Core/build/docs/javadoc/index-all.html b/Lab5Core/build/docs/javadoc/index-all.html index cdebda9..999b67a 100644 --- a/Lab5Core/build/docs/javadoc/index-all.html +++ b/Lab5Core/build/docs/javadoc/index-all.html @@ -306,8 +306,12 @@
     
    executeOnClient() - Method in class common.commandline.pdcommands.PeopleDatabaseCommand
     
    +
    executeScript(String) - Method in class common.commandline.CommandLineHandler
    +
     
    ExecuteScriptCommand() - Constructor for class common.commandline.CommandLineHandler.ExecuteScriptCommand
     
    +
    exit() - Method in class common.commandline.CommandLineHandler
    +
     
    ExitCommand() - Constructor for class common.commandline.CommandLineHandler.ExitCommand
     
    @@ -364,6 +368,10 @@
     
    history - Variable in class common.commandline.CommandLineHandler
     
    +
    history() - Method in class common.commandline.CommandLineHandler
    +
     
    +
    history(int) - Method in class common.commandline.CommandLineHandler
    +
     
    HistoryCommand() - Constructor for class common.commandline.CommandLineHandler.HistoryCommand
     
    @@ -510,6 +518,8 @@
    Метод, сохраняющий базу данных в файл, находящемся по пути, указанном в переменной окружения PeopleDatabase.ENV_VAR
    +
    save(PeopleDatabase) - Method in class common.commandline.CommandLineHandler
    +
     
    SaveCommand() - Constructor for class common.commandline.CommandLineHandler.SaveCommand
     
    setHeight(Integer) - Method in class common.data.Person
    diff --git a/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin b/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin index 6d136d8..5c50243 100644 Binary files a/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin and b/Lab5Core/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/Lab5Core/src/main/java/common/commandline/CommandLineHandler.java b/Lab5Core/src/main/java/common/commandline/CommandLineHandler.java index 71b236d..489a25e 100644 --- a/Lab5Core/src/main/java/common/commandline/CommandLineHandler.java +++ b/Lab5Core/src/main/java/common/commandline/CommandLineHandler.java @@ -176,13 +176,75 @@ public abstract class CommandLineHandler { this.reader = new BufferedReader(reader); } + public CommandResult executeScript(String fileName) { + return executeScript(new Object[]{ fileName }); + } + + private CommandResult executeScript(Object[] args) { + String fileName = (String) args[0]; + File file = new File(fileName); + if (!file.exists() || file.isDirectory()) { + Response response = DefaultResponse.FILE_NOT_FOUND; + return new CommandResult(response.getMsg(), response); + } + + Reader streamReader; + try { + streamReader = new InputStreamReader(new FileInputStream(file)); + } catch (FileNotFoundException e) { + Response response = DefaultResponse.UNKNOWN; + return new CommandResult(response.getMsg(), response); + } + addNewInput(streamReader, fileName); + Response response = DefaultResponse.OK; + return new CommandResult(response.getMsg(), response); + } + + public CommandResult history() { + return history(new Object[]{}); + } + + public CommandResult history(int amount) { + return history(new Object[]{ amount }); + } + + private CommandResult history (Object[] args) { + int lines = args.length > 0 ? (int) args[0] : 6; + int start = lines < history.size() ? history.size() - lines : 0; + StringBuilder result = new StringBuilder("История последних команд:\n"); + for (int i = start; i < history.size(); i++) + result.append(history.get(i)).append("\n"); + return new CommandResult(result.toString(), DefaultResponse.OK); + } + + public CommandResult exit() { + return exit(new Object[]{}); + } + + private CommandResult exit(Object[] args) { + isActive = false; + return new CommandResult("Выход из программы...", DefaultResponse.OK); + } + + public CommandResult save(PeopleDatabase peopleDatabase) { + return save(new Object[]{ peopleDatabase }); + } + + private CommandResult save(Object[] args) { + PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; + try { + peopleDatabase.save(); + Response response = DefaultResponse.OK; + return new CommandResult(response.getMsg(), response); + } catch (Database.DatabaseSaveFailedException e) { + return new CommandResult(e.getMessage(), PeopleDatabaseResponse.SAVE_FAILED); + } + } + public class ExitCommand extends Command { public ExitCommand() { super("exit", true, "exit : завершить программу (без сохранения в файл)"); - this.executable = args -> { - isActive = false; - return new CommandResult("Выход из программы...", DefaultResponse.OK); - }; + this.executable = CommandLineHandler.this::exit; } @Override @@ -194,14 +256,7 @@ public abstract class CommandLineHandler { public class HistoryCommand extends Command { public HistoryCommand() { super("history", true, "history [count] : вывести последние count введенных команд, по умолчанию count равен 6"); - this.executable = args -> { - int lines = args.length > 0 ? (int) args[0] : 6; - int start = lines < history.size() ? history.size() - lines : 0; - StringBuilder result = new StringBuilder("История последних команд:\n"); - for (int i = start; i < history.size(); i++) - result.append(history.get(i)).append("\n"); - return new CommandResult(result.toString(), DefaultResponse.OK); - }; + this.executable = CommandLineHandler.this::history; } @Override @@ -222,25 +277,7 @@ public abstract class CommandLineHandler { public class ExecuteScriptCommand extends Command { public ExecuteScriptCommand() { super("execute_script", true, "execute_script {file_name} : считать и исполнить скрипт из указанного файла. В скрипте содержатся команды в таком же виде, в котором их вводит пользователь в интерактивном режиме."); - this.executable = args -> { - String fileName = (String) args[0]; - File file = new File(fileName); - if (!file.exists() || file.isDirectory()) { - Response response = DefaultResponse.FILE_NOT_FOUND; - return new CommandResult(response.getMsg(), response); - } - - Reader streamReader; - try { - streamReader = new InputStreamReader(new FileInputStream(file)); - } catch (FileNotFoundException e) { - Response response = DefaultResponse.UNKNOWN; - return new CommandResult(response.getMsg(), response); - } - addNewInput(streamReader, fileName); - Response response = DefaultResponse.OK; - return new CommandResult(response.getMsg(), response); - }; + this.executable = CommandLineHandler.this::executeScript; } @Override @@ -254,19 +291,10 @@ public abstract class CommandLineHandler { } } - public static class SaveCommand extends PeopleDatabaseCommand { + public class SaveCommand extends PeopleDatabaseCommand { public SaveCommand() { super("save", true, "save : сохранить коллекцию в файл"); - this.executable = args -> { - PeopleDatabase peopleDatabase = (PeopleDatabase) args[0]; - try { - peopleDatabase.save(); - Response response = DefaultResponse.OK; - return new CommandResult(response.getMsg(), response); - } catch (Database.DatabaseSaveFailedException e) { - return new CommandResult(e.getMessage(), PeopleDatabaseResponse.SAVE_FAILED); - } - }; + this.executable = CommandLineHandler.this::save; } @Override diff --git a/Lab5Server/build/classes/java/main/server/Lab5Server.class b/Lab5Server/build/classes/java/main/server/Lab5Server.class index b68bc39..88022c7 100644 Binary files a/Lab5Server/build/classes/java/main/server/Lab5Server.class and b/Lab5Server/build/classes/java/main/server/Lab5Server.class differ diff --git a/Lab5Server/build/libs/lab5.xml b/Lab5Server/build/libs/lab5.xml index f88d11c..48bc1c5 100644 --- a/Lab5Server/build/libs/lab5.xml +++ b/Lab5Server/build/libs/lab5.xml @@ -1,22 +1,4 @@ - - wasd - - 2.0 - 2.0 - - 2022-05-16 - 321 - 3213123123 - BLACK - CHINA - - 2.0 - 2.0 - 2 - 2 - - 2022-05-10 diff --git a/Lab5Server/build/tmp/compileJava/previous-compilation-data.bin b/Lab5Server/build/tmp/compileJava/previous-compilation-data.bin index e727e97..0d942fb 100644 Binary files a/Lab5Server/build/tmp/compileJava/previous-compilation-data.bin and b/Lab5Server/build/tmp/compileJava/previous-compilation-data.bin differ diff --git a/Lab5Server/src/main/java/server/Lab5Server.java b/Lab5Server/src/main/java/server/Lab5Server.java index 462b261..49b21e5 100644 --- a/Lab5Server/src/main/java/server/Lab5Server.java +++ b/Lab5Server/src/main/java/server/Lab5Server.java @@ -28,6 +28,8 @@ public class Lab5Server { UDPServer udp = new UDPServer(ConnectionProperties.getPort(), LOGGER); if (!udp.connect()) System.exit(-1); + Runtime.getRuntime().addShutdownHook(new Thread(() -> cmd.save(peopleDatabase))); + Thread thread = new Thread(() -> { while (true) udp.receive(peopleDatabase);