sewy/src/main/java/me/bvn13/sewy/CommandClientListener.java

103 lines
3.5 KiB
Java
Raw Normal View History

2022-01-28 23:45:02 +03:00
/*
Copyright 2022 Vyacheslav Boyko
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package me.bvn13.sewy;
import me.bvn13.sewy.command.AbstractCommand;
import org.apache.commons.lang3.SerializationUtils;
2022-02-21 02:07:35 +03:00
import java.io.IOException;
2022-01-28 23:45:02 +03:00
import java.io.Serializable;
import java.net.Socket;
import static java.lang.String.format;
2022-01-29 03:58:07 +03:00
import static me.bvn13.sewy.Sewy.getSeparator;
2022-01-28 23:45:02 +03:00
/**
* Client listener describing protocol-oriented communication
*/
public class CommandClientListener extends AbstractClientListener implements AbstractCommandExecutor {
public CommandClientListener(Socket socket) {
super(socket);
}
/**
* Thread runner
*/
@Override
public void run() {
for (Thread.yield(); !socket.isConnected() && !socket.isClosed(); Thread.yield()) {
}
2022-02-21 02:07:35 +03:00
while (socket.isConnected() && !socket.isClosed()) {
2022-01-29 03:04:35 +03:00
try {
2022-02-21 02:07:35 +03:00
Thread.yield();
byte[] line = readBytes(getSeparator());
if (line == null || line.length == 0) {
continue;
}
final Object command;
try {
command = SerializationUtils.deserialize(line);
} catch (Throwable e) {
log.warn("Deserialization exception occurred!", e);
continue;
}
if (command == null) {
continue;
}
if (!Sewy.getRegisteredDataTypes().contains(command.getClass())) {
log.error("Unexpected command received");
continue;
}
log.debug("Command received: " + command.getClass());
if (!(command instanceof AbstractCommand)) {
log.warn("Incorrect command received: " + command);
continue;
}
final Serializable response = onCommand((AbstractCommand) command);
log.debug(format("Response for %s is: %s", command, response));
writeBytes(SerializationUtils.serialize(response), getSeparator());
} catch (Exception e) {
log.error("Failed to communicate!", e);
2022-01-28 23:45:02 +03:00
}
}
}
/**
* Method to receive the data command-by-command incoming from clients
* You need to override it
*
* @param command serialized command to be checked by using
* <p>{@code instanceof ConcreteCommandClass}</p>
* @return server answer on client command
*/
public AbstractCommand onCommand(AbstractCommand command) {
return null;
}
/**
* Sends command to opposite side
2022-02-21 02:07:35 +03:00
*
2022-01-28 23:45:02 +03:00
* @param command command to be sent
2022-02-21 02:07:35 +03:00
* @param <T> generic type
2022-01-28 23:45:02 +03:00
*/
2022-02-21 02:07:35 +03:00
public <T extends AbstractCommand> void send(T command) throws IOException {
log.debug("Start to send command: {}", command);
2022-01-29 03:58:07 +03:00
writeBytes(SerializationUtils.serialize(command), getSeparator());
2022-01-28 23:45:02 +03:00
}
}