/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package handling.farm;
import constants.ServerConfig;
import handling.MapleServerHandler;
import handling.channel.PlayerStorage;
import handling.mina.MapleCodecFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.SimpleByteBufferAllocator;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
/**
*
* @author Itzik
*/
public class FarmServer {
private static String ip;
private static InetSocketAddress InetSocketadd;
private static final int PORT = 8611;
private static IoAcceptor acceptor;
private static PlayerStorage players;
private static boolean finishedShutdown = false;
public static void run_startup_configurations() {
ip = ServerConfig.interface_ + ":" + PORT;
ByteBuffer.setUseDirectBuffers(false);
ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
acceptor = new SocketAcceptor();
SocketAcceptorConfig cfg = new SocketAcceptorConfig();
cfg.getSessionConfig().setTcpNoDelay(true);
cfg.setDisconnectOnUnbind(true);
cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
players = new PlayerStorage(-30);
try {
InetSocketadd = new InetSocketAddress(PORT);
acceptor.bind(InetSocketadd, new MapleServerHandler(), cfg);
System.out.println("Farm Server is listening on port" + PORT + ".");
} catch (IOException e) {
System.err.println("Binding to port " + PORT + " failed");
throw new RuntimeException("Binding failed.", e);
}
}
public static String getIP() {
return ip;
}
public static PlayerStorage getPlayerStorage() {
return players;
}
public static void shutdown() {
if (finishedShutdown) {
return;
}
System.out.println("Saving all connected clients (Farm)...");
players.disconnectAll();
System.out.println("Shutting down Farm...");
finishedShutdown = true;
}
public static boolean isShutdown() {
return finishedShutdown;
}
}