Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // server
- public class AsyncHTTPServer {
- public static void main(String args[]) {
- Map<String, String[]> config = null;
- int port;
- String documentRoot;
- int cacheSize;
- long timeout = 3000;
- try {
- config = ServerUtil.readConfiguration(args);
- try {
- // Required Arguments
- port = Integer.parseInt(config.get("Listen")[0]);
- documentRoot = config.get("DocumentRoot")[0];
- cacheSize = Integer.parseInt(config.get("CacheSize")[0]);
- } catch (NullPointerException e) {
- throw new InvalidConfigException();
- }
- // Optional Arguments
- String tmp[] = config.get("IncompleteTimeout");
- timeout = (tmp == null) ? timeout : Integer.parseInt(tmp[0]);
- } catch (InvalidConfigException e) {
- System.err.println("Invalid Configuration or no config file.");
- return;
- }
- try {
- Dispatcher d = new Dispatcher();
- TimeoutThread t = new TimeoutThread(d, timeout);
- Map<String, ByteBuffer> cache = new CacheMap<String, ByteBuffer>(
- cacheSize);
- ServerSocketChannel server = ServerSocketChannel.open();
- server.socket().bind(new InetSocketAddress(port));
- server.configureBlocking(false);
- SocketReadWriteHandlerFactory s = new HTTPReadWriteHandlerFactory(
- documentRoot, "AsyncHTTPServer", cache);
- AcceptHandler a = new HTTPAcceptHandler(server, d, s, t);
- d.registerSelection(server, a, SelectionKey.OP_ACCEPT);
- System.out.println("Listening at: " + port);
- new Thread(d).start();
- t.start();
- } catch (IOException e) {
- System.err.println("Server initialization failed.");
- e.printStackTrace();
- return;
- }
- }
- }
- // dispatcher
- import java.io.IOException;
- import java.nio.channels.ClosedChannelException;
- import java.nio.channels.SelectableChannel;
- import java.nio.channels.SelectionKey;
- import java.nio.channels.Selector;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Queue;
- public class Dispatcher implements Runnable {
- private Selector sel;
- private Queue<DispatcherCommand> commandQueue;
- public Dispatcher() throws IOException {
- try {
- sel = Selector.open();
- } catch (IOException e) {
- System.err.println("Unable to open selector.");
- return;
- }
- commandQueue = new LinkedList<DispatcherCommand>();
- }
- public void registerSelection(SelectableChannel channel,
- ChannelHandler handler, int ops) throws ClosedChannelException {
- SelectionKey key = channel.register(sel, ops);
- key.attach(handler);
- return;
- }
- public SelectionKey getKey(SelectableChannel channel) {
- return channel.keyFor(sel);
- }
- public void addCommand(DispatcherCommand cmd) {
- synchronized (commandQueue) {
- commandQueue.add(cmd);
- }
- }
- public void run() {
- while (true) {
- // Process events off the command queue first
- synchronized (commandQueue) {
- while (!commandQueue.isEmpty()) {
- try {
- commandQueue.remove().run(this);
- } catch (Exception e) {
- System.err.println("Unable to execute command from queue.");
- e.printStackTrace();
- return;
- }
- }
- }
- try {
- sel.select();
- } catch (IOException e) {
- System.err.println("I/O Error in Selector.");
- e.printStackTrace();
- return;
- }
- Iterator<SelectionKey> it = sel.selectedKeys().iterator();
- while (it.hasNext()) {
- SelectionKey key = it.next();
- it.remove();
- ChannelHandler ch = (ChannelHandler) key.attachment();
- try {
- if (key.isAcceptable()) {
- AcceptHandler ah = (AcceptHandler)ch;
- ah.handleAccept(key);
- }
- if (key.isReadable() || key.isWritable()) {
- SocketReadWriteHandler rwh = (SocketReadWriteHandler) ch;
- if (key.isReadable())
- rwh.handleRead(key);
- if (key.isWritable())
- rwh.handleWrite(key);
- }
- } catch (IOException e) {
- ch.handleException(key);
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment