blob: 72264a9a78400bd368b487c3a7b0e769d3b3989e [file] [log] [blame] [raw]
package net.glowstone;
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 net.glowstone.io.NbtChunkIoService;
import net.glowstone.net.MinecraftPipelineFactory;
import net.glowstone.net.Session;
import net.glowstone.net.SessionRegistry;
import net.glowstone.scheduler.PulseTask;
import net.glowstone.scheduler.TaskScheduler;
import net.glowstone.world.ForestWorldGenerator;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
/**
* The core class of the Glowstone server.
* @author Graham Edgecombe
*/
public final class Server {
/**
* The logger for this class.
*/
public static final Logger logger = Logger.getLogger(Server.class.getName());
/**
* Creates a new server on TCP port 25565 and starts listening for
* connections.
* @param args The command-line arguments.
*/
public static void main(String[] args) {
try {
Server server = new Server();
server.bind(new InetSocketAddress(13000));
server.start();
} catch (Throwable t) {
logger.log(Level.SEVERE, "Error during server startup.", t);
}
}
/**
* The {@link ServerBootstrap} used to initialize Netty.
*/
private final ServerBootstrap bootstrap = new ServerBootstrap();
/**
* A group containing all of the channels.
*/
private final ChannelGroup group = new DefaultChannelGroup();
/**
* The network executor service - Netty dispatches events to this thread
* pool.
*/
private final ExecutorService executor = Executors.newCachedThreadPool();
/**
* A list of all the active {@link Session}s.
*/
private final SessionRegistry sessions = new SessionRegistry();
/**
* The task scheduler used by this server.
*/
private final TaskScheduler scheduler = new TaskScheduler();
/**
* The world this server is managing.
*/
private final World world = new World(new NbtChunkIoService(), new ForestWorldGenerator());
/**
* Creates a new server.
*/
public Server() {
logger.info("Starting Glowstone...");
init();
}
/**
* Initializes the channel and pipeline factories.
*/
private void init() {
ChannelFactory factory = new NioServerSocketChannelFactory(executor, executor);
bootstrap.setFactory(factory);
ChannelPipelineFactory pipelineFactory = new MinecraftPipelineFactory(this);
bootstrap.setPipelineFactory(pipelineFactory);
}
/**
* Binds this server to the specified address.
* @param address The addresss.
*/
public void bind(SocketAddress address) {
logger.info("Binding to address: " + address + "...");
group.add(bootstrap.bind(address));
}
/**
* Starts this server.
*/
public void start() {
scheduler.schedule(new PulseTask(this));
logger.info("Ready for connections.");
}
/**
* Gets the channel group.
* @return The {@link ChannelGroup}.
*/
public ChannelGroup getChannelGroup() {
return group;
}
/**
* Gets the session registry.
* @return The {@link SessionRegistry}.
*/
public SessionRegistry getSessionRegistry() {
return sessions;
}
/**
* Gets the task scheduler.
* @return The {@link TaskScheduler}.
*/
public TaskScheduler getScheduler() {
return scheduler;
}
/**
* Gets the world this server manages.
* @return The {@link World} this server manages.
*/
public World getWorld() {
return world;
}
}