/*******************************************************************************
* HelloNzb -- The Binary Usenet Tool
* Copyright (C) 2010-2013 Matthias F. Brandstetter
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package me.mabra.hellonzb.httpserver.nioengine;
import me.mabra.hellonzb.util.MyLogger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.ChannelGroupFuture;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
public class HttpServer
{
private final HttpGetHandler getHandler;
private final HttpJythonHandler jythonHandler;
private final HttpNzbFileReceiver fileReceiver;
private final String homeUrl;
private final int port;
private MyLogger logger;
private NioServerSocketChannelFactory channelFactory;
protected static final ChannelGroup allChannels = new DefaultChannelGroup("http-server");
protected static final Map<String, String> authClients = new HashMap<String, String>(); // Ccokie ID -> Client IP
public HttpServer(MyLogger logger, String home, int port)
{
this.jythonHandler = new HttpJythonHandler();
this.getHandler = new HttpGetHandler(jythonHandler);
this.fileReceiver = new HttpNzbFileReceiver();
this.logger = logger;
this.homeUrl = home;
this.port = port;
}
public void run()
{
// Configure the server.
channelFactory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
// Enable TCP_NODELAY to handle pipelined requests without latency.
bootstrap.setOption("child.tcpNoDelay", true);
// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new HttpServerPipelineFactory(getHandler, fileReceiver, homeUrl));
// Bind and start to accept incoming connections.
Channel channel = bootstrap.bind(new InetSocketAddress(port));
allChannels.add(channel);
logger.msg("HTTP server initialised.", MyLogger.SEV_INFO);
}
public void shutdown()
{
ChannelGroupFuture future = allChannels.close();
future.awaitUninterruptibly();
channelFactory.releaseExternalResources();
logger.msg("HTTP server shutdown complete.", MyLogger.SEV_INFO);
}
public HttpGetHandler getHttpHandler()
{
return getHandler;
}
public HttpJythonHandler getJythonHandler()
{
return jythonHandler;
}
public HttpNzbFileReceiver getFileReceiver()
{
return fileReceiver;
}
}