------------------------- Week 12 Notes for CST8165 ------------------------- -Ian! D. Allen - idallen@idallen.ca - www.idallen.com Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Keep up on your readings (Course Outline: average 4 hours/week homework) Lab 4 debriefing ---------------- - Most people didn't finish Lab 4. Why? - What is fair to the students who *did* finish? Coding an HTTP server (Java) ---------------------------- HTTP RFC: http://tools.ietf.org/html/rfc2616 Testing tools: http://teaching.idallen.com/cst8165/08w/notes/autotest_http.sh.txt http://teaching.idallen.com/cst8165/08w/notes/sample_http_test_out.txt W3C Java server (HTTP 1.1): Jigsaw http://www.w3.org/Jigsaw/ A working Java HTTP server with basic functionality (in 145 lines) is available here: http://www.brics.dk/ixwt/examples/FileServer.java - this version does not adhere to the HTTP RFC in many respects - needs comments on functionality (not on how Java works) - has many "public" items that should be made private - may be missing things such as closing opened files... (Older version: http://www.brics.dk/~amoeller/WWW/javaweb/index.html ) An overview of TCP, HTTP and servers using Java: http://www.brics.dk/ixwt/http.pdf Sun Guides/Tutorials on Java networking (mostly client side): http://java.sun.com/j2se/1.5.0/docs/guide/net/overview/overview.html http://java.sun.com/docs/books/tutorial/networking/index.html http://java.sun.com/docs/books/tutorial/networking/urls/index.html java.net references: http://java.sun.com/j2se/1.5.0/docs/api/java/net/package-summary.html java.net intro http://www.brics.dk/~amoeller/WWW/javaweb/javanet.html Java 5.0 (also known as 1.5) package documentation: http://java.sun.com/j2se/1.5.0/docs/ http://java.sun.com/j2se/1.5.0/docs/api/ - java.io.File, java.lang.String, etc. Java Notes (from a non-Java programmer) ---------- * On returning a pair of strings from a function I suggested that your HTTP server error function take two input strings. The first string is the Status Code and Reason Phrase from the HTTP RFC. The second string is text to put into the Message Body of the Response, giving more detail on the error, e.g.: "404 Not Found" "The Request /nosuchfile.html was not found on this server." * How to return a pair of strings from a function in Java: public class IanStrings { private String[] foo() { return new String[] { "string one", "string two" }; } public static void main(String[] args) { IanStrings istr = new IanStrings(); String[] result = istr.foo(); if (result != null) { System.out.println(result[0] + " and " + result[1]); } } } * On setting and using the setSoTimeout method - the action of using the method to set a time-out may raise a socket I/O exception, at the time you set the time-out (you need a try/catch) - later, when the timer triggers, it will raise the SocketTimeoutException - the above are different exceptions and will occur in different places in your program (and you need different try/catch for them) - to set the time-out, you need to know exactly where your HTTP server blocks waiting for input (which is the same place as all your previous servers) ------------------------------------------------------------------------- Eclipse IDE demo (in the T127 Lab - Fall 2007) ---------------------------------------------- - see also the NetBeans IDE from Sun Warning: Eclipse will need about 10MB of file space for each workspace! You can use your "N" drive to store unused files: $ share //algshare/home/ share: Attaching smb file system //algshare/home at /tmp/smb-abcd0001. Password: Spawning /bin/bash. Exit shell to unmount Samba share. $ cp -a workspace-old /tmp/smb-abcd0001 # use your userid not abcd0001 $ rm -rf workspace-old $ exit Unmounting /tmp/smb-abcd0001. Most of the actions below have keyboard shortcuts that are much faster than navigating menus. Preparing to run Eclipse and starting Eclipse .. If you have old .eclipse or workspace directories from a previous version of Eclipse (old version of Java), remove or rename them. .. Start eclipse (e.g. from the command line or a menu) .. Select a location for your workspace (e.g. accept the default) .. Close the "Welcome" tab (using the X box) Creating a new Project .. Select: File | New | Project .. Select a wizard: Java | Java Project (Next) .. enter Project name: PigLatinHTTP .. select link at bottom: Configure compliance .. select Compiler compliance level: 6.0 .. Apply .. Yes (full rebuild) .. OK .. In the New Java Project dialog, make sure the "Configure compliance" warning is gone .. Finish .. Open Associated Perspective? Yes Importing your FileServer.java file .. Select your PigLatinHTTP project first, then .. File | Import .. Select import source: General | File System (Next) Make sure that the "Into folder:" says PigLatinHTTP .. Browse to the From directory containing your FileServer.java file - you are selecting a *directory* here, not a *file* - enter "." to use your home directory .. In the directory listing, select FileServer.java .. Finish Opening the imported FileServer.java file in the editor .. In the Package tab, use the drop arrow to open PigLatinHTTP .. Use the drop arrow to open (default package) .. Right Click on FileServer.java and select Open - or double-click on the source file name - the FileServer.java tab should open with the source visible - make sure there are no error tags in the left margin of the code Running your project for the first time (setting arguments) .. Right click on the source code and select Run As | Java Application - the program should run - you should see FileServer [Java Application] - you should see a "Usage:" message from the server in the console window at the bottom of the screen (because of missing arguments) .. From the top menu bar select Run | Run .. Select the (x)= Arguments tab .. In "Program arguments" enter the port and directory: 55555 /tmp .. select Apply .. select Run - you should see a successful start-up message in the console window "FileServer accepting connections on port 55555" .. Push the red square to kill the application. See "" in the console window Re-running your project .. select the green Arrow in the top menu bar See the console output window at the bottom of the screen. .. Push the red square to kill the application. See "" in the console window Adding more files to your project .. Use the Import facility to get more source files .. If the files are imported to the same project/directory, their classes will be available to your main program; you only need to use them Tips: - in the source code, hold your cursor over any word to get help on that word - use F2 to lock focus on the help and allow scrolling - right-click on the source and select Source | Format ------------------------------------------------------------------------- HTTP server testing Testing - black box vs. white box, "behavioral" vs. "structural" ------- - Neither I nor your boss has enough time to read and test all your code; you have to do it, using the test framework supplied. Looking at the FileServer white-box style: http://www.brics.dk/ixwt/examples/FileServer.java - what tests exercise every line of code, especially each of the exceptions? - Internet break-ins happen via causing obscure error conditions that trigger lines of code that were never properly tested Automated Testing - use it right from the start ----------------- I've provided a script that will do automated testing of your HTTP server, and I've written a few simple automated tests. You must use this script to test your server, and you must organize the script and add your own tests to the script to test things that I haven't. No marks are awarded for using my random tests without modification. Don't be limited by the categories or tests I've coded in the script - my list of tests is incomplete and in a random order. Rewrite the test suite to suit yourself. Add more tests to the suite and organize and renumber the tests that are there into logical categories. If you start immediately using the automated testing script to test your server, you'll save time over doing manual testing and then having to repeat all your tests for handing in. Some programming disciplines have you write the test suite first, then write the code to pass all the tests. If a test doesn't exist for a function, the function is not considered implemented (because it can't be tested). ----------------------------------------------------------------------------- Lab 2 debriefing ---------------- Marking demo: I demonstrated the test harness I used to test your Lab 2 submissions.