com.msi.network.server
Class RequestHandler

java.lang.Object
  extended bycom.msi.network.server.RequestHandler
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
CommandProcessor

public class RequestHandler
extends java.lang.Object
implements java.lang.Runnable

A request handler provides a single method for servicing a request and providing a response. Since some servers may be multi-threaded (hence extending runnable) care must be taken to assure that the class implementing this interface is thread safe. A very simple echo handler is shown below.

 public class echoRequestHandler implements RequestHandler {
   private Connection conn = null;
   public echoRequestHandler() { }
 
   public void setConnection(Connection c) {
     this.conn = c;
   }
 
   public void run() {
     processRequest();
     conn.close();
   }
 
   public void processRequest() {
     if (conn == null) return;
     String rqstring;
     try {
       rqstring = conn.getLine();
      } catch (IOException e) {
       System.err.println("IOException reading from client");
       return;
     }
     if (rqstring.equals(".")) {
       return;
     }
     conn.putLine(rqstring + "\n");
     String clientIn = "";
     do {
       try {
         clientIn = conn.getLine();
       } catch (IOException e) {
         System.err.println("IOException reading from client");
         return;
       }
       conn.putLine(clientIn + "\n");
     } while (!clientIn.equals("."));
   }

  public String helpString() {
    return "echo all input until receive a line with only a period '.'";
  }
 }

Version:
1.0
Author:
Ken Thompson

Field Summary
protected  Connection conn
          conn - connection this RequestHandler uses for client communication.
 
Constructor Summary
RequestHandler()
           
 
Method Summary
 void processRequest()
          This method is the entry point for the server to request an operation be performed by the application on behalf of the client.
 void run()
          Main processing loop for a command session.
 void setConnection(Connection c)
          Set up the connection for this client/server session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

conn

protected Connection conn
conn - connection this RequestHandler uses for client communication.

Constructor Detail

RequestHandler

public RequestHandler()
Method Detail

setConnection

public void setConnection(Connection c)
Set up the connection for this client/server session.

Parameters:
c - connection for this session.

processRequest

public void processRequest()
                    throws RHException
This method is the entry point for the server to request an operation be performed by the application on behalf of the client.

Throws:
RHException - if an error occurs

run

public void run()
Main processing loop for a command session.

Specified by:
run in interface java.lang.Runnable


Copyright © 2001-2002 MileStone Solutions, Inc. All Rights Reserved.