blob: 0f7580ca6708922db86f8362660f4f28d010f6cc [file] [log] [blame] [raw]
/*
* Copyright (c) 2010-2011 Graham Edgecombe.
*
* This file is part of Lightstone.
*
* Lightstone is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Lightstone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Lightstone. If not, see <http://www.gnu.org/licenses/>.
*/
package net.lightstone;
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.lightstone.io.NbtChunkIoService;
import net.lightstone.net.MinecraftPipelineFactory;
import net.lightstone.net.Session;
import net.lightstone.net.SessionRegistry;
import net.lightstone.task.PulseTask;
import net.lightstone.task.TaskScheduler;
import net.lightstone.world.TestWorldGenerator;
import net.lightstone.world.World;
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 Lightstone server.
* @author Graham Edgecombe
*/
public final class Server {
/**
* The logger for this class.
*/
private 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(25565));
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 TestWorldGenerator());
/**
* Creates a new server.
*/
public Server() {
logger.info("Starting Lightstone...");
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;
}
}