import java.io.*; import java.util.*; import java.net.*; import java.awt.*; public class ThreadServer { static ServerSocket mySocket; static InetAddress myAddress; static String myHostName; static int myPort; static World myWorld; static Dimension bounds; static ControlThreadManager controller; static WorkSender workSender; static PictureKeeper pictureKeeper; public static void main(String[] args) { startServer(args); // bounds = new Dimension(100, 100); pictureKeeper = new PictureKeeper(bounds); workSender = new WorkSender(bounds, myWorld, pictureKeeper); controller = new ControlThreadManager( mySocket, workSender, pictureKeeper); controller.start(); } // StartServer also makes a World. public static void startServer(String[] args) { // First, get an address. System.out.println("Getting my address..."); try { myAddress = InetAddress.getLocalHost(); myHostName = myAddress.getHostName(); System.out.println("My address is " + myAddress); } catch (UnknownHostException e) { System.out.println("Could not find an IP address."); } // Now initialize the World. try { if (args.length < 2) { System.out.println("Usage: java ThreadServer width height"); System.exit(10); } myWorld = new World(); bounds = new Dimension(Integer.parseInt(args[0]), Integer.parseInt(args[1])); myWorld.spherulate(bounds.width, bounds.height, 50); } catch (Exception e) { System.out.println("Couldn't create a world."); System.out.println(e + ": " + e.getMessage()); } // Open the server socket. try { System.out.println("Opening server socket..."); mySocket = new ServerSocket(0); myPort = mySocket.getLocalPort(); System.out.println("Socket opened on port " + myPort); } catch (Exception e) { System.out.println("Couldn't open socket. " + e.getMessage()); System.exit(0); } createHTMLFile(); } public static void createHTMLFile() { try { FileWriter f = new FileWriter("index.html", false); StringBuffer html = new StringBuffer(); System.out.println("Creating HTML file..."); html.append("\n"); html.append("\n"); html.append("\n"); html.append("\n"); html.append("\n"); html.append("No java. "); String out = html.toString(); f.write(out, 0, out.length()); f.flush(); f.close(); System.out.println("Done creating HTML file."); System.out.println(); System.out.println("Ready."); System.out.println(); } catch (Exception e) { System.out.println("Couldn't write index.html. " + e.getMessage()); System.exit(0); } } }