Files
2006Scape/2006Scape Server/src/main/java/org/apollo/jagcached/FileServer.java
T
Josh Shippam 1c5b400f00 Merge The File&Game Servers Into One Module (#519)
* Merge The File&Game Servers Into One Module

* Make SettingsLoader A GameConstants ConfigLoader
If A Config File Isn't Used, The Server Will Fall Back To The Defaults Set In GameConstants.java

Config Files Can Be Loaded With The "-c/-config configfilelocation.json"
Added A Default Prefilled ServerConfig.json

* Update ConfigLoader

* Bring Back Independant "Secrets" Loader For External Password Stuff
* Added A Bunch More Vars To The ConfigLoader
* Included A Sample "Server Config"
* Also Updated README.md As Parabot Is No Longer Maintained & We No Longer Have A FileServer Module

* Bundle FileServer with Server (docker)

* Remove /udp and http port

* Update .gitignore

* Move FileServer from `org.apollo.jagcached` → `org/apollo/jagcached`

* Tidy GameConstants & Add More Vars To ConfigLoader

* Organised Up GameConstants A Little To Separate ConfigLoader Vars From The Rest
* Added Some More Variables To Be Loaded Through The ConfigLoader

* Fix A Derp Caused By Laziness

* Add -c/-config arg to README.md

* Enable FileServer By Default

Co-authored-by: Danial <admin@redsparr0w.com>
2021-11-23 00:29:25 +00:00

121 lines
4.0 KiB
Java

package org.apollo.jagcached;
import java.io.File;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apollo.jagcached.dispatch.RequestWorkerPool;
import org.apollo.jagcached.net.FileServerHandler;
import org.apollo.jagcached.net.HttpPipelineFactory;
import org.apollo.jagcached.net.JagGrabPipelineFactory;
import org.apollo.jagcached.net.NetworkConstants;
import org.apollo.jagcached.net.OnDemandPipelineFactory;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;
/**
* The core class of the file server.
* @author Graham
*/
public final class FileServer {
/**
* The logger for this class.
*/
private static final Logger logger = Logger.getLogger(FileServer.class.getName());
/**
* The entry point of the application.
* @param args The command-line arguments.
*/
public static void main(String[] args) {
try {
new FileServer().start();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Error starting server.", t);
}
}
/**
* The executor service.
*/
private final ExecutorService service = Executors.newCachedThreadPool();
/**
* The request worker pool.
*/
private final RequestWorkerPool pool = new RequestWorkerPool();
/**
* The file server event handler.
*/
private final FileServerHandler handler = new FileServerHandler();
/**
* The timer used for idle checking.
*/
private final Timer timer = new HashedWheelTimer();
/**
* Starts the file server.
* @throws Exception if an error occurs.
*/
public void start() throws Exception {
if (!new File(Constants.FILE_SYSTEM_DIR).exists())
{
System.out.println("Working Directory = " + System.getProperty("user.dir"));
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("WARNING: I could not find the data/cache folder. You are LIKELY running this in the wrong directory!");
System.out.println("In IntelliJ, fix it by clicking \"GameEngine\" > Edit Configurations at the top of your screen");
System.out.println("Then changing the \"Working Directory\" to be in \"2006Scape/2006Scape Server\", instead of just \"2006Scape\"");
System.out.println("************************************");
System.out.println("************************************");
System.out.println("************************************");
System.exit(1);
}
logger.info("Starting workers...");
pool.start();
logger.info("Starting services...");
try {
start("HTTP", new HttpPipelineFactory(handler, timer), NetworkConstants.HTTP_PORT);
} catch (Throwable t) {
logger.log(Level.SEVERE, "Failed to start HTTP service.", t);
logger.warning("HTTP will be unavailable. JAGGRAB will be used as a fallback by clients but this isn't reccomended!");
}
start("JAGGRAB", new JagGrabPipelineFactory(handler, timer), NetworkConstants.JAGGRAB_PORT);
start("ondemand", new OnDemandPipelineFactory(handler, timer), NetworkConstants.SERVICE_PORT);
logger.info("Ready for connections.");
}
/**
* Starts the specified service.
* @param name The name of the service.
* @param pipelineFactory The pipeline factory.
* @param port The port.
*/
private void start(String name, ChannelPipelineFactory pipelineFactory, int port) {
SocketAddress address = new InetSocketAddress(port);
logger.info("Binding " + name + " service to " + address + "...");
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.setFactory(new NioServerSocketChannelFactory(service, service));
bootstrap.setPipelineFactory(pipelineFactory);
bootstrap.bind(address);
}
}