001 /**
002 * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003 *
004 * All rights reserved. Licensed under the OSI BSD License.
005 *
006 * http://www.opensource.org/licenses/bsd-license.php
007 */
008 package com.barchart.udt;
009
010 import java.io.IOException;
011 import java.io.InputStream;
012 import java.net.InetSocketAddress;
013 import java.net.Socket;
014 import java.util.concurrent.Callable;
015 import java.util.concurrent.Executors;
016
017 import org.slf4j.Logger;
018 import org.slf4j.LoggerFactory;
019
020 import com.barchart.udt.net.NetServerSocketUDT;
021
022 public class AppServer {
023
024 /**
025 * @param args
026 * @throws IOException
027 */
028
029 static Logger log = LoggerFactory.getLogger(AppServer.class);
030
031 public static void main(final String[] args) throws IOException {
032
033 int port = 9000;
034
035 if (args.length > 1) {
036 System.out.println("usage: appserver [server_port]");
037 return;
038 }
039
040 if (args.length == 1) {
041 port = Integer.parseInt(args[0]);
042 }
043
044 final NetServerSocketUDT acceptorSocket = new NetServerSocketUDT();
045 acceptorSocket.bind(new InetSocketAddress("0.0.0.0", port), 256);
046
047 System.out.printf("server is ready at port: %d\n", port);
048
049 while (true) {
050
051 final Socket clientSocket = acceptorSocket.accept();
052
053 // Start the read ahead background task
054 Executors.newSingleThreadExecutor().submit(new Callable<Boolean>() {
055 @Override
056 public Boolean call() {
057 return clientTask(clientSocket);
058 }
059 });
060 }
061 }
062
063 public static boolean clientTask(final Socket clientSocket) {
064
065 final byte[] data = new byte[10000];
066
067 try {
068
069 final InputStream is = clientSocket.getInputStream();
070
071 while (true) {
072
073 int remain = data.length;
074
075 while (remain > 0) {
076 final int ret = is.read(data, data.length - remain, remain);
077 remain -= ret;
078 }
079 }
080
081 } catch (final IOException ioe) {
082 ioe.printStackTrace();
083 return false;
084 }
085 }
086 }