SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 33 End Game

To Lecture Notes Index
San Diego State University -- This page last updated May 13, 1997

Contents of Doc 33 End Game


End Game slide # 1
...Scaling C/S Up slide # 1
...Logging Object slide # 5
...Homework Issues slide # 6
...Rust Never Sleeps slide # 7

Doc 33 End Game Slide # 1

End Game

Scaling C/S Up


What happens when:

100 clients use same server simultaneously?
1,000 clients use same server simultaneously?
10,000 clients use same server simultaneously?

Solution One - Use a Thread Pool

Creating many threads is also expensive

So reuse the threads

ServerThread with Thread Pool

import java.net.*;
import java.io.*;
import java.util.Stack;

class ServerThread extends Thread
     {
     private static int MAX_THREADS;
     private static int THREADS_CREATED;
     private static Stack THREAD_POOL;
     
     static 
          {
          MAX_THREADS = 100;
          THREADS_CREATED = 0;
          THREAD_POOL = new Stack( );
          }
Doc 33 End Game Slide # 2

     ServerEngine handlerFactory;
     Socket client;
     
     public static void setMaxThreads( int max )
          {
          MAX_THREADS = max;
          }
          
     public static Thread getThread(ServerEngine handlerFactory,
                               Socket client ) throws OutOfThreadsException
          {
          ServerThread nextThread = null;
          if ( THREAD_POOL.size() > 0 )
               nextThread = (ServerThread) THREAD_POOL.pop();
          else if (THREADS_CREATED < MAX_THREADS) 
               nextThread = new ServerThread();
          else
               throw new OutOfThreadsException( "No more Threads");
                         
          nextThread.handlerFactory = handlerFactory;
          nextThread.client = client;
          return nextThread; 
          }
          
     private ServerThread()
          {
          THREADS_CREATED++;
          }
          

OutOfThreadsException is a new exception which must be defined
Doc 33 End Game Slide # 3
public void run()
          {
          InputStream in;
          OutputStream out;
          try
               {
               in = client.getInputStream();
               out = client.getOutputStream();
               Runnable clientHandler = handlerFactory.newInstance( in,
                                                                       out); 
               clientHandler.run();
               in.close();
               out.close();
               client.close();

               // recycle thread
               THREAD_POOL.push( this );
               }
          catch ( Exception howToHandleThis )
               {
               // A topic to be covered later
               }
          }
          
     }

Doc 33 End Game Slide # 4
Solution Two:Transaction Processing (TP) Monitors

Break server into separate applications

An application can be duplicated on different processors

Clients talk to a router

Router:

Passes client request to proper server
Handles all transactions between server and client
Balances load between servers
Hides server crashes from client


Doc 33 End Game Slide # 5

Logging Object


Enough of the

          catch ( Exception howToHandleThis )
               {
               // A topic to be covered later
               }
          }

Give your serverEngine a Logging object

class Logger
     {
     public void debugMessage( String Message)
          { blah}

     public void errorMessage( String Message)
          { blah}

     public void warningMessage( String Message)
          { blah}

     public void informationalMessage( String Message)
          { blah}

     }

(Andy your on - What patterns can be used here?)
What are the issues in the Logger class?

Doc 33 End Game Slide # 6

Homework Issues


Config files, remember them?

Concurrency in server

Concurrency in client

Telnet client

Interface as Constants

interface ARSStuff
     {
     public static final String EOF = "Done";
     public static final int      END_STATE = -1;
     public static final String blah = 
     }

class ARSServer implements ARSStuff
     {
     blah;
     }


Where do you place fields:

At end of a class?
At beginning of a class?
Both locations?

Doc 33 End Game Slide # 7

Rust Never Sleeps


Debugging the Development Process, Steve Maguire, Mircosoft Press, 1994