Added error response.
This commit is contained in:
parent
66224c63d4
commit
4d9973b374
|
@ -1,5 +1,6 @@
|
|||
package nl.andrewl.concord_core.msg;
|
||||
|
||||
import nl.andrewl.concord_core.msg.types.Error;
|
||||
import nl.andrewl.concord_core.msg.types.*;
|
||||
|
||||
import java.io.*;
|
||||
|
@ -22,6 +23,7 @@ public class Serializer {
|
|||
registerType(6, ChannelUsersRequest.class);
|
||||
registerType(7, ChannelUsersResponse.class);
|
||||
registerType(8, ServerMetaData.class);
|
||||
registerType(9, Error.class);
|
||||
}
|
||||
|
||||
private static void registerType(int id, Class<? extends Message> clazz) {
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
package nl.andrewl.concord_core.msg.types;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import nl.andrewl.concord_core.msg.Message;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import static nl.andrewl.concord_core.msg.MessageUtils.*;
|
||||
|
||||
/**
|
||||
* Error message which can be sent between either the server or client to
|
||||
* indicate an unsavory situation.
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Error implements Message {
|
||||
public enum Level {WARNING, ERROR}
|
||||
|
||||
private Level level;
|
||||
private String message;
|
||||
|
||||
public static Error warning(String message) {
|
||||
return new Error(Level.WARNING, message);
|
||||
}
|
||||
|
||||
public static Error error(String message) {
|
||||
return new Error(Level.ERROR, message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getByteCount() {
|
||||
return Integer.BYTES + getByteSize(this.message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutputStream o) throws IOException {
|
||||
writeEnum(this.level, o);
|
||||
writeString(this.message, o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInputStream i) throws IOException {
|
||||
this.level = readEnum(Level.class, i);
|
||||
this.message = readString(i);
|
||||
}
|
||||
}
|
|
@ -12,13 +12,16 @@ public class AddChannelCommand implements ServerCliCommand {
|
|||
public void handle(ConcordServer server, String[] args) throws Exception {
|
||||
if (args.length < 1) {
|
||||
System.err.println("Missing required name argument.");
|
||||
return;
|
||||
}
|
||||
String name = args[0].trim().toLowerCase().replaceAll("\\s+", "-");
|
||||
if (name.isBlank()) {
|
||||
System.err.println("Cannot create channel with blank name.");
|
||||
return;
|
||||
}
|
||||
if (server.getChannelManager().getChannelByName(name).isPresent()) {
|
||||
System.err.println("Channel with that name already exists.");
|
||||
return;
|
||||
}
|
||||
String description = null;
|
||||
if (args.length > 1) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package nl.andrewl.concord_server.event;
|
||||
|
||||
import nl.andrewl.concord_core.msg.types.Chat;
|
||||
import nl.andrewl.concord_core.msg.types.Error;
|
||||
import nl.andrewl.concord_server.ClientThread;
|
||||
import nl.andrewl.concord_server.ConcordServer;
|
||||
import org.dizitart.no2.Document;
|
||||
|
@ -11,6 +12,10 @@ import java.util.Map;
|
|||
public class ChatHandler implements MessageHandler<Chat> {
|
||||
@Override
|
||||
public void handle(Chat msg, ClientThread client, ConcordServer server) throws IOException {
|
||||
if (msg.getMessage().length() > server.getConfig().getMaxMessageLength()) {
|
||||
client.getCurrentChannel().sendMessage(Error.warning("Message is too long."));
|
||||
return;
|
||||
}
|
||||
server.getExecutorService().submit(() -> {
|
||||
var collection = client.getCurrentChannel().getMessageCollection();
|
||||
Document doc = new Document(Map.of(
|
||||
|
|
Loading…
Reference in New Issue