SDSU CS 696 Emerging Technologies: Java Distributed Computing
Spring Semester, 1999
Assignment 1 Comments
Previous    Lecture Notes Index    Next    
© 1999, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 05-Apr-99

Contents of Doc 23, Assignment 1 Comments


References

Student papers

Doc 23, Assignment 1 Comments Slide # 2

Names

String s;
Socket ss;

Names should indicate the role the item plays in the program

Data Structure
Role, function
InputRec
EmployeeData
BitFlag
PrinterReady
Queue
buffer


Some Examples of Names, Good and Bad

TrainVelocity
Velt, V, X, Train
CurrentDate
CD, Current, C, X, Date
LinesPerPage
LPP, Lines, L, X


Doc 23, Assignment 1 Comments Slide # 3

Opening Files

Issues:

public class ServiceProvider
   {
   public String translateToSpanish( String english ) 
      {
      FileReader translateDatabase = 
         new FileReader( "engToSpan.txt");
      // etc.
      }
   }


Doc 23, Assignment 1 Comments Slide # 4

TCP based Server

Protocol

All data sent between client and server uses key-value pairs

The characters "=", ";", and "@" have special meaning:

   =   separates key and values
   ;    terminates key-value pairs
   @   terminates message between client & server


White space can surround either "=" or ";"

"=" or ";" can appear in a value, but "@" can not

If a value contains white space, the character ";" or "=" the entire value will be embedded between two single quote characters

Client Commands to Server

command=status;@
command=time;@
command=translate;data=<englishWord>;@

Server Responses to Client

data=<response>;@
error=<errorMessage>;@

Doc 23, Assignment 1 Comments Slide # 5

MiscServicesServer

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import sdsu.util.LabeledData;
import sdsu.util.ProgramProperties;
public class MiscServicesServer extends Thread {
   private int serverPort;
   //Protocol Keys & special char
   static final String COMMAND = "command";
   static final String TIME = "time";
   static final String STATUS = "status";
   static final String TRANSLATE = "translate";
   static final String ERROR = "error";
   static final String DATA = "data";
   static final char END_OF_MESSAGE  = '@';
   public static void main( String[] args ) {
      try {
         ProgramProperties flags = new ProgramProperties( args );
         
         if ( !flags.containsKey( "port" ) ) {
            System.out.println( 
               "Usage: java MiscServicesServer -port portNumber" );
            System.exit( 0 );
         }
         int port = flags.getInt( "port" );
         MiscServicesServer aServer = new MiscServicesServer( port );
         aServer.start();
      } catch (IOException parseError) {
         System.out.println( 
            "Error parsing command line. Exception is:" );
         parseError.printStackTrace();
      }
   }
   public MiscServicesServer( int port) {
      serverPort = port;
   }

Doc 23, Assignment 1 Comments Slide # 6
MiscServicesServer Continued
   public String time( ) {
      Date now = new Date();
      return now.toString();
   }
   public String status( ) {
      String machineStatus;
      try {
         Process uptime = 
            (Runtime.getRuntime()).exec("/usr/ucb/uptime");
         uptime.waitFor();
         
         InputStream processIn = uptime.getInputStream();
         
         StringBuffer statusBuffer = new StringBuffer();
         int nextChar;
         while (  (nextChar = processIn.read() )  != -1 )
            statusBuffer.append( (char) nextChar );
         
         machineStatus = statusBuffer.toString();
      } catch (Exception IOorProcessProblem ) {
         machineStatus = "Status not available now. Try later";
      }
      return machineStatus;
   }

Doc 23, Assignment 1 Comments Slide # 7
MiscServicesServer Continued
   public String translate( String englishWord ) {
      //Left to the reader...
      return " ";
   }
   public void run() {
      try {
         ServerSocket acceptor = new ServerSocket( serverPort );
         while (true) {
            Socket client = acceptor.accept();
            processClientRequest(client.getInputStream(),
                           client.getOutputStream());
            client.close();
         }
      } catch (IOException startup) {
         System.err.println( "Socket error: " + startup);
      }
   }

Doc 23, Assignment 1 Comments Slide # 8
MiscServicesServer Continued
   public void processClientRequest( InputStream in, 
      OutputStream out ) {
      out = new BufferedOutputStream( out );
      in = new BufferedInputStream( in );
      try {
         String clientMessage = readMessage( in );
         LabeledData clientRequest = new LabeledData();
         clientRequest.fromString(clientMessage);
         
         String clientCommand = 
            clientRequest.getData( COMMAND, "none" );
         
         String returnMessage;
         
         if (clientCommand.equals( TIME ) ) {
            returnMessage = time();
            returnMessage = 
               formatReturnMessage(DATA, returnMessage );
         }
         else if (clientCommand.equals( STATUS ) ) {
            returnMessage = status();
            returnMessage = 
               formatReturnMessage(DATA, returnMessage );
         }
         else if (clientCommand.equals( TRANSLATE ) ) {
            String englishWord = clientRequest.getData( DATA, " " );
            returnMessage = translate( englishWord );
         }
         else {
            returnMessage = "Command not valid";
            returnMessage = 
               formatReturnMessage(ERROR, returnMessage );   
         }
         out.write( returnMessage.getBytes() );
         out.write( END_OF_MESSAGE );
         out.flush(); 
      } catch (IOException  error){
         System.err.println( "IOException in processing request: " +
            error.getMessage() );
      }
   }

Doc 23, Assignment 1 Comments Slide # 9
MiscServicesServer Continued
   private String readMessage( InputStream in ) throws IOException {
      StringBuffer inputBuffer = new StringBuffer();
      int inputChar = in.read();
      while ( (inputChar != -1) && 
                  ( inputChar != END_OF_MESSAGE)
               ) {
         inputBuffer.append( (char) inputChar);
         inputChar = in.read();
      }
      return  inputBuffer.toString();
   }
   private String formatReturnMessage( String key, String value) {
      // LabeledData is used to format since it already exists
      LabeledData formater = new LabeledData();
      formater.put( key, value );
      return formater.toString();
   }
}

Doc 23, Assignment 1 Comments Slide # 10

Student Variations

What do we gain and lose from the variations below?

Send serialized data object between client & server

Send serialized object to server that performs desired action

Send method name to server, which uses reflection to perform the method

RMI Server


interface Service extends Remote
   {
   public String perform( String command) throws RemoteException;
   }
verses

interface MiscServices extends Remote
   {
   public String time( ) throws RemoteException;
   public String status( ) throws RemoteException;
   public String translateToSpanish( String englishWord) 
      throws RemoteException;
   }

Copyright ©, All rights reserved.
1999 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 05-Apr-99    Next