FlashClient.java

00001 package edu.stanford.hci.r3.flash;
00002 
00003 import java.io.BufferedReader;
00004 import java.io.IOException;
00005 import java.io.PrintStream;
00006 import java.net.Socket;
00007 
00008 import edu.stanford.hci.r3.util.DebugUtils;
00009 
00022 public class FlashClient {
00023 
00024         private int clientID;
00025         private Socket clientSocket;
00026         private BufferedReader fromClient;
00027         private FlashCommunicationServer server;
00028         private PrintStream toClient;
00029         private Thread clientThread ;
00030 
00031         public FlashClient(FlashCommunicationServer flashCommServer, int id, Socket clientSock,
00032                         BufferedReader readerIn, PrintStream writerOut) {
00033                 server = flashCommServer;
00034                 clientID = id;
00035                 clientSocket = clientSock;
00036                 toClient = writerOut;
00037                 fromClient = readerIn;
00038 
00039                 clientThread = new Thread(new Runnable() {
00040                         @Override
00041                         public void run() {
00042                                 DebugUtils.println("New Flash Client: " + clientID);
00043 
00044                                 boolean done = false;
00045                                 while (!done) {
00046                                         String command = null;
00047                                         String commandLowerCase = null;
00048                                         try {
00049                                                 command = fromClient.readLine();
00050                                         } catch (IOException e) {
00051                                                 e.printStackTrace();
00052                                         }
00053                                         try {
00054                                                 if (command == null) {
00055                                                         done = true;
00056                                                         clientSocket.close();
00057                                                 } else {
00058                                                         command = command.trim();
00059                                                         commandLowerCase = command.toLowerCase();
00060                                                         // System.out.println(">> Reading a line from the client: [" + command + "]");
00061                                                         if (command.equals("exit")) {
00062                                                                 done = true;
00063                                                                 exitClient();
00064                                                         } else if (command.equals("exitServer")) {
00065                                                                 done = true;
00066                                                                 clientSocket.close();
00067                                                                 server.exitServer();
00068                                                         } else if (command.equals("exitApplication")) {
00069                                                                 done = true;
00070                                                                 clientSocket.close();
00071                                                                 server.exitServer();
00072                                                                 System.exit(0);
00073                                                         } else {
00074                                                                 // DebugUtils.println("Client " + clientID + " Unhandled command: "
00075                                                                 // + command);
00076                                                                 server.handleCommand(clientID, command);
00077                                                         }
00078                                                 }
00079                                         } catch (IOException e) {
00080                                                 e.printStackTrace();
00081                                         }
00082                                 }
00083                         }
00084 
00085                 });
00086                 clientThread.start();
00087         }
00088 
00092         public void exitClient() {
00093                 DebugUtils.println("Exiting client " + clientID);
00094                 try {
00095                         clientSocket.close();
00096                 } catch (IOException e) {
00097                         e.printStackTrace();
00098                 }
00099         }
00100 
00107         public void sendMessage(String message) {
00108                 if (toClient != null) {
00109                         toClient.print(message + "\0");
00110                         toClient.flush();
00111                 } else {
00112                         DebugUtils.println("There is no client to send the message to.");
00113                 }
00114                 DebugUtils.println("Sent " + message.length() + " bytes.");
00115                 System.out.flush();
00116         }
00117 
00118 }

Generated on Sat Apr 14 18:21:34 2007 for R3 Paper Toolkit by  doxygen 1.4.7