FlashCommunicationServer.java

00001 package edu.stanford.hci.r3.flash;
00002 
00003 import java.awt.Desktop;
00004 import java.io.BufferedReader;
00005 import java.io.File;
00006 import java.io.IOException;
00007 import java.io.InputStreamReader;
00008 import java.io.PrintStream;
00009 import java.net.ServerSocket;
00010 import java.net.Socket;
00011 import java.net.SocketException;
00012 import java.util.ArrayList;
00013 import java.util.List;
00014 
00015 import edu.stanford.hci.r3.config.Constants;
00016 import edu.stanford.hci.r3.util.DebugUtils;
00017 
00033 public class FlashCommunicationServer {
00034 
00038         public static final int DEFAULT_PORT = Constants.Ports.FLASH_COMMUNICATION_SERVER;
00039 
00043         private int clientID = 0;
00044 
00048         private List<FlashClient> flashClients = new ArrayList<FlashClient>();
00049 
00050         private List<FlashListener> listeners = new ArrayList<FlashListener>();
00051 
00055         private int serverPort;
00056 
00057         private Thread serverThread;
00058 
00062         private ServerSocket socket;
00063 
00067         public FlashCommunicationServer() {
00068                 this(DEFAULT_PORT);
00069         }
00070 
00074         public FlashCommunicationServer(int port) {
00075                 serverPort = port;
00076                 serverThread = new Thread(getServer());
00077                 serverThread.start();
00078         }
00079 
00083         public void addFlashClientListener(FlashListener flashListener) {
00084                 listeners.add(flashListener);
00085         }
00086 
00090         public void exitServer() {
00091                 for (FlashClient client : flashClients) {
00092                         client.exitClient();
00093                 }
00094                 try {
00095                         socket.close();
00096                 } catch (IOException e) {
00097                         e.printStackTrace();
00098                 }
00099                 DebugUtils.println("Exiting Flash Communications Server.... If this is the last thread, the program should exit.");
00100         }
00101 
00105         private Runnable getServer() {
00106                 return new Runnable() {
00107                         public void run() {
00108                                 log("Starting Flash Communications Server at port: " + serverPort);
00109                                 try {
00110                                         socket = new ServerSocket(serverPort);
00111                                         while (true) {
00112                                                 log("Waiting for a Client...");
00113                                                 final Socket incoming = socket.accept();
00114                                                 log("Flash Client connected.");
00115                                                 final BufferedReader readerIn = new BufferedReader(new InputStreamReader(incoming
00116                                                                 .getInputStream()));
00117                                                 final PrintStream writerOut = new PrintStream(incoming.getOutputStream());
00118 
00119                                                 // pass this to a handler thread that will service this client!
00120                                                 flashClients.add(new FlashClient(FlashCommunicationServer.this, clientID++, incoming,
00121                                                                 readerIn, writerOut));
00122                                         }
00123                                 } catch (SocketException e) {
00124                                         log("Server Socket was Closed");
00125                                         // e.printStackTrace();
00126                                 } catch (IOException e) {
00127                                         e.printStackTrace();
00128                                 }
00129                                 log("Closing Flash Communications Server");
00130                         }
00131 
00132                 };
00133         }
00134 
00135         private void log(String string) {
00136                 DebugUtils.println(string);
00137         }
00138 
00143         public void handleCommand(int clientID, String command) {
00144                 DebugUtils.println("Server got command [" + command + "] from client " + clientID);
00145                 for (FlashListener listener : listeners) {
00146                         listener.messageReceived(command);
00147                 }
00148         }
00149 
00159         public void openFlashGUI(File flashGUIFile) {
00160                 // browse to the flash GUI file, and pass over our port as a query parameter
00161                 // TODO: pass the port
00162                 try {
00163                         Desktop.getDesktop().browse(flashGUIFile.toURI());
00164                 } catch (IOException e) {
00165                         e.printStackTrace();
00166                 }
00167         }
00168 
00174         public void openFlashApolloGUI(File apolloGUIFile) {
00175                 try {
00176                         ProcessBuilder processBuilder = new ProcessBuilder(apolloGUIFile.getAbsolutePath(), "port:"
00177                                         + serverPort);
00178                         processBuilder.start();
00179                 } catch (IOException e) {
00180                         e.printStackTrace();
00181                 }
00182         }
00183 
00184         public void removeAllFlashClientListeners() {
00185                 listeners.clear();
00186         }
00187 
00191         public void sendMessage(String msg) {
00192                 DebugUtils.println("Sending message: " + msg + " to all " + flashClients.size() + " clients");
00193                 for (FlashClient client : flashClients) {
00194                         client.sendMessage(msg);
00195                 }
00196         }
00197 }

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