Serverpublic abstract class Server extends Object The main server base class.
This class is responsible for setting up most of the server state
before the actual server subclasses take over. |
Fields Summary |
---|
ServerSocketChannel | ssc | SSLContext | sslContext | private static int | PORT | private static int | BACKLOG | private static boolean | SECURE |
Constructors Summary |
---|
Server(int port, int backlog, boolean secure)
if (secure) {
createSSLContext();
}
ssc = ServerSocketChannel.open();
ssc.socket().setReuseAddress(true);
ssc.socket().bind(new InetSocketAddress(port), backlog);
|
Methods Summary |
---|
private static void | checkArgs(int i, int len)
if ((i + 1) >= len) {
usage();
}
| private void | createSSLContext()
char[] passphrase = "passphrase".toCharArray();
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(new FileInputStream("testkeys"), passphrase);
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, passphrase);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(ks);
sslContext = SSLContext.getInstance("TLS");
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
| private static Server | createServer(java.lang.String[] args)
if (args.length < 1) {
usage();
}
int port = PORT;
int backlog = BACKLOG;
boolean secure = SECURE;
for (int i = 1; i < args.length; i++) {
if (args[i].equals("-port")) {
checkArgs(i, args.length);
port = Integer.valueOf(args[++i]);
} else if (args[i].equals("-backlog")) {
checkArgs(i, args.length);
backlog = Integer.valueOf(args[++i]);
} else if (args[i].equals("-secure")) {
secure = true;
} else {
usage();
}
}
Server server = null;
if (args[0].equals("B1")) {
server = new B1(port, backlog, secure);
} else if (args[0].equals("BN")) {
server = new BN(port, backlog, secure);
} else if (args[0].equals("BP")) {
server = new BP(port, backlog, secure);
} else if (args[0].equals("N1")) {
server = new N1(port, backlog, secure);
} else if (args[0].equals("N2")) {
server = new N2(port, backlog, secure);
}
return server;
| public static void | main(java.lang.String[] args)
Server server = createServer(args);
if (server == null) {
usage();
}
System.out.println("Server started.");
server.runServer();
| abstract void | runServer()
| private static void | usage()
System.out.println(
"Usage: Server <type> [options]\n"
+ " type:\n"
+ " B1 Blocking/Single-threaded Server\n"
+ " BN Blocking/Multi-threaded Server\n"
+ " BP Blocking/Pooled-Thread Server\n"
+ " N1 Nonblocking/Single-threaded Server\n"
+ " N2 Nonblocking/Dual-threaded Server\n"
+ "\n"
+ " options:\n"
+ " -port port port number\n"
+ " default: " + PORT + "\n"
+ " -backlog backlog backlog\n"
+ " default: " + BACKLOG + "\n"
+ " -secure encrypt with SSL/TLS");
System.exit(1);
|
|