Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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 net.dv8tion.jda.api.sharding;
import gnu.trove.set.TIntSet;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.SelfUser;
import net.dv8tion.jda.api.exceptions.InvalidTokenException;
import net.dv8tion.jda.api.requests.*;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.SessionController;
import net.dv8tion.jda.api.utils.cache.ShardCacheView;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.entities.SelfUserImpl;
import net.dv8tion.jda.internal.managers.PresenceImpl;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.IOUtil;
import net.dv8tion.jda.internal.utils.JDALogger;
import net.dv8tion.jda.internal.utils.UnlockHook;
import net.dv8tion.jda.internal.utils.cache.ShardCacheViewImpl;
import net.dv8tion.jda.internal.utils.config.AuthorizationConfig;
import net.dv8tion.jda.internal.utils.config.MetaConfig;
import net.dv8tion.jda.internal.utils.config.SessionConfig;
import net.dv8tion.jda.internal.utils.config.ThreadingConfig;
import net.dv8tion.jda.internal.utils.config.sharding.*;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import org.slf4j.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Queue;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.IntFunction;
/**
* JDA's default {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager} implementation.
* To create new instances use the {@link net.dv8tion.jda.api.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder}.
*
* @since 3.4
* @author Aljoscha Grebe
*/
public class DefaultShardManager implements ShardManager
{
public static final Logger LOG = JDALogger.getLog(ShardManager.class);
public static final ThreadFactory DEFAULT_THREAD_FACTORY = r ->
{
final Thread t = new Thread(r, "DefaultShardManager");
t.setPriority(Thread.NORM_PRIORITY + 1);
return t;
};
/**
* The executor that is used by the ShardManager internally to create new JDA instances.
*/
protected final ScheduledExecutorService executor;
/**
* The queue of shards waiting for creation.
*/
protected final Queue queue = new ConcurrentLinkedQueue<>();
/**
* The {@link ShardCacheView ShardCacheView} that holds all shards.
*/
protected ShardCacheViewImpl shards;
/**
* This can be used to check if the ShardManager is shutting down.
*/
protected final AtomicBoolean shutdown = new AtomicBoolean(false);
/**
* The shutdown hook used by this ShardManager. If this is null the shutdown hook is disabled.
*/
protected final Thread shutdownHook;
/**
* The token of the account associated with this ShardManager.
*/
protected final String token;
/**
* The worker running on the {@link #executor ScheduledExecutorService} that spawns new shards.
*/
protected Future> worker;
/**
* The gateway url for JDA to use. Will be {@code nul} until the first shard is created.
*/
protected String gatewayURL;
/**
* {@link PresenceProviderConfig} containing providers for activity and other presence information.
*/
protected final PresenceProviderConfig presenceConfig;
/**
* {@link EventConfig} containing listeners and possibly a custom event manager.
*/
protected final EventConfig eventConfig;
/**
* {@link ShardingConfig} containing information on shard specific meta information.
*/
protected final ShardingConfig shardingConfig;
/**
* {@link ThreadingProviderConfig} containing a series of {@link ThreadPoolProvider} instances for shard specific configuration.
*/
protected final ThreadingProviderConfig threadingConfig;
/**
* {@link ShardingSessionConfig} containing general configurations for sessions of shards like the http client.
*/
protected final ShardingSessionConfig sessionConfig;
/**
* {@link ShardingMetaConfig} containing details on logging configuration, compression mode and shutdown behavior of the manager.
*/
protected final ShardingMetaConfig metaConfig;
/**
* {@link ChunkingFilter} used to determine whether a guild should be lazy loaded or chunk members by default.
*/
protected final ChunkingFilter chunkingFilter;
protected final IntFunction extends RestConfig> restConfigProvider;
public DefaultShardManager(@Nonnull String token)
{
this(token, null);
}
public DefaultShardManager(@Nonnull String token, @Nullable Collection shardIds)
{
this(token, shardIds, null, null, null, null, null, null, null, null);
}
public DefaultShardManager(
@Nonnull String token, @Nullable Collection shardIds,
@Nullable ShardingConfig shardingConfig, @Nullable EventConfig eventConfig,
@Nullable PresenceProviderConfig presenceConfig, @Nullable ThreadingProviderConfig threadingConfig,
@Nullable ShardingSessionConfig sessionConfig, @Nullable ShardingMetaConfig metaConfig, @Nullable IntFunction extends RestConfig> restConfigProvider,
@Nullable ChunkingFilter chunkingFilter)
{
this.token = token;
this.eventConfig = eventConfig == null ? EventConfig.getDefault() : eventConfig;
this.shardingConfig = shardingConfig == null ? ShardingConfig.getDefault() : shardingConfig;
this.threadingConfig = threadingConfig == null ? ThreadingProviderConfig.getDefault() : threadingConfig;
this.sessionConfig = sessionConfig == null ? ShardingSessionConfig.getDefault() : sessionConfig;
this.presenceConfig = presenceConfig == null ? PresenceProviderConfig.getDefault() : presenceConfig;
this.metaConfig = metaConfig == null ? ShardingMetaConfig.getDefault() : metaConfig;
this.chunkingFilter = chunkingFilter == null ? ChunkingFilter.ALL : chunkingFilter;
this.restConfigProvider = restConfigProvider == null ? (i) -> new RestConfig() : restConfigProvider;
this.executor = createExecutor(this.threadingConfig.getThreadFactory());
this.shutdownHook = this.metaConfig.isUseShutdownHook() ? new Thread(this::shutdown, "JDA Shutdown Hook") : null;
synchronized (queue)
{
if (getShardsTotal() != -1)
{
if (shardIds == null)
{
this.shards = new ShardCacheViewImpl(getShardsTotal());
for (int i = 0; i < getShardsTotal(); i++)
this.queue.add(i);
}
else
{
this.shards = new ShardCacheViewImpl(shardIds.size());
shardIds.stream().distinct().sorted().forEach(this.queue::add);
}
}
}
}
@Nonnull
@Override
public EnumSet getGatewayIntents()
{
return GatewayIntent.getIntents(shardingConfig.getIntents());
}
@Override
public void addEventListener(@Nonnull final Object... listeners)
{
ShardManager.super.addEventListener(listeners);
for (Object o : listeners)
eventConfig.addEventListener(o);
}
@Override
public void removeEventListener(@Nonnull final Object... listeners)
{
ShardManager.super.removeEventListener(listeners);
for (Object o : listeners)
eventConfig.removeEventListener(o);
}
@Override
public void addEventListeners(@Nonnull IntFunction