added shutdown hook which saves data on the server

This commit is contained in:
Egor 2022-05-16 15:55:16 +03:00
parent 822dba1f63
commit d19dd08ec9
12 changed files with 82 additions and 67 deletions

View file

@ -23,7 +23,6 @@
<li><a href="common/commandline/Command.html" title="class in common.commandline" target="classFrame">Command</a></li>
<li><a href="common/commandline/CommandLineHandler.html" title="class in common.commandline" target="classFrame">CommandLineHandler</a></li>
<li><a href="common/commandline/CommandLineHandler.CommandLineNotInitialized.html" title="class in common.commandline" target="classFrame">CommandLineHandler.CommandLineNotInitialized</a></li>
<li><a href="common/commandline/CommandLineHandler.SaveCommand.html" title="class in common.commandline" target="classFrame">CommandLineHandler.SaveCommand</a></li>
<li><a href="common/commandline/CommandRegistry.html" title="class in common.commandline" target="classFrame">CommandRegistry</a></li>
<li><a href="common/commandline/CommandRegistry.CommandAlreadyExistsException.html" title="class in common.commandline" target="classFrame">CommandRegistry.CommandAlreadyExistsException</a></li>
<li><a href="common/commandline/CommandRegistry.CommandNotFoundException.html" title="class in common.commandline" target="classFrame">CommandRegistry.CommandNotFoundException</a></li>

View file

@ -23,7 +23,6 @@
<li><a href="common/commandline/Command.html" title="class in common.commandline">Command</a></li>
<li><a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></li>
<li><a href="common/commandline/CommandLineHandler.CommandLineNotInitialized.html" title="class in common.commandline">CommandLineHandler.CommandLineNotInitialized</a></li>
<li><a href="common/commandline/CommandLineHandler.SaveCommand.html" title="class in common.commandline">CommandLineHandler.SaveCommand</a></li>
<li><a href="common/commandline/CommandRegistry.html" title="class in common.commandline">CommandRegistry</a></li>
<li><a href="common/commandline/CommandRegistry.CommandAlreadyExistsException.html" title="class in common.commandline">CommandRegistry.CommandAlreadyExistsException</a></li>
<li><a href="common/commandline/CommandRegistry.CommandNotFoundException.html" title="class in common.commandline">CommandRegistry.CommandNotFoundException</a></li>

View file

@ -18,7 +18,6 @@
<ul title="Classes">
<li><a href="Command.html" title="class in common.commandline" target="classFrame">Command</a></li>
<li><a href="CommandLineHandler.html" title="class in common.commandline" target="classFrame">CommandLineHandler</a></li>
<li><a href="CommandLineHandler.SaveCommand.html" title="class in common.commandline" target="classFrame">CommandLineHandler.SaveCommand</a></li>
<li><a href="CommandRegistry.html" title="class in common.commandline" target="classFrame">CommandRegistry</a></li>
<li><a href="CommandRegistry.HelpCommand.html" title="class in common.commandline" target="classFrame">CommandRegistry.HelpCommand</a></li>
</ul>

View file

@ -107,10 +107,6 @@
на выполнение из регистра команд, используйте метод <a href="../../common/commandline/CommandLineHandler.html#start--"><code>CommandLineHandler.start()</code></a> для его запуска</div>
</td>
</tr>
<tr class="rowColor">
<td class="colFirst"><a href="../../common/commandline/CommandLineHandler.SaveCommand.html" title="class in common.commandline">CommandLineHandler.SaveCommand</a></td>
<td class="colLast">&nbsp;</td>
</tr>
<tr class="altColor">
<td class="colFirst"><a href="../../common/commandline/CommandRegistry.html" title="class in common.commandline">CommandRegistry</a></td>
<td class="colLast">

View file

@ -306,8 +306,12 @@
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/pdcommands/PeopleDatabaseCommand.html#executeOnClient--">executeOnClient()</a></span> - Method in class common.commandline.pdcommands.<a href="common/commandline/pdcommands/PeopleDatabaseCommand.html" title="class in common.commandline.pdcommands">PeopleDatabaseCommand</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#executeScript-java.lang.String-">executeScript(String)</a></span> - Method in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.ExecuteScriptCommand.html#ExecuteScriptCommand--">ExecuteScriptCommand()</a></span> - Constructor for class common.commandline.<a href="common/commandline/CommandLineHandler.ExecuteScriptCommand.html" title="class in common.commandline">CommandLineHandler.ExecuteScriptCommand</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#exit--">exit()</a></span> - Method in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.ExitCommand.html#ExitCommand--">ExitCommand()</a></span> - Constructor for class common.commandline.<a href="common/commandline/CommandLineHandler.ExitCommand.html" title="class in common.commandline">CommandLineHandler.ExitCommand</a></dt>
<dd>&nbsp;</dd>
</dl>
@ -364,6 +368,10 @@
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#history">history</a></span> - Variable in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#history--">history()</a></span> - Method in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#history-int-">history(int)</a></span> - Method in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.HistoryCommand.html#HistoryCommand--">HistoryCommand()</a></span> - Constructor for class common.commandline.<a href="common/commandline/CommandLineHandler.HistoryCommand.html" title="class in common.commandline">CommandLineHandler.HistoryCommand</a></dt>
<dd>&nbsp;</dd>
</dl>
@ -510,6 +518,8 @@
<div class="block">Метод, сохраняющий базу данных в файл, находящемся по пути, указанном в
переменной окружения <a href="common/collection/PeopleDatabase.html#ENV_VAR"><code>PeopleDatabase.ENV_VAR</code></a></div>
</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.html#save-common.collection.PeopleDatabase-">save(PeopleDatabase)</a></span> - Method in class common.commandline.<a href="common/commandline/CommandLineHandler.html" title="class in common.commandline">CommandLineHandler</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/commandline/CommandLineHandler.SaveCommand.html#SaveCommand--">SaveCommand()</a></span> - Constructor for class common.commandline.<a href="common/commandline/CommandLineHandler.SaveCommand.html" title="class in common.commandline">CommandLineHandler.SaveCommand</a></dt>
<dd>&nbsp;</dd>
<dt><span class="memberNameLink"><a href="common/data/Person.html#setHeight-java.lang.Integer-">setHeight(Integer)</a></span> - Method in class common.data.<a href="common/data/Person.html" title="class in common.data">Person</a></dt>

View file

@ -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

View file

@ -1,22 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<peopleDatabase>
<person>
<name>wasd</name>
<coordinates>
<x>2.0</x>
<y>2.0</y>
</coordinates>
<creationDate>2022-05-16</creationDate>
<height>321</height>
<passportID>3213123123</passportID>
<eyeColor>BLACK</eyeColor>
<nationality>CHINA</nationality>
<location>
<x>2.0</x>
<y>2.0</y>
<z>2</z>
<name>2</name>
</location>
</person>
<initDate>2022-05-10</initDate>
</peopleDatabase>

View file

@ -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);