diff --git a/.gitignore b/.gitignore index 79fbb028..c884b9fd 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* /.metadata/ +/bin/ diff --git a/Circles/build.xml b/Circles/build.xml new file mode 100644 index 00000000..e69de29b diff --git a/HelloWorld/src/edu/nmsu/cs/helloworld/helloworld.java b/HelloWorld/src/edu/nmsu/cs/helloworld/helloworld.java new file mode 100644 index 00000000..89d7952d --- /dev/null +++ b/HelloWorld/src/edu/nmsu/cs/helloworld/helloworld.java @@ -0,0 +1,7 @@ +package edu.nmsu.cs.helloworld; + +public class helloworld { + public static void main(String[] args) { + System.out.println("Hello World"); + } +} diff --git a/Questions/p1Questions.txt b/Questions/p1Questions.txt new file mode 100644 index 00000000..020b5520 --- /dev/null +++ b/Questions/p1Questions.txt @@ -0,0 +1,6 @@ +1.4 hours +2.Thought/Design: 3 hours + Programming: 2 hours + Debugging: 2 hours + TOTAL: 7 hours +3.10% accuracy diff --git a/Questions/p2Questions.txt b/Questions/p2Questions.txt new file mode 100644 index 00000000..e69de29b diff --git a/README.md b/README.md index 685238c5..b1d35643 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,6 @@ This assignment relies on the Coverage programs. ## Program 4 This assignment relies on the Circles programs. + +## user ID +Anishmod diff --git a/SimpleWebServer/build.xml b/SimpleWebServer/build.xml new file mode 100644 index 00000000..5c25ded2 --- /dev/null +++ b/SimpleWebServer/build.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SimpleWebServer/resources/res/acc/test.html b/SimpleWebServer/resources/res/acc/test.html new file mode 100644 index 00000000..3bc5cf19 --- /dev/null +++ b/SimpleWebServer/resources/res/acc/test.html @@ -0,0 +1,7 @@ + + + + +

Hello world, the current date is: . The server is: .

+ + \ No newline at end of file diff --git a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebServer.java b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebServer.java index 0bfe882d..bb77dd8a 100644 --- a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebServer.java +++ b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebServer.java @@ -15,49 +15,38 @@ import java.net.ServerSocket; import java.net.Socket; -public class WebServer -{ - private ServerSocket socket; +public class WebServer { + private ServerSocket socket; - private boolean running; + private boolean running; /** * Constructor **/ - private WebServer() - { + private WebServer() { running = false; } /** - * Web server starting point. This method does not return until the server is finished, so perhaps - * it should be named "runServer" or something like that. + * Web server starting point. This method does not return until the server is + * finished, so perhaps it should be named "runServer" or something like that. * - * @param port - * is the TCP port number to accept connections on + * @param port is the TCP port number to accept connections on **/ - private boolean start(int port) - { + private boolean start(int port) { Socket workerSocket; WebWorker worker; - try - { + try { socket = new ServerSocket(port); - } - catch (Exception e) - { + } catch (Exception e) { System.err.println("Error binding to port " + port + ": " + e); return false; } - while (true) - { - try - { + while (true) { + try { // wait and listen for new client connection workerSocket = socket.accept(); - } - catch (Exception e) - { + } catch (Exception e) { System.err.println("No longer accepting: " + e); break; } @@ -71,38 +60,29 @@ private boolean start(int port) /** * Does not do anything, since start() never returns. **/ - private boolean stop() - { + private boolean stop() { return true; } /** - * Application main: process command line and start web server; default port number is 8080 if not - * given on command line. + * Application main: process command line and start web server; default port + * number is 8080 if not given on command line. **/ - public static void main(String args[]) - { + public static void main(String args[]) { int port = 8080; - if (args.length > 1) - { + if (args.length > 1) { System.err.println("Usage: java Webserver "); return; - } - else if (args.length == 1) - { - try - { + } else if (args.length == 1) { + try { port = Integer.parseInt(args[0]); - } - catch (Exception e) - { + } catch (Exception e) { System.err.println("Argument must be an int (" + e + ")"); return; } } WebServer server = new WebServer(); - if (!server.start(port)) - { + if (!server.start(port)) { System.err.println("Execution failed!"); } } // end main diff --git a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java index 78bba687..44b1e8eb 100644 --- a/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java +++ b/SimpleWebServer/src/edu/nmsu/cs/webserver/WebWorker.java @@ -20,73 +20,89 @@ **/ import java.io.BufferedReader; +import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.text.DateFormat; import java.util.Date; +import java.util.StringTokenizer; import java.util.TimeZone; -public class WebWorker implements Runnable -{ +public class WebWorker implements Runnable { private Socket socket; + private boolean exist = false; + private FileInputStream in; + private String fileName; /** * Constructor: must have a valid open socket **/ - public WebWorker(Socket s) - { + public WebWorker(Socket s) { socket = s; } /** - * Worker thread starting point. Each worker handles just one HTTP request and then returns, which - * destroys the thread. This method assumes that whoever created the worker created it with a - * valid open socket object. + * Worker thread starting point. Each worker handles just one HTTP request and + * then returns, which destroys the thread. This method assumes that whoever + * created the worker created it with a valid open socket object. **/ - public void run() - { - System.err.println("Handling connection..."); - try - { - InputStream is = socket.getInputStream(); - OutputStream os = socket.getOutputStream(); + public void run() { + System.err.println("Handling connection..."); // prints to command line when run + try { + InputStream is = socket.getInputStream(); // used for Input + OutputStream os = socket.getOutputStream(); // used for output readHTTPRequest(is); writeHTTPHeader(os, "text/html"); writeContent(os); os.flush(); - socket.close(); + socket.close(); // closes socket + in.close(); // closes File Input Stream + } catch (Exception e) { + System.err.println("Output error: " + e); // prints to command line when there is an error } - catch (Exception e) - { - System.err.println("Output error: " + e); - } - System.err.println("Done handling connection."); + System.err.println("Done handling connection."); // prints to command line before program is closed return; } /** * Read the HTTP request header. **/ - private void readHTTPRequest(InputStream is) - { - String line; + private void readHTTPRequest(InputStream is) { + String line = ""; BufferedReader r = new BufferedReader(new InputStreamReader(is)); - while (true) - { - try - { + int fileCheck = 1; + // while running this is constantly checking for new http request headers + while (line.length() == 0) { + try { while (!r.ready()) Thread.sleep(1); + // saves the HTTP path into string variable line line = r.readLine(); + // checks for a filepath + if (fileCheck > 0) { + // seperates the line into tokens + StringTokenizer token = new StringTokenizer(line); + token.nextToken(); + // goes through tokens to build the file name that will be accessed saves to + // fileName + fileName = token.nextToken(); + fileName = "." + fileName; + in = null; + exist = true; + // open the html file that was built + try { + in = new FileInputStream(fileName); + } catch (Exception fileNotFound) { + exist = false; + } + fileCheck--; + } System.err.println("Request line: (" + line + ")"); - if (line.length() == 0) - break; - } - catch (Exception e) - { + } // end try + catch (Exception e) { System.err.println("Request error: " + e); break; } @@ -97,42 +113,64 @@ private void readHTTPRequest(InputStream is) /** * Write the HTTP header lines to the client network connection. * - * @param os - * is the OutputStream object to write to - * @param contentType - * is the string MIME content type (e.g. "text/html") + * @param os is the OutputStream object to write to + * @param contentType is the string MIME content type (e.g. "text/html") **/ - private void writeHTTPHeader(OutputStream os, String contentType) throws Exception - { + private void writeHTTPHeader(OutputStream os, String contentType) throws Exception { Date d = new Date(); DateFormat df = DateFormat.getDateTimeInstance(); df.setTimeZone(TimeZone.getTimeZone("GMT")); - os.write("HTTP/1.1 200 OK\n".getBytes()); + if (exist) { + os.write("HTTP/1.1 200 OK\n".getBytes()); + } else { + os.write("HTTP/1.1 404 File Not Found\n".getBytes()); + } os.write("Date: ".getBytes()); os.write((df.format(d)).getBytes()); os.write("\n".getBytes()); - os.write("Server: Jon's very own server\n".getBytes()); - // os.write("Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT\n".getBytes()); - // os.write("Content-Length: 438\n".getBytes()); + os.write("Server: Anish's very own server\n".getBytes()); os.write("Connection: close\n".getBytes()); os.write("Content-Type: ".getBytes()); os.write(contentType.getBytes()); - os.write("\n\n".getBytes()); // HTTP header ends with 2 newlines + os.write("\n\n".getBytes()); return; } /** - * Write the data content to the client network connection. This MUST be done after the HTTP - * header has been written out. + * Write the data content to the client network connection. This MUST be done + * after the HTTP header has been written out. * - * @param os - * is the OutputStream object to write to + * @param os is the OutputStream object to write to **/ - private void writeContent(OutputStream os) throws Exception - { - os.write("\n".getBytes()); - os.write("

My web server works!

\n".getBytes()); - os.write("\n".getBytes()); + private void writeContent(OutputStream os) throws Exception { + System.out.println("writeContent"); + if (exist) { + System.out.println("writeContent: File exists"); + byte[] buffer = new byte[1024]; + int bytes = 0; + Date date = new Date(); + // file is used for the html file that will be opened in the browser + String file = ""; + String server = "Anish's Server!"; + + while ((bytes = in.read(buffer)) != -1) { + file = new String(buffer, 0, bytes); + // prints date when date tag is used + if (file.contains("")) { + file = file.replace("", date.toString()); + } + // prints server name when server tag is used + if (file.contains("")) { + file = file.replace("", server); + } + } + + os.write(file.getBytes()); + } else { // if file cannot be open/does not exist this html is opened + os.write("\n".getBytes()); + os.write("

404: File not found.

\n".getBytes()); + os.write("\n".getBytes()); + } } -} // end class +} // end class \ No newline at end of file diff --git a/meta/code_formatter-ecologylab-tabs.xml b/meta/code_formatter-ecologylab-tabs.xml index 35b24c1f..cf3ea537 100644 --- a/meta/code_formatter-ecologylab-tabs.xml +++ b/meta/code_formatter-ecologylab-tabs.xml @@ -1,4 +1,4 @@ - +