Search in sources :

Example 6 with InstanceInfo

use of com.almightyalpaca.jetbrains.plugins.discord.data.InstanceInfo in project Intellij-Discord-Integration by Almighty-Alpaca.

the class DiscordIntegrationApplicationComponent method checkRpcConnection.

private void checkRpcConnection(@Nullable PresenceRenderContext renderContext) {
    synchronized (rpcLock) {
        LOG.trace("DiscordIntegrationApplicationComponent#checkRpcConnection()");
        if (this.data == null || this.instanceInfo == null)
            return;
        Map<String, InstanceInfo> instances = this.data.getInstances();
        boolean rpcConnectionExists;
        // noinspection SynchronizationOnLocalVariableOrMethodParameter
        synchronized (instances) {
            rpcConnectionExists = instances.values().stream().anyMatch(InstanceInfo::isHasRpcConnection);
        }
        LOG.trace("DiscordIntegrationApplicationComponent#checkRpcConnection()#instanceInfo.isHasRpcConnection() = {}", instanceInfo.isHasRpcConnection());
        if (instanceInfo.isHasRpcConnection() && (renderContext == null ? renderContext = new PresenceRenderContext(data) : renderContext).isEmpty()) {
            this.data.instanceSetHasRpcConnection(System.currentTimeMillis(), instanceInfo, false);
            RPC.dispose();
        } else // @formatter:off
        if (!rpcConnectionExists && !instanceInfo.isHasRpcConnection() && this.channel != null && Objects.equals(channel.getView().getCoord(), this.channel.getAddress()) && !(renderContext == null ? new PresenceRenderContext(data) : renderContext).isEmpty()) // @formatter:on
        {
            this.data.instanceSetHasRpcConnection(System.currentTimeMillis(), instanceInfo, true);
            new Thread(() -> {
                DiscordEventHandlers handlers = new DiscordEventHandlers();
                handlers.ready = this::rpcReady;
                handlers.errored = this::rpcError;
                handlers.disconnected = this::rpcDisconnected;
                RPC.init(handlers, CLIENT_ID, () -> new PresenceRenderContext(this.data), new PresenceRenderer(), this::presenceUpdated);
            }, "JetbrainsDiscordIntegration-RPC-Starter").start();
        }
    }
}
Also used : PresenceRenderContext(com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext) PresenceRenderer(com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderer) InstanceInfo(com.almightyalpaca.jetbrains.plugins.discord.data.InstanceInfo) DiscordEventHandlers(club.minnced.discord.rpc.DiscordEventHandlers)

Aggregations

InstanceInfo (com.almightyalpaca.jetbrains.plugins.discord.data.InstanceInfo)6 ProjectInfo (com.almightyalpaca.jetbrains.plugins.discord.data.ProjectInfo)3 DiscordIntegrationProjectComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationProjectComponent)2 DiscordEventHandlers (club.minnced.discord.rpc.DiscordEventHandlers)1 DiscordRichPresence (club.minnced.discord.rpc.DiscordRichPresence)1 DiscordIntegrationApplicationComponent (com.almightyalpaca.jetbrains.plugins.discord.components.DiscordIntegrationApplicationComponent)1 FileInfo (com.almightyalpaca.jetbrains.plugins.discord.data.FileInfo)1 ReplicatedData (com.almightyalpaca.jetbrains.plugins.discord.data.ReplicatedData)1 PresenceRenderContext (com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderContext)1 PresenceRenderer (com.almightyalpaca.jetbrains.plugins.discord.presence.PresenceRenderer)1 ProjectSettingsStorage (com.almightyalpaca.jetbrains.plugins.discord.settings.data.storage.ProjectSettingsStorage)1 EditorEventMulticaster (com.intellij.openapi.editor.event.EditorEventMulticaster)1 Project (com.intellij.openapi.project.Project)1 MessageBus (com.intellij.util.messages.MessageBus)1 Locale (java.util.Locale)1 Nullable (org.jetbrains.annotations.Nullable)1 JChannel (org.jgroups.JChannel)1