00001 package edu.stanford.hci.r3.flash.timelineControl;
00002
00003 import java.io.*;
00004 import java.net.ServerSocket;
00005 import java.net.Socket;
00006
00007 import edu.stanford.hci.r3.config.Constants;
00008
00024 public class FlashControlServer {
00025
00029 public static final int DEFAULT_PORT = Constants.Ports.FLASH_CONTROL_SERVER;
00030
00034 public static void main(String[] args) {
00035 int port = DEFAULT_PORT;
00036
00037 try {
00038 port = Integer.parseInt(args[0]);
00039 } catch (ArrayIndexOutOfBoundsException e) {
00040
00041 }
00042
00043 new FlashControlServer(port);
00044 }
00045
00049 private Socket incoming;
00050
00054 private BufferedReader readerIn;
00055
00059 private int serverPort;
00060
00064 private ServerSocket socket;
00065
00069 private PrintStream writerOut;
00070
00074 public FlashControlServer() {
00075 this(DEFAULT_PORT);
00076 }
00077
00081 public FlashControlServer(int port) {
00082 serverPort = port;
00083 new Thread(getServer()).start();
00084 }
00085
00089 private Runnable getServer() {
00090 return new Runnable() {
00091
00092 public void run() {
00093 System.out.println(">> Starting FlashControlServer");
00094 try {
00095 socket = new ServerSocket(serverPort);
00096 System.out.println(">> Waiting for a Client...");
00097 incoming = socket.accept();
00098 System.out.println(">> Flash UI Client connected.");
00099 readerIn = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
00100 writerOut = new PrintStream(incoming.getOutputStream());
00101 sendMessage("Say EXIT to exit.\r");
00102 boolean done = false;
00103 while (!done) {
00104 System.out.println(">> Reading a line from the client.");
00105 String str = readerIn.readLine();
00106 if (str == null || str.trim().equals("EXIT")) {
00107 done = true;
00108 incoming.close();
00109 }
00110 }
00111 } catch (Exception e) {
00112 System.out.println(e);
00113 }
00114 System.out.println(">> Closing FlashControlServer");
00115 }
00116 };
00117 }
00118
00125 public void sendMessage(String message) {
00126 if (writerOut != null) {
00127 writerOut.println(message);
00128 writerOut.flush();
00129 } else {
00130 System.out.println("There is no client to send the message to.");
00131 }
00132 System.out.println(message);
00133 System.out.flush();
00134 }
00135 }