Search in sources :

Example 1 with ServiceId

use of de.dytanic.cloudnet.lib.service.ServiceId in project CloudNet by Dytanic.

the class CommandScreen method onExecuteCommand.

@Override
public void onExecuteCommand(CommandSender sender, String[] args) {
    if (CloudNet.getInstance().getScreenProvider().getMainServiceId() != null && args.length > 1 && args[0].equalsIgnoreCase("write")) {
        ServiceId serviceId = CloudNet.getInstance().getScreenProvider().getMainServiceId();
        StringBuilder stringBuilder = new StringBuilder();
        for (short i = 1; i < args.length; i++) {
            stringBuilder.append(args[i]).append(NetworkUtils.SPACE_STRING);
        }
        String commandLine = stringBuilder.substring(0, stringBuilder.length() - 1);
        Wrapper wrapper = CloudNet.getInstance().getWrappers().get(serviceId.getWrapperId());
        if (wrapper != null) {
            if (wrapper.getServers().containsKey(serviceId.getServerId())) {
                wrapper.writeServerCommand(commandLine, wrapper.getServers().get(serviceId.getServerId()).getServerInfo());
            }
            if (wrapper.getProxys().containsKey(serviceId.getServerId())) {
                wrapper.writeProxyCommand(commandLine, wrapper.getProxys().get(serviceId.getServerId()).getProxyInfo());
            }
        }
        return;
    }
    switch(args.length) {
        case 1:
            if (args[0].equalsIgnoreCase("leave") && CloudNet.getInstance().getScreenProvider().getMainServiceId() != null) {
                ServiceId serviceId = CloudNet.getInstance().getScreenProvider().getMainServiceId();
                CloudNet.getInstance().getScreenProvider().disableScreen(serviceId.getServerId());
                CloudNet.getInstance().getScreenProvider().setMainServiceId(null);
                sender.sendMessage("You left the screen session");
                return;
            }
            break;
        case 2:
            if (args[0].equalsIgnoreCase("-s") || args[0].equalsIgnoreCase("server")) {
                MinecraftServer minecraftServer = CloudNet.getInstance().getServer(args[1]);
                if (minecraftServer != null) {
                    ServiceId serviceId = CloudNet.getInstance().getScreenProvider().getMainServiceId();
                    if (serviceId != null) {
                        CloudNet.getInstance().getScreenProvider().disableScreen(serviceId.getServerId());
                        CloudNet.getInstance().getScreenProvider().setMainServiceId(null);
                    }
                    minecraftServer.getWrapper().enableScreen(minecraftServer.getServerInfo());
                    sender.sendMessage("You joined the screen session of " + minecraftServer.getServerId());
                    CloudNet.getInstance().getScreenProvider().setMainServiceId(minecraftServer.getServiceId());
                }
                return;
            }
            if (args[0].equalsIgnoreCase("-p") || args[0].equalsIgnoreCase("proxy")) {
                ProxyServer minecraftServer = CloudNet.getInstance().getProxy(args[1]);
                if (minecraftServer != null) {
                    ServiceId serviceId = CloudNet.getInstance().getScreenProvider().getMainServiceId();
                    if (serviceId != null) {
                        CloudNet.getInstance().getScreenProvider().disableScreen(serviceId.getServerId());
                        CloudNet.getInstance().getScreenProvider().setMainServiceId(null);
                    }
                    minecraftServer.getWrapper().enableScreen(minecraftServer.getProxyInfo());
                    sender.sendMessage("You joined the screen session of " + minecraftServer.getServerId());
                    CloudNet.getInstance().getScreenProvider().setMainServiceId(minecraftServer.getServiceId());
                }
                return;
            }
            break;
        default:
            sender.sendMessage("screen server (-s) | proxy (-p) <name> | The output of the console of the service is transferred to the console of this instance", "screen leave | The console output closes", "screen write <command> | You write a command directly into the console of the service");
            break;
    }
}
Also used : Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) MinecraftServer(de.dytanic.cloudnetcore.network.components.MinecraftServer) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Example 2 with ServiceId

use of de.dytanic.cloudnet.lib.service.ServiceId in project CloudNet by Dytanic.

the class CloudNet method startGameServerAsync.

public void startGameServerAsync(ServerGroup serverGroup, ServerConfig config, int memory, boolean prioritystop, String url, String[] processParameters, boolean onlineMode, Collection<ServerInstallablePlugin> plugins, String customServerName, Properties serverProperties) {
    Wrapper wrapper = fetchPerformanceWrapper(memory, toWrapperInstances(serverGroup.getWrapper()));
    if (wrapper == null)
        return;
    if (serverGroup.getTemplates().size() == 0)
        return;
    Map<String, Integer> templateMap = new WeakHashMap<>();
    Collection<Integer> collection = CollectionWrapper.getCollection(wrapper.getServers(), new Catcher<Integer, MinecraftServer>() {

        @Override
        public Integer doCatch(MinecraftServer key) {
            return key.getServerInfo().getPort();
        }
    });
    collection.addAll(wrapper.getBinndedPorts());
    CollectionWrapper.iterator(getServers(serverGroup.getName()), new Runnabled<MinecraftServer>() {

        @Override
        public void run(MinecraftServer obj) {
            Template template = obj.getProcessMeta().getTemplate();
            if (!templateMap.containsKey(template.getName()))
                templateMap.put(template.getName(), 1);
            else
                templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
        }
    });
    CollectionWrapper.iterator(wrapper.getWaitingServices().values(), new Runnabled<Quad<Integer, Integer, ServiceId, Template>>() {

        @Override
        public void run(Quad<Integer, Integer, ServiceId, Template> obj) {
            Template template = obj.getFourth();
            if (template != null) {
                if (!templateMap.containsKey(template.getName()))
                    templateMap.put(template.getName(), 1);
                else
                    templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
            }
        }
    });
    for (Template template : serverGroup.getTemplates()) {
        if (!templateMap.containsKey(template.getName()))
            templateMap.put(template.getName(), 1);
        else
            templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
    }
    Map.Entry<String, Integer> entry = null;
    for (Map.Entry<String, Integer> values : templateMap.entrySet()) {
        if (entry == null) {
            entry = values;
        } else {
            if (entry.getValue() >= values.getValue()) {
                entry = values;
            }
        }
    }
    Template template = null;
    for (Template t : serverGroup.getTemplates()) {
        if (entry.getKey().equalsIgnoreCase(t.getName())) {
            template = t;
            break;
        }
    }
    if (template == null)
        return;
    int startport = wrapper.getWrapperInfo().getStartPort();
    startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    while (collection.contains(startport)) {
        startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    }
    ServerProcessMeta serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    wrapper.startGameServerAsync(serverProcessMeta);
}
Also used : LocalCloudWrapper(de.dytanic.cloudnetcore.setup.LocalCloudWrapper) Template(de.dytanic.cloudnet.lib.server.template.Template) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 3 with ServiceId

use of de.dytanic.cloudnet.lib.service.ServiceId in project CloudNet by Dytanic.

the class CloudNet method startGameServer.

public void startGameServer(ServerGroup serverGroup, ServerConfig config, int memory, boolean prioritystop, String url, String[] processParameters, boolean onlineMode, Collection<ServerInstallablePlugin> plugins, String customServerName, Properties serverProperties) {
    if (serverGroup.getMaxOnlineServers() != -1 && serverGroup.getMaxOnlineServers() != 0 && CloudNet.getInstance().getServersAndWaitings(serverGroup.getName()).size() >= serverGroup.getMaxOnlineServers())
        return;
    Wrapper wrapper = fetchPerformanceWrapper(memory, toWrapperInstances(serverGroup.getWrapper()));
    if (wrapper == null)
        return;
    if (serverGroup.getTemplates().size() == 0)
        return;
    Map<String, Integer> templateMap = new WeakHashMap<>();
    Collection<Integer> collection = CollectionWrapper.getCollection(wrapper.getServers(), new Catcher<Integer, MinecraftServer>() {

        @Override
        public Integer doCatch(MinecraftServer key) {
            return key.getServerInfo().getPort();
        }
    });
    collection.addAll(wrapper.getBinndedPorts());
    CollectionWrapper.iterator(getServers(serverGroup.getName()), new Runnabled<MinecraftServer>() {

        @Override
        public void run(MinecraftServer obj) {
            Template template = obj.getProcessMeta().getTemplate();
            if (!templateMap.containsKey(template.getName()))
                templateMap.put(template.getName(), 1);
            else
                templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
        }
    });
    CollectionWrapper.iterator(wrapper.getWaitingServices().values(), new Runnabled<Quad<Integer, Integer, ServiceId, Template>>() {

        @Override
        public void run(Quad<Integer, Integer, ServiceId, Template> obj) {
            Template template = obj.getFourth();
            if (template != null) {
                if (!templateMap.containsKey(template.getName()))
                    templateMap.put(template.getName(), 1);
                else
                    templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
            }
        }
    });
    for (Template template : serverGroup.getTemplates()) {
        if (!templateMap.containsKey(template.getName()))
            templateMap.put(template.getName(), 1);
        else
            templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
    }
    Map.Entry<String, Integer> entry = null;
    for (Map.Entry<String, Integer> values : templateMap.entrySet()) {
        if (entry == null)
            entry = values;
        else {
            if (entry.getValue() >= values.getValue()) {
                entry = values;
            }
        }
    }
    Template template = null;
    for (Template t : serverGroup.getTemplates()) if (entry.getKey().equalsIgnoreCase(t.getName())) {
        template = t;
        break;
    }
    if (template == null)
        return;
    int startport = wrapper.getWrapperInfo().getStartPort();
    startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    while (collection.contains(startport)) {
        startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    }
    ServerProcessMeta serverProcessMeta;
    if (customServerName != null)
        serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper, customServerName), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    else
        serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    wrapper.startGameServer(serverProcessMeta);
}
Also used : LocalCloudWrapper(de.dytanic.cloudnetcore.setup.LocalCloudWrapper) Template(de.dytanic.cloudnet.lib.server.template.Template) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 4 with ServiceId

use of de.dytanic.cloudnet.lib.service.ServiceId in project CloudNet by Dytanic.

the class CloudNet method startGameServer.

public void startGameServer(Wrapper wrapper, ServerGroup serverGroup, ServerConfig config, int memory, boolean prioritystop, String url, String[] processParameters, boolean onlineMode, Collection<ServerInstallablePlugin> plugins, String customServerName, Properties serverProperties) {
    if (serverGroup.getMaxOnlineServers() != -1 && serverGroup.getMaxOnlineServers() != 0 && CloudNet.getInstance().getServersAndWaitings(serverGroup.getName()).size() >= serverGroup.getMaxOnlineServers())
        return;
    if (wrapper == null)
        return;
    if (serverGroup.getTemplates().size() == 0)
        return;
    Map<String, Integer> templateMap = new WeakHashMap<>();
    Collection<Integer> collection = CollectionWrapper.getCollection(wrapper.getServers(), new Catcher<Integer, MinecraftServer>() {

        @Override
        public Integer doCatch(MinecraftServer key) {
            return key.getServerInfo().getPort();
        }
    });
    collection.addAll(wrapper.getBinndedPorts());
    CollectionWrapper.iterator(getServers(serverGroup.getName()), new Runnabled<MinecraftServer>() {

        @Override
        public void run(MinecraftServer obj) {
            Template template = obj.getProcessMeta().getTemplate();
            if (!templateMap.containsKey(template.getName()))
                templateMap.put(template.getName(), 1);
            else
                templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
        }
    });
    CollectionWrapper.iterator(wrapper.getWaitingServices().values(), new Runnabled<Quad<Integer, Integer, ServiceId, Template>>() {

        @Override
        public void run(Quad<Integer, Integer, ServiceId, Template> obj) {
            Template template = obj.getFourth();
            if (template != null) {
                if (!templateMap.containsKey(template.getName()))
                    templateMap.put(template.getName(), 1);
                else
                    templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
            }
        }
    });
    for (Template template : serverGroup.getTemplates()) {
        if (!templateMap.containsKey(template.getName()))
            templateMap.put(template.getName(), 1);
        else
            templateMap.put(template.getName(), templateMap.get(template.getName()) + 1);
    }
    Map.Entry<String, Integer> entry = null;
    for (Map.Entry<String, Integer> values : templateMap.entrySet()) {
        if (entry == null) {
            entry = values;
        } else {
            if (entry.getValue() >= values.getValue()) {
                entry = values;
            }
        }
    }
    Template template = null;
    for (Template t : serverGroup.getTemplates()) {
        if (entry.getKey().equalsIgnoreCase(t.getName())) {
            template = t;
            break;
        }
    }
    if (template == null)
        return;
    int startport = wrapper.getWrapperInfo().getStartPort();
    startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    while (collection.contains(startport)) {
        startport = (startport + NetworkUtils.RANDOM.nextInt(20) + 1);
    }
    ServerProcessMeta serverProcessMeta;
    if (customServerName != null)
        serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper, customServerName), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    else
        serverProcessMeta = new ServerProcessMeta(newServiceId(serverGroup, wrapper), memory, prioritystop, url, processParameters, onlineMode, plugins, config, customServerName, startport, serverProperties, template);
    wrapper.startGameServer(serverProcessMeta);
}
Also used : Template(de.dytanic.cloudnet.lib.server.template.Template) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 5 with ServiceId

use of de.dytanic.cloudnet.lib.service.ServiceId in project CloudNet by Dytanic.

the class PacketInAuthHandler method handleAuth.

@Override
public void handleAuth(Auth auth, AuthType authType, Document authData, PacketSender packetSender) {
    if (!(packetSender instanceof CloudNetClientAuth))
        return;
    CloudNetClientAuth client = (CloudNetClientAuth) packetSender;
    switch(authType) {
        case CLOUD_NET:
            {
                String key = authData.getString("key");
                String id = authData.getString("id");
                if (CloudNet.getInstance().getWrappers().containsKey(id)) {
                    Wrapper cn = CloudNet.getInstance().getWrappers().get(id);
                    String wrapperKey = CloudNet.getInstance().getConfig().getWrapperKey();
                    if (wrapperKey != null && cn.getChannel() == null && wrapperKey.equals(key)) {
                        Channel channel = client.getChannel();
                        channel.pipeline().remove("client");
                        client.getChannel().writeAndFlush(new PacketOutAuthResult(new AuthLoginResult(true))).syncUninterruptibly();
                        channel.pipeline().addLast(new CloudNetClient(cn, channel));
                        return;
                    } else {
                        client.getChannel().writeAndFlush(new PacketOutAuthResult(new AuthLoginResult(false))).syncUninterruptibly();
                        CloudNet.getLogger().info("Authentication failed [" + (wrapperKey != null ? "Invalid WrapperKey or Wrapper is already connected!" : "WrapperKey not found, please copy a wrapper key to this instance") + "]");
                    }
                } else {
                    client.getChannel().writeAndFlush(new PacketOutAuthResult(new AuthLoginResult(false))).syncUninterruptibly();
                }
            }
            return;
        case GAMESERVER_OR_BUNGEE:
            {
                ServiceId serviceId = authData.getObject("serviceId", ServiceId.class);
                if (CloudNet.getInstance().getWrappers().containsKey(serviceId.getWrapperId())) {
                    Wrapper wrapper = CloudNet.getInstance().getWrappers().get(serviceId.getWrapperId());
                    if (wrapper.getServers().containsKey(serviceId.getServerId())) {
                        MinecraftServer minecraftServer = wrapper.getServers().get(serviceId.getServerId());
                        if (minecraftServer.getChannel() == null && minecraftServer.getServerInfo().getServiceId().getUniqueId().equals(serviceId.getUniqueId())) {
                            Channel channel = client.getChannel();
                            channel.pipeline().remove("client");
                            channel.pipeline().addLast(new CloudNetClient(minecraftServer, channel));
                            return;
                        }
                    } else if (wrapper.getCloudServers().containsKey(serviceId.getServerId())) {
                        CloudServer minecraftServer = wrapper.getCloudServers().get(serviceId.getServerId());
                        if (minecraftServer.getChannel() == null && minecraftServer.getServerInfo().getServiceId().getUniqueId().equals(serviceId.getUniqueId())) {
                            Channel channel = client.getChannel();
                            channel.pipeline().remove("client");
                            channel.pipeline().addLast(new CloudNetClient(minecraftServer, channel));
                            return;
                        }
                    } else if (wrapper.getProxys().containsKey(serviceId.getServerId())) {
                        ProxyServer minecraftServer = wrapper.getProxys().get(serviceId.getServerId());
                        if (minecraftServer.getChannel() == null && minecraftServer.getProxyInfo().getServiceId().getUniqueId().equals(serviceId.getUniqueId())) {
                            Channel channel = client.getChannel();
                            channel.pipeline().remove("client");
                            channel.pipeline().addLast(new CloudNetClient(minecraftServer, channel));
                            return;
                        }
                    } else {
                        client.getChannel().close().syncUninterruptibly();
                    }
                } else {
                    client.getChannel().close().syncUninterruptibly();
                }
            }
            return;
        default:
            return;
    }
}
Also used : AuthLoginResult(de.dytanic.cloudnet.lib.network.auth.AuthLoginResult) Wrapper(de.dytanic.cloudnetcore.network.components.Wrapper) PacketOutAuthResult(de.dytanic.cloudnet.lib.network.auth.packetio.PacketOutAuthResult) Channel(io.netty.channel.Channel) CloudNetClientAuth(de.dytanic.cloudnetcore.network.CloudNetClientAuth) CloudNetClient(de.dytanic.cloudnetcore.network.CloudNetClient) CloudServer(de.dytanic.cloudnetcore.network.components.CloudServer) ServiceId(de.dytanic.cloudnet.lib.service.ServiceId) MinecraftServer(de.dytanic.cloudnetcore.network.components.MinecraftServer) ProxyServer(de.dytanic.cloudnetcore.network.components.ProxyServer)

Aggregations

ServiceId (de.dytanic.cloudnet.lib.service.ServiceId)9 Template (de.dytanic.cloudnet.lib.server.template.Template)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Wrapper (de.dytanic.cloudnetcore.network.components.Wrapper)3 TypeToken (com.google.gson.reflect.TypeToken)2 MinecraftServer (de.dytanic.cloudnetcore.network.components.MinecraftServer)2 ProxyServer (de.dytanic.cloudnetcore.network.components.ProxyServer)2 LocalCloudWrapper (de.dytanic.cloudnetcore.setup.LocalCloudWrapper)2 AuthLoginResult (de.dytanic.cloudnet.lib.network.auth.AuthLoginResult)1 PacketOutAuthResult (de.dytanic.cloudnet.lib.network.auth.packetio.PacketOutAuthResult)1 Quad (de.dytanic.cloudnet.lib.utility.Quad)1 CloudNetClient (de.dytanic.cloudnetcore.network.CloudNetClient)1 CloudNetClientAuth (de.dytanic.cloudnetcore.network.CloudNetClientAuth)1 CloudServer (de.dytanic.cloudnetcore.network.components.CloudServer)1 Channel (io.netty.channel.Channel)1