Concurrent Java Server | slide # 2 |
...Issues | slide # 5 |
A New Hinge | slide # 8 |
Principle of Reusable Object Oriented Design | slide # 13 |
Protection from Sloppy ServerEngines | slide # 14 |
A Java Hinge - forName() | slide # 17 |
...JavaServer | slide # 19 |
...Using JavaServer | slide # 22 |
The Java Programming Language, Arnold and Gosling, Addison-Wesley, 1996
import java.net.*; import java.io.*; import java.util.*; class DateServer implements Runnable { DataInputStream input; PrintStream output; public DateServer( InputStream in, OutputStream out ) { input = new DataInputStream( in ); output = new PrintStream( out ); } public void run() { try { String inputLine = input.readLine().toLowerCase(); if ( inputLine.startsWith("date") ) { Date now = new Date(); output.println(now.toString()); } input.close(); output.close(); } catch ( IOException howToHandleThis ) {// A topic to be covered later} } }
class SimpleConcurrentServer { ServerSocket acceptor; public SimpleConcurrentServer() throws IOException { this( 0 ); System.out.println("On port " + acceptor.getLocalPort()); } public SimpleConcurrentServer( int portNumber) throws IOException { acceptor = new ServerSocket( portNumber ); } public void run() throws IOException { while (true) { Socket client = acceptor.accept(); processClientRequest( client ); } } private void processClientRequest( Socket client ) throws IOException { DateServer handler handler = new DateServer( client.getInputStream(), client.getOutputStream()); Thread serverThread = new Thread( handler ); serverThread.setPriority( 4 ); serverThread.start(); } }
class Test { public static void main( String args[] ) throws IOException { SimpleConcurrentServer test test = new SimpleConcurrentServer(); test.run(); } }
What priority to run different parts of the server?
How to use the server code to write more servers?
What if DateServer does not close streams?
What if to many clients connect to server?
class AirlineConcurrentServer extends SimpleConcurrentServer { public AirlineConcurrentServer() throws IOException { super(); } public SimpleConcurrentServer( int portNumber) throws IOException { super( portNumber ); } private void processClientRequest( Socket client ) throws IOException { AirlineServer handler handler = new AirlineServer( client.getInputStream(), client.getOutputStream()); Thread serverThread = new Thread( handler ); serverThread.setPriority( 4 ); serverThread.start(); } }
class AirlineServer { //blah }
interface ServerEngine extends Runnable { public ServerEngine newInstance( InputStream in, OutputStream out ); } class DateServer implements ServerEngine { DataInputStream input; PrintStream output; public ServerEngine newInstance( InputStream in, OutputStream out ) { return new DateServer( in, out ); } public DateServer() {} public DateServer( InputStream in, OutputStream out ) { input = new DataInputStream(in); output = new PrintStream(out); }
public void run() { try { String inputLine = input.readLine().toLowerCase(); if ( inputLine.startsWith("date") ) { Date now = new Date(); output.println(now.toString()); } input.close(); output.close(); } catch ( IOException howToHandleThis ) { // A topic to be covered later } } }
class HingedConcurrentServer { ServerSocket acceptor; ServerEngine engineFactory; public HingedConcurrentServer( ServerEngine factory) throws IOException { this( 0, factory ); System.out.println("On port " + acceptor.getLocalPort()); } public HingedConcurrentServer( int portNumber, ServerEngine factory ) throws IOException { acceptor = new ServerSocket( portNumber ); engineFactory = factory; } public void run() throws IOException { while (true) { Socket client = acceptor.accept(); processClientRequest( client ); } }
HingedConcurrentServer Continued
private void processClientRequest( Socket client ) throws IOException { InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); ServerEngine handler handler = engineFactory.newInstance( in , out ); Thread serverThread = new Thread( handler ); serverThread.setPriority( 4 ); serverThread.start(); } }
class Test { public static void main( String args[] ) throws IOException { ServerEngine aFactory = new DateServer(); SimpleConcurrentServer test; test = new SimpleConcurrentServer( aFactory ); test.run(); } }
class Test { public static void main( String args[] ) throws IOException { ServerEngine aFactory = new AirlineServer(); SimpleConcurrentServer test; test = new SimpleConcurrentServer( aFactory ); test.run(); } }
Note in Java or C++ we could have used an abstract class to create the
ServerEngineinterface
class ServerThread extends Thread { ServerEngine handlerFactory; Socket client; public ServerThread( ServerEngine handlerFactory, Socket client) { this.handlerFactory = handlerFactory; this.client = client; } public void run() { try { InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); Runnable clientHandler; clientHandler = handlerFactory.newInstance( in, out); clientHandler.run(); in.close(); out.close(); client.close(); } catch ( Exception howToHandleThis ) {// A topic to be covered later } }
}
class SaferConcurrentServer { ServerSocket acceptor; ServerEngine engineFactory; public SaferConcurrentServer( ServerEngine factory) throws IOException { this( 0, factory ); System.out.println("On port " + acceptor.getLocalPort()); } public SaferConcurrentServer( int portNumber, ServerEngine factory ) throws IOException { acceptor = new ServerSocket( portNumber ); engineFactory = factory; } public void run() throws IOException { while (true) { Socket client = acceptor.accept(); Thread serverThread; serverThread = new ServerThread(engineFactory, client); serverThread.setPriority( 4 ); serverThread.start(); } } }
import java.net.*; import java.io.*; import java.util.*; class Test { public static void main( String args[] ) throws IOException { ServerEngine aFactory = new DateServer(); SaferConcurrentServer test; test = new SaferConcurrentServer( aFactory ); test.run(); } }
class Example { public String toString() { return "This is a simple class"; } } class Test { public static void main( String args[] ) throws Exception { Class which = Class.forName( "Example" ); Object whichOne = which.newInstance(); System.out.println( whichOne.toString() ); } }
public static Class forName(String className) throws ClassNotFoundException
class JavaServer { ServerSocket acceptor; ServerEngine engineFactory; public static void main( String[] arguments ) throws Exception { LabeledData parameters = new LabeledData(); parameters.fromCommandLine( arguments ); String serverClass = parameters.getData( "Server" ); ServerEngine factory = createFactory( serverClass); Integer port = new Integer( parameters.getData( "Port", "0") ); JavaServer aServer = new JavaServer( port.intValue(), factory ); if ( parameters.containsKey( "Local" ) ) { aServer.runLocal( ); return; } if ( ! parameters.containsKey( "Port" ) ) System.out.println( "Using port " + aServer.getPort() );
if ( parameters.containsKey( "Iterative" ) ) aServer.runIterative(); else aServer.runConcurrent(); }
private static ServerEngine createFactory( String factoryClass ) throws Exception { Class aFactoryClass = Class.forName( factoryClass ); return (ServerEngine) aFactoryClass.newInstance(); } public JavaServer( ServerEngine factory) throws IOException { this( 0, factory ); } public JavaServer( int portNumber, ServerEngine factory ) throws IOException { acceptor = new ServerSocket( portNumber ); engineFactory = factory; } public int getPort() { return acceptor.getLocalPort(); } public void runLocal( ) { ServerEngine engine = engineFactory.newInstance( System.in, System.out ); engine.run(); }
public void runConcurrent() throws IOException { while (true) { Socket client = acceptor.accept(); Thread serverThread = new ServerThread( engineFactory, client ); serverThread.setPriority( 4 ); serverThread.start(); } } public void runIterative() throws IOException { while (true) { Socket client = acceptor.accept(); Thread serverThread = new ServerThread( engineFactory, client ); serverThread.run(); } } } //JavaServer
2) Compile FooBarServer
3) Run JavaServer