changed some methods that would return null in place of a message in CommandResult
removed null check when calling CommandResult#getValue()
This commit is contained in:
parent
a74c7496a5
commit
dda459c971
13 changed files with 53 additions and 39 deletions
Binary file not shown.
|
@ -1,2 +1,2 @@
|
|||
hostname=helios.se.ifmo.ru
|
||||
hostname=localhost
|
||||
port=1234
|
|
@ -1,2 +1,2 @@
|
|||
hostname=s316304@helios.se.ifmo.ru
|
||||
port=1234
|
||||
hostname=localhost
|
||||
port=2222
|
|
@ -1,8 +1,10 @@
|
|||
package client.commandline;
|
||||
|
||||
import client.net.UDPClient;
|
||||
import common.commandline.Executables;
|
||||
import common.commandline.response.CommandResult;
|
||||
import common.commandline.response.DefaultResponse;
|
||||
import common.commandline.response.Response;
|
||||
import common.parser.ConnectionProperties;
|
||||
import common.util.UtilFunctions;
|
||||
|
||||
|
@ -68,7 +70,7 @@ public final class CommandLineHandler {
|
|||
System.out.flush();
|
||||
}
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("Что-то пошло не так");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,11 +94,10 @@ public final class CommandLineHandler {
|
|||
}
|
||||
boolean argsValid = command.validate(args);
|
||||
if (!argsValid) return;
|
||||
CommandResult result = command.isClientOnly() || clientMode ?
|
||||
command.executeOnClient() : executeOnServer(udp, command);
|
||||
boolean isClient = command.isClientOnly() || clientMode;
|
||||
CommandResult result = isClient ? command.executeOnClient() : executeOnServer(udp, command);
|
||||
PrintStream ps = result.getResponse() == DefaultResponse.OK ? System.out : System.err;
|
||||
if (result.getValue() == null) ps.println(result.getResponse().getMsg());
|
||||
else ps.println(result.getValue());
|
||||
ps.println(result.getValue());
|
||||
updateHistory(alias);
|
||||
}
|
||||
|
||||
|
@ -243,18 +244,21 @@ public final class CommandLineHandler {
|
|||
args -> {
|
||||
String fileName = (String) args[0];
|
||||
File file = new File(fileName);
|
||||
if (!file.exists() || file.isDirectory())
|
||||
return new CommandResult(null, DefaultResponse.FILE_NOT_FOUND);
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
return new CommandResult(null, DefaultResponse.UNKNOWN);
|
||||
Response response = DefaultResponse.UNKNOWN;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}
|
||||
instance.addNewInput(streamReader, fileName);
|
||||
return new CommandResult(null, DefaultResponse.OK);
|
||||
Response response = DefaultResponse.OK;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -45,10 +45,8 @@ public class UDPClient {
|
|||
public CommandResult send(Executable executable, Object[] args) {
|
||||
if (!available) return new CommandResult(null, DefaultResponse.HOST_NOT_FOUND);
|
||||
byte[] buffer;
|
||||
try (
|
||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream)
|
||||
) {
|
||||
try (ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) {
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);
|
||||
objectOutputStream.writeObject(executable);
|
||||
objectOutputStream.writeObject(args);
|
||||
buffer = byteOutputStream.toByteArray();
|
||||
|
|
Binary file not shown.
|
@ -91,8 +91,8 @@
|
|||
<ul>
|
||||
<li type="circle">java.lang.Enum<E> (implements java.lang.Comparable<T>, java.io.Serializable)
|
||||
<ul>
|
||||
<li type="circle">common.data.<a href="../../common/data/Country.html" title="enum in common.data"><span class="typeNameLink">Country</span></a> (implements java.io.Serializable)</li>
|
||||
<li type="circle">common.data.<a href="../../common/data/Color.html" title="enum in common.data"><span class="typeNameLink">Color</span></a> (implements java.io.Serializable)</li>
|
||||
<li type="circle">common.data.<a href="../../common/data/Country.html" title="enum in common.data"><span class="typeNameLink">Country</span></a> (implements java.io.Serializable)</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -5,6 +5,7 @@ import common.collection.PeopleDatabase;
|
|||
import common.commandline.response.CommandResult;
|
||||
import common.commandline.response.DefaultResponse;
|
||||
import common.commandline.response.PeopleDatabaseResponse;
|
||||
import common.commandline.response.Response;
|
||||
import common.data.Person;
|
||||
|
||||
import java.util.Collections;
|
||||
|
@ -15,26 +16,30 @@ public enum Executables {
|
|||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
Person person = (Person) args[1];
|
||||
boolean success = peopleDatabase.getCollection().add(person);
|
||||
return new CommandResult(null, success ? DefaultResponse.OK : PeopleDatabaseResponse.FAILED_TO_ADD);
|
||||
Response response = success ? DefaultResponse.OK : PeopleDatabaseResponse.FAILED_TO_ADD;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}),
|
||||
ADD_IF_MAX(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
Person person = (Person) args[1];
|
||||
Person last = peopleDatabase.getCollection().last();
|
||||
if (person.compareTo(last) > 0) return ADD.executable.execute(args);
|
||||
return new CommandResult(null, PeopleDatabaseResponse.FAILED_TO_ADD);
|
||||
Response response = PeopleDatabaseResponse.FAILED_TO_ADD;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}),
|
||||
ADD_IF_MIN(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
Person person = (Person) args[1];
|
||||
Person first = peopleDatabase.getCollection().first();
|
||||
if (person.compareTo(first) < 0) return ADD.executable.execute(args);
|
||||
return new CommandResult(null, PeopleDatabaseResponse.FAILED_TO_ADD);
|
||||
Response response = PeopleDatabaseResponse.FAILED_TO_ADD;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}),
|
||||
CLEAR(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
peopleDatabase.getCollection().clear();
|
||||
return new CommandResult(null, DefaultResponse.OK);
|
||||
Response response = DefaultResponse.OK;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}),
|
||||
FILTER_CONTAINS_NAME(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
|
@ -64,13 +69,15 @@ public enum Executables {
|
|||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
long id = (long) args[1];
|
||||
boolean success = peopleDatabase.getCollection().removeIf(p -> p.getId().equals(id));
|
||||
return new CommandResult(null, success ? DefaultResponse.OK : PeopleDatabaseResponse.ELEMENT_NOT_FOUND);
|
||||
Response response = success ? DefaultResponse.OK : PeopleDatabaseResponse.ELEMENT_NOT_FOUND;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
}),
|
||||
SAVE(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
try {
|
||||
peopleDatabase.save();
|
||||
return new CommandResult(null, DefaultResponse.OK);
|
||||
Response response = DefaultResponse.OK;
|
||||
return new CommandResult(response.getMsg(), response);
|
||||
} catch (Database.DatabaseSaveFailedException e) {
|
||||
return new CommandResult(e.getMessage(), PeopleDatabaseResponse.SAVE_FAILED);
|
||||
}
|
||||
|
@ -83,11 +90,11 @@ public enum Executables {
|
|||
}),
|
||||
SUM_OF_HEIGHT(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
int sum = peopleDatabase.getCollection()
|
||||
String sum = "Сумма ростов всех людей в коллекции - " + peopleDatabase.getCollection()
|
||||
.stream()
|
||||
.mapToInt(p -> p.getHeight() == null ? 0 : p.getHeight())
|
||||
.sum();
|
||||
return new CommandResult("Сумма ростов всех людей в коллекции - " + sum, DefaultResponse.OK);
|
||||
return new CommandResult(sum, DefaultResponse.OK);
|
||||
}),
|
||||
UPDATE(args -> {
|
||||
PeopleDatabase peopleDatabase = (PeopleDatabase) args[0];
|
||||
|
@ -97,7 +104,8 @@ public enum Executables {
|
|||
.stream()
|
||||
.filter(p -> p.getId().equals(id))
|
||||
.findFirst();
|
||||
if (!optionalPerson.isPresent()) return new CommandResult(null, PeopleDatabaseResponse.ELEMENT_NOT_FOUND);
|
||||
Response response = PeopleDatabaseResponse.ELEMENT_NOT_FOUND;
|
||||
if (!optionalPerson.isPresent()) return new CommandResult(response.getMsg(), response);
|
||||
Person oldPerson = optionalPerson.get();
|
||||
CommandResult result = REMOVE_BY_ID.executable.execute(new Object[]{ peopleDatabase, id });
|
||||
if (result.getResponse() != DefaultResponse.OK) return result;
|
||||
|
|
Binary file not shown.
|
@ -1,2 +1,2 @@
|
|||
hostname=localhost
|
||||
hostname=s316304@helios.se.ifmo.ru
|
||||
port=1234
|
|
@ -2,8 +2,10 @@ package server.net;
|
|||
|
||||
import common.collection.PeopleDatabase;
|
||||
import common.commandline.Executable;
|
||||
import common.commandline.Executables;
|
||||
import common.commandline.response.CommandResult;
|
||||
import common.commandline.response.DefaultResponse;
|
||||
import common.commandline.response.Response;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
@ -51,10 +53,8 @@ public class UDPServer {
|
|||
|
||||
public void send(CommandResult result, InetAddress address, int port) {
|
||||
byte[] buffer;
|
||||
try (
|
||||
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream)
|
||||
) {
|
||||
try (ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream()) {
|
||||
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);
|
||||
objectOutputStream.writeObject(result);
|
||||
buffer = byteOutputStream.toByteArray();
|
||||
DatagramPacket request = new DatagramPacket(buffer, buffer.length, address, port);
|
||||
|
@ -80,17 +80,21 @@ public class UDPServer {
|
|||
if (args[0] == null) args[0] = peopleDatabase;
|
||||
result = command.execute(args);
|
||||
} catch (IOException e) {
|
||||
logger.info("Не удалось преобразовать полученные данные, данные были повреждены во время передачи");
|
||||
result = new CommandResult(null, DefaultResponse.SERVER_ERROR);
|
||||
logger.severe("Не удалось преобразовать полученные данные, данные были повреждены во время передачи");
|
||||
Response response = DefaultResponse.SERVER_ERROR;
|
||||
result = new CommandResult(response.getMsg(), response);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
logger.info("Не удалось преобразовать полученные данные, классов полученных объектов не существует");
|
||||
result = new CommandResult(null, DefaultResponse.CLASS_NOT_FOUND);
|
||||
logger.severe("Не удалось преобразовать полученные данные, классов полученных объектов не существует");
|
||||
Response response = DefaultResponse.CLASS_NOT_FOUND;
|
||||
result = new CommandResult(response.getMsg(), response);
|
||||
} catch (ClassCastException e) {
|
||||
logger.info("Не удалось преобразовать полученные данные, ожидались объекты другого типа");
|
||||
result = new CommandResult(null, DefaultResponse.TYPE_ERROR);
|
||||
logger.severe("Не удалось преобразовать полученные данные, ожидались объекты другого типа");
|
||||
Response response = DefaultResponse.TYPE_ERROR;
|
||||
result = new CommandResult(response.getMsg(), response);
|
||||
}
|
||||
logger.info("Команда выполнена, отправляем результат клиенту...");
|
||||
logger.info("Команда выполнена, сохраняем результат и отправляем результат клиенту...");
|
||||
Executables.SAVE.getExecutable().execute(new Object[]{ peopleDatabase });
|
||||
send(result, request.getAddress(), request.getPort());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue