FileDocCategorySizeDatePackage
ServerNetworkWrapper.javaAPI DocExample2057Thu Nov 08 00:23:44 GMT 2001com.ora.rmibook.chapter3

ServerNetworkWrapper

public class ServerNetworkWrapper extends NetworkBaseClass

Fields Summary
private Printer
_printer
private int
_port
private ServerSocket
_serverSocket
Constructors Summary
public ServerNetworkWrapper(Printer printer)

        this (printer, DEFAULT_SERVER_PORT, DEFAULT_SERVER_BACKLOG);
    
public ServerNetworkWrapper(Printer printer, int port, int backlog)

        _serverSocket = new ServerSocket(port, backlog);
        _printer = printer;
    
Methods Summary
public voidaccept()

        while (true) {
            Socket clientSocket = null;

            try {
                clientSocket = _serverSocket.accept();	// blocking call
                processPrintRequest(clientSocket);
            } catch (IOException e) {
                e.printStackTrace();
            }
            closeSocket(clientSocket);
        }
    
private voidprocessPrintRequest(java.net.Socket clientSocket)

        InputStream clientRequestStream;
        OutputStream clientResponseStream;
        DataOutputStream dataOutputStream;
        DocumentDescription documentToPrint;

        try {
            clientRequestStream = clientSocket.getInputStream();
            clientResponseStream = clientSocket.getOutputStream();
            dataOutputStream = new DataOutputStream(clientResponseStream);
            documentToPrint = new DocumentDescription(clientRequestStream);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        try {
            try {
                _printer.printDocument(documentToPrint);
                dataOutputStream.writeBoolean(true);
            } catch (PrinterException printerError) {
                dataOutputStream.writeBoolean(false);
                printerError.writeToStream(dataOutputStream);
            }
        } catch (IOException ee) {
            ee.printStackTrace();
        }