Slight housekeeping in NetworkExtensionClient

This commit is contained in:
dorving 2022-07-11 22:09:51 +02:00
parent 5f08eb599b
commit 047d948086
2 changed files with 45 additions and 28 deletions

View File

@ -15,58 +15,75 @@ import org.slf4j.LoggerFactory;
import java.util.Arrays; import java.util.Arrays;
/**
* A client for managing remote extensions.
*
* @author Dorving
*/
public final class NetworkExtensionClient extends GEarthExtension { public final class NetworkExtensionClient extends GEarthExtension {
private final static Logger LOGGER = LoggerFactory.getLogger(NetworkExtensionClient.class); private final static Logger LOGGER = LoggerFactory.getLogger(NetworkExtensionClient.class);
private final Channel channel;
private final String title; private final String title;
private final String author; private final String author;
private final String version; private final String version;
private final String description; private final String description;
private final String fileName;
private final String cookie;
private final boolean fireEventButtonVisible; private final boolean fireEventButtonVisible;
private final boolean leaveButtonVisible; private final boolean leaveButtonVisible;
private final boolean deleteButtonVisible; private final boolean deleteButtonVisible;
private final boolean isInstalledExtension; private final boolean isInstalledExtension;
private final String fileName;
private final String cookie;
private final Channel channel;
public NetworkExtensionClient(Incoming.ExtensionInfo msg, Channel channel) { /**
title = msg.getTitle(); * Create a new {@link NetworkExtensionClient} instance.
author = msg.getAuthor(); *
version = msg.getVersion(); * @param channel the channel through which to communicate with the remote extension.
description = msg.getDescription(); * @param info the {@link Incoming.ExtensionInfo} detailing the extension.
fireEventButtonVisible = msg.isOnClickUsed(); */
leaveButtonVisible = msg.isCanLeave(); public NetworkExtensionClient(Channel channel, Incoming.ExtensionInfo info) {
deleteButtonVisible = msg.isCanDelete();
isInstalledExtension = msg.getFile() != null;
fileName = msg.getFile();
cookie = msg.getCookie();
this.channel = channel; this.channel = channel;
title = info.getTitle();
author = info.getAuthor();
version = info.getVersion();
description = info.getDescription();
fireEventButtonVisible = info.isOnClickUsed();
leaveButtonVisible = info.isCanLeave();
deleteButtonVisible = info.isCanDelete();
isInstalledExtension = info.getFile() != null;
fileName = info.getFile();
cookie = info.getCookie();
} }
public void handleIncomingMessage(Incoming msg) { /**
* Handles {@link Incoming incoming messages}.
*
* @param incoming the {@link Incoming message} to be handled.
*/
public void handleIncomingMessage(Incoming incoming) {
try { try {
if (msg instanceof Incoming.RequestFlags) if (incoming instanceof Incoming.RequestFlags)
requestFlags(); requestFlags();
else if (msg instanceof Incoming.SendMessage) { else if (incoming instanceof Incoming.SendMessage) {
final Incoming.SendMessage message = ((Incoming.SendMessage) msg); final Incoming.SendMessage message = ((Incoming.SendMessage) incoming);
final HPacket packet = message.getPacket(); final HPacket packet = message.getPacket();
if (!packet.isCorrupted()) if (!packet.isCorrupted())
sendMessage(message.getDirection(), packet); sendMessage(message.getDirection(), packet);
} else if (msg instanceof Incoming.ManipulatedPacket) { } else if (incoming instanceof Incoming.ManipulatedPacket) {
sendManipulatedPacket(((Incoming.ManipulatedPacket) msg).gethMessage()); sendManipulatedPacket(((Incoming.ManipulatedPacket) incoming).gethMessage());
} else if (msg instanceof Incoming.ExtensionConsoleLog) { } else if (incoming instanceof Incoming.ExtensionConsoleLog) {
log(((Incoming.ExtensionConsoleLog) msg).getContents()); log(((Incoming.ExtensionConsoleLog) incoming).getContents());
} else if (msg instanceof Incoming.PacketToStringRequest) { } else if (incoming instanceof Incoming.PacketToStringRequest) {
final HPacket hPacket = new HPacket(new byte[0]); final HPacket hPacket = new HPacket(new byte[0]);
hPacket.constructFromString(((Incoming.PacketToStringRequest) msg).getString()); hPacket.constructFromString(((Incoming.PacketToStringRequest) incoming).getString());
packetToStringRequest(hPacket); packetToStringRequest(hPacket);
} else if (msg instanceof Incoming.StringToPacketRequest) { } else if (incoming instanceof Incoming.StringToPacketRequest) {
stringToPacketRequest(((Incoming.StringToPacketRequest) msg).getString()); stringToPacketRequest(((Incoming.StringToPacketRequest) incoming).getString());
} }
} catch (Exception e){ } catch (Exception e){
LOGGER.error("Failed to handle incoming message {} (channel={})", msg, channel, e); LOGGER.error("Failed to handle incoming message {} (channel={})", incoming, channel, e);
} }
} }

View File

@ -230,7 +230,7 @@ public final class NetworkExtensionServer implements ExtensionProducer {
if (msg instanceof Incoming.ExtensionInfo) { if (msg instanceof Incoming.ExtensionInfo) {
if (client != null) if (client != null)
LOGGER.warn("Overriding pre-existing CLIENT for channel (client={}, channel={})", client, channel); LOGGER.warn("Overriding pre-existing CLIENT for channel (client={}, channel={})", client, channel);
client = new NetworkExtensionClient((Incoming.ExtensionInfo) msg, channel); client = new NetworkExtensionClient(channel, (Incoming.ExtensionInfo) msg);
if (NetworkExtensionAuthenticator.evaluate(client)) { if (NetworkExtensionAuthenticator.evaluate(client)) {
LOGGER.info("Successfully authenticated client {}", client); LOGGER.info("Successfully authenticated client {}", client);
clientAttribute.set(client); clientAttribute.set(client);