Remove CarbonChat#serverId and add @ServerId injection binding annotation

This commit is contained in:
Jason Penilla committed 2023-08-10 14:11:47 -07:00
1 parent dc37c94fb2
commit ea4d0c9b5a
10 files changed
+67 -40

No files matched your search

@@ -19,7 +19,6 @@
*/
package net.draycia.carbon.api;
import java.util.UUID;
import net.draycia.carbon.api.channels.ChannelRegistry;
import net.draycia.carbon.api.event.CarbonEvent;
import net.draycia.carbon.api.event.CarbonEventHandler;
@@ -41,14 +40,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public interface CarbonChat {
/**
* The server's ID, used for cross-server message handling.
*
* @return the server id
* @since 2.1.0
*/
UUID serverId();
/**
* The {@link CarbonEventHandler event handler}, used for listening to
* and emitting {@link CarbonEvent events}.
@@ -24,7 +24,6 @@ import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.draycia.carbon.api.CarbonChat;
@@ -53,7 +52,6 @@ import org.checkerframework.framework.qual.DefaultQualifier;
@DefaultQualifier(NonNull.class)
public abstract class CarbonChatInternal<C extends CarbonPlayer> implements CarbonChat {
private final UUID serverId;
private final Injector injector;
private final Logger logger;
private final ScheduledExecutorService periodicTasks;
@@ -81,7 +79,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
final CarbonChannelRegistry channelRegistry,
final Provider<MessagingManager> messagingManagerProvider
) {
this.serverId = UUID.randomUUID();
this.injector = injector;
this.logger = logger;
this.periodicTasks = periodicTasks;
@@ -141,11 +138,6 @@ public abstract class CarbonChatInternal<C extends CarbonPlayer> implements Carb
this.commandExecutor.shutdown();
}
@Override
public UUID serverId() {
return this.serverId;
}
public Logger logger() {
return this.logger;
}
@@ -63,6 +63,7 @@ import net.draycia.carbon.common.messages.placeholders.ComponentPlaceholderResol
import net.draycia.carbon.common.messages.placeholders.KeyPlaceholderResolver;
import net.draycia.carbon.common.messages.placeholders.StringPlaceholderResolver;
import net.draycia.carbon.common.messages.placeholders.UUIDPlaceholderResolver;
import net.draycia.carbon.common.messaging.ServerId;
import net.draycia.carbon.common.messaging.packets.PacketFactory;
import net.draycia.carbon.common.users.Backing;
import net.draycia.carbon.common.users.CarbonPlayerCommon;
@@ -141,6 +142,7 @@ public final class CarbonCommonModule extends AbstractModule {
protected void configure() {
this.install(new FactoryModuleBuilder().build(ArgumentFactory.class));
this.install(factoryModule(PacketFactory.class));
this.bind(ServerId.KEY).toInstance(UUID.randomUUID());
this.bind(ChannelRegistry.class).to(CarbonChannelRegistry.class);
this.bind(CarbonEventHandler.class).to(CarbonEventHandlerImpl.class);
this.bind(PlayerSuggestions.class).to(NetworkUsers.class);
@@ -21,12 +21,13 @@ package net.draycia.carbon.common.listeners;
import com.google.inject.Inject;
import com.google.inject.Provider;
import net.draycia.carbon.api.CarbonChat;
import java.util.UUID;
import net.draycia.carbon.api.event.CarbonEventHandler;
import net.draycia.carbon.api.event.events.CarbonChatEvent;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.event.events.CarbonChatEventImpl;
import net.draycia.carbon.common.messaging.MessagingManager;
import net.draycia.carbon.common.messaging.ServerId;
import net.draycia.carbon.common.messaging.packets.ChatMessagePacket;
import net.draycia.carbon.common.users.ConsoleCarbonPlayer;
import net.kyori.adventure.text.Component;
@@ -39,7 +40,7 @@ public class MessagePacketHandler implements Listener {
@Inject
public MessagePacketHandler(
final CarbonEventHandler events,
final CarbonChat carbonChat,
final @ServerId UUID serverId,
final Provider<MessagingManager> messaging
) {
events.subscribe(CarbonChatEvent.class, 100, false, event -> {
@@ -54,7 +55,7 @@ public class MessagePacketHandler implements Listener {
final CarbonPlayer sender = event.sender();
final Component networkMessage = e.renderFor(sender);
packetService.queuePacket(new ChatMessagePacket(carbonChat.serverId(), sender.uuid(),
packetService.queuePacket(new ChatMessagePacket(serverId, sender.uuid(),
event.chatChannel().permission(), event.chatChannel().key(), sender.username(), networkMessage));
});
});
@@ -78,8 +78,8 @@ public class MessagingManager {
private static final byte protocolVersion = 0;
private final CarbonChat carbonChat;
private final Logger logger;
private final UUID serverId;
private final @MonotonicNonNull ScheduledExecutorService scheduledExecutor;
private final @MonotonicNonNull PacketService packetService;
private @MonotonicNonNull MessagingService messagingService;
@@ -88,6 +88,7 @@ public class MessagingManager {
public MessagingManager(
final ConfigFactory configFactory,
final CarbonChat carbonChat,
final @ServerId UUID serverId,
final CarbonServer server,
final Logger logger,
final UserManagerInternal<?> userManager,
@@ -95,7 +96,7 @@ public class MessagingManager {
final WhisperCommand.WhisperHandler whisper,
final PacketFactory packetFactory
) {
this.carbonChat = carbonChat;
this.serverId = serverId;
this.logger = logger;
if (!configFactory.primaryConfig().messagingSettings().enabled()) {
if (!((CarbonChatInternal<?>) carbonChat).isProxy()) {
@@ -131,7 +132,7 @@ public class MessagingManager {
ConcurrentUtil.carbonThreadFactory(logger, "MessagingManager"), logger);
final MessagingHandlerImpl handlerImpl = new MessagingHandlerImpl(this.packetService);
handlerImpl.addHandler(new CarbonServerHandler(server, carbonChat.serverId(), this.packetService, handlerImpl, packetFactory));
handlerImpl.addHandler(new CarbonServerHandler(server, serverId, this.packetService, handlerImpl, packetFactory));
handlerImpl.addHandler(new CarbonChatPacketHandler(carbonChat, this, userManager, networkUsers, whisper));
try {
@@ -144,12 +145,12 @@ public class MessagingManager {
this.packetService.addMessenger(this.messagingService);
this.packetService.queuePacket(new InitializationPacket(carbonChat.serverId(), protocolVersion));
this.packetService.queuePacket(new InitializationPacket(serverId, protocolVersion));
this.packetService.flushQueue();
// Broadcast keepalive packets
this.scheduledExecutor.scheduleAtFixedRate(() -> {
this.packetService.queuePacket(new KeepAlivePacket(carbonChat.serverId()));
this.packetService.queuePacket(new KeepAlivePacket(serverId));
this.packetService.flushQueue();
}, 5, 5, TimeUnit.SECONDS);
@@ -200,7 +201,7 @@ public class MessagingManager {
case RABBITMQ -> {
this.logger.info("Initializing RabbitMQ Messaging services...");
final RabbitMQMessagingService.Builder builder = RabbitMQMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
final RabbitMQMessagingService.Builder builder = RabbitMQMessagingService.builder(packetService, name, channelName, this.serverId, handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port(), messagingSettings.vhost())
.timeout(5000);
@@ -213,7 +214,7 @@ public class MessagingManager {
case NATS -> {
this.logger.info("Initializing NATS Messaging services...");
final NATSMessagingService.Builder builder = NATSMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
final NATSMessagingService.Builder builder = NATSMessagingService.builder(packetService, name, channelName, this.serverId, handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port())
.life(5000);
@@ -226,7 +227,7 @@ public class MessagingManager {
case REDIS -> {
this.logger.info("Initializing Redis Messaging services...");
final RedisMessagingService.Builder builder = RedisMessagingService.builder(packetService, name, channelName, this.carbonChat.serverId(), handlerImpl, 0L, false, packetDir)
final RedisMessagingService.Builder builder = RedisMessagingService.builder(packetService, name, channelName, this.serverId, handlerImpl, 0L, false, packetDir)
.url(messagingSettings.url(), messagingSettings.port());
if (messagingSettings.password() != null && !messagingSettings.password().isBlank()) {
@@ -0,0 +1,40 @@
/*
* CarbonChat
*
* Copyright (c) 2023 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.common.messaging;
import com.google.inject.BindingAnnotation;
import com.google.inject.Key;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.UUID;
/**
* Injection binding annotation for the {@link UUID} identifier of
* the currently running Carbon instance for cross-server packet
* communications.
*/
@BindingAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD})
public @interface ServerId {
Key<UUID> KEY = Key.get(UUID.class, ServerId.class);
}
@@ -23,7 +23,7 @@ import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import io.netty.buffer.ByteBuf;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.common.messaging.ServerId;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@@ -38,12 +38,12 @@ public final class LocalPlayerChangePacket extends CarbonPacket {
@AssistedInject
public LocalPlayerChangePacket(
final CarbonChat carbonChat,
final @ServerId UUID serverId,
final @Assisted UUID playerId,
final @Assisted @Nullable String playerName,
final @Assisted ChangeType changeType
) {
super(carbonChat.serverId());
super(serverId);
if (changeType == ChangeType.ADD && playerName == null) {
throw new IllegalArgumentException("playerName cannot be null for ChangeType.ADD");
}
@@ -24,7 +24,7 @@ import com.google.inject.assistedinject.AssistedInject;
import io.netty.buffer.ByteBuf;
import java.util.Map;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.common.messaging.ServerId;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -36,10 +36,10 @@ public final class LocalPlayersPacket extends CarbonPacket {
@AssistedInject
public LocalPlayersPacket(
final CarbonChat carbonChat,
final @ServerId UUID serverId,
final @Assisted Map<UUID, String> players
) {
super(carbonChat.serverId());
super(serverId);
this.players = players;
}
@@ -23,7 +23,7 @@ import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import io.netty.buffer.ByteBuf;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.common.messaging.ServerId;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;
@@ -34,8 +34,8 @@ public final class SaveCompletedPacket extends CarbonPacket {
private @MonotonicNonNull UUID player;
@AssistedInject
public SaveCompletedPacket(final CarbonChat carbonChat, final @Assisted UUID player) {
super(carbonChat.serverId());
public SaveCompletedPacket(final @ServerId UUID serverId, final @Assisted UUID player) {
super(serverId);
this.player = player;
}
@@ -23,7 +23,7 @@ import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import io.netty.buffer.ByteBuf;
import java.util.UUID;
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.common.messaging.ServerId;
import net.kyori.adventure.text.Component;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -38,12 +38,12 @@ public final class WhisperPacket extends CarbonPacket {
@AssistedInject
public WhisperPacket(
final CarbonChat carbonChat,
final @ServerId UUID serverId,
final @Assisted("from") UUID from,
final @Assisted("to") UUID to,
final @Assisted Component message
) {
super(carbonChat.serverId());
super(serverId);
this.from = from;
this.to = to;
this.message = message;