1 36 37 import java.io.*; 38 import java.net.*; 39 import java.nio.channels.*; 40 import java.security.*; 41 import javax.net.ssl.*; 42 43 53 public abstract class Server { 54 55 ServerSocketChannel ssc; 56 SSLContext sslContext = null; 57 58 static private int PORT = 8000; 59 static private int BACKLOG = 1024; 60 static private boolean SECURE = false; 61 62 Server(int port, int backlog, 63 boolean secure) throws Exception { 64 65 if (secure) { 66 createSSLContext(); 67 } 68 69 ssc = ServerSocketChannel.open(); 70 ssc.socket().setReuseAddress(true); 71 ssc.socket().bind(new InetSocketAddress(port), backlog); 72 } 73 74 79 private void createSSLContext() throws Exception { 80 81 char[] passphrase = "passphrase".toCharArray(); 82 83 KeyStore ks = KeyStore.getInstance("JKS"); 84 ks.load(new FileInputStream("testkeys"), passphrase); 85 86 KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 87 kmf.init(ks, passphrase); 88 89 TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); 90 tmf.init(ks); 91 92 sslContext = SSLContext.getInstance("TLS"); 93 sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); 94 } 95 96 abstract void runServer() throws Exception ; 97 98 static private void usage() { 99 System.out.println( 100 "Usage: Server <type> [options]\n" 101 + " type:\n" 102 + " B1 Blocking/Single-threaded Server\n" 103 + " BN Blocking/Multi-threaded Server\n" 104 + " BP Blocking/Pooled-Thread Server\n" 105 + " N1 Nonblocking/Single-threaded Server\n" 106 + " N2 Nonblocking/Dual-threaded Server\n" 107 + "\n" 108 + " options:\n" 109 + " -port port port number\n" 110 + " default: " + PORT + "\n" 111 + " -backlog backlog backlog\n" 112 + " default: " + BACKLOG + "\n" 113 + " -secure encrypt with SSL/TLS"); 114 System.exit(1); 115 } 116 117 121 static private Server createServer(String args[]) throws Exception { 122 if (args.length < 1) { 123 usage(); 124 } 125 126 int port = PORT; 127 int backlog = BACKLOG; 128 boolean secure = SECURE; 129 130 for (int i = 1; i < args.length; i++) { 131 if (args[i].equals("-port")) { 132 checkArgs(i, args.length); 133 port = Integer.valueOf(args[++i]); 134 } else if (args[i].equals("-backlog")) { 135 checkArgs(i, args.length); 136 backlog = Integer.valueOf(args[++i]); 137 } else if (args[i].equals("-secure")) { 138 secure = true; 139 } else { 140 usage(); 141 } 142 } 143 144 Server server = null; 145 146 if (args[0].equals("B1")) { 147 server = new B1(port, backlog, secure); 148 } else if (args[0].equals("BN")) { 149 server = new BN(port, backlog, secure); 150 } else if (args[0].equals("BP")) { 151 server = new BP(port, backlog, secure); 152 } else if (args[0].equals("N1")) { 153 server = new N1(port, backlog, secure); 154 } else if (args[0].equals("N2")) { 155 server = new N2(port, backlog, secure); 156 } 157 158 return server; 159 } 160 161 static private void checkArgs(int i, int len) { 162 if ((i + 1) >= len) { 163 usage(); 164 } 165 } 166 167 static public void main(String args[]) throws Exception { 168 Server server = createServer(args); 169 170 if (server == null) { 171 usage(); 172 } 173 174 System.out.println("Server started."); 175 server.runServer(); 176 } 177 } 178 | Popular Tags |