CS 596: Client-Server Programming
| End Game | slide # 1 |
| ...Scaling C/S Up | slide # 1 |
| ...Logging Object | slide # 5 |
| ...Homework Issues | slide # 6 |
| ...Rust Never Sleeps | slide # 7 |
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( );
}
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++;
}
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
}
}
}An application can be duplicated on different processors
Clients talk to a router
Router:
catch ( Exception howToHandleThis )
{
// A topic to be covered later
}
}
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}
}
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: