Copyright 1998 Massachusetts Institute of Technology
*
* @author Todd C. Panrell, tparnell@ai.mit.edu
* @version $Id: BabySitter.java,v 1.2 2003/09/23 15:18:08 gus Exp $
*
*/
public class BabySitter implements Runnable {
/** Server this client is connected to */
protected Server server;
/** Animacy that runs this object */
protected Thread spirit;
/** Client connection */
protected Socket sock;
/** Where to read from client */
protected ObjectInputStream ois;
/** Where to write to client */
protected ObjectOutputStream oos;
/** Flag to indicate whether the Thread animating this should stop */
private boolean stopped;
/**
* Creates a BabySitter to handle communication with a
* client. Spawns a new thread to handle input from client;
* sends strings on request.
*
* @param sock the client socket of this threads client
* @param server The Server
object for this BabySitter
*
* @see #send
* @see Server#sendToAllExcept
*/
protected BabySitter(Socket sock, Server server) {
System.out.println( "Server: setting up new connection from " +
sock.getInetAddress().getHostName() +
" on port " + sock.getPort() );
this.sock = sock;
this.server = server;
try {
// it is very, very important to create the Output before Input
this.oos = new ObjectOutputStream(sock.getOutputStream());
this.ois = new ObjectInputStream(sock.getInputStream());
} catch (IOException e) {
try { this.sock.close(); } catch ( IOException e2 ) {}
System.out.println("Server: socket error in BabySitter");
this.server.removeBabySitter(this);
return;
}
this.spirit = new Thread( this );
this.spirit.start();
}
/**
* Sends a String to the client.
*
* @param s the String to send
*/
protected void send(String s) {
try {
// System.out.println("Server: sending "+s);
this.oos.writeObject(s);
// System.out.println("Server: successfully sent "+s);
} catch (IOException e) {
System.out.println("Server: socket error in send");
this.server.removeBabySitter(this);
}
}
/**
* Recieves new info from clients.
*
* @see Server#sendToAllExcept
*/
public void run() {
System.out.println("Server: BabySitter running");
while( !this.stopped )
{
try {
String s = (String)ois.readObject();
System.out.println( "Server: just read '" + s +"' from " +
this.sock.getInetAddress().getHostName() +
", port " + this.sock.getPort() );
if( s == null || s == "" ) {
this.server.removeBabySitter(this);
return;
}
this.server.sendToAllExcept(s,this);
} catch (IOException e) {
System.out.println("Server: socket error in run ");
this.server.removeBabySitter(this);
} catch (ClassNotFoundException cnfe) {
System.out.println("Server: could not find class String.");
this.server.removeBabySitter(this);
}
}
}
/**
* Should only be called by Server.
* User server.removeBabySittter( BabySitter ) instead.
*
* @see Server#removeBabySitter
*/
protected void stop() {
System.out.println( "Server: closing connection from " +
this.sock.getInetAddress().getHostName() +
" on port " + this.sock.getPort() );
try {
this.ois.close();
} catch (IOException e) {}
try {
this.oos.close();
} catch (IOException e) {}
try {
this.sock.close();
} catch (IOException e) {}
this.stopped = true;
}
}
/*
* $Log: BabySitter.java,v $
* Revision 1.2 2003/09/23 15:18:08 gus
* javadoc fix
*
* Revision 1.1.1.1 2002/06/05 21:56:32 root
* CS101 comes to Olin finally.
*
* Revision 1.7 1998/07/24 17:13:36 tparnell
* Placate new javadoc behavior
*
* Revision 1.6 1998/07/22 15:42:22 tparnell
* moved to cs101.net
*
* Revision 1.5 1998/07/20 21:35:50 tparnell
* Moved from Data*Stream to Object*Stream.
*
* Revision 1.4 1998/07/08 16:11:49 tparnell
* update to JDK1.2: removed refrences to Thread.stop() since it has been
* deprecated in 1.2
*
* Revision 1.3 1998/06/24 21:24:27 tparnell
* added logging to file
*
*/