From a5cb4677541bd99b0e3bbdabc6ac0666156ccacd Mon Sep 17 00:00:00 2001 From: adash Date: Wed, 1 Nov 2006 21:06:28 +0000 Subject: [PATCH] Bug fixed Comments added; Most up-to-date working version with logging functionality --- Robust/src/Tests/Logger.java | 14 ++++---------- Robust/src/Tests/WebServerExample.java | 22 ++++++++++------------ Robust/src/Tests/WebServerSocket.java | 22 +++++++++++----------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/Robust/src/Tests/Logger.java b/Robust/src/Tests/Logger.java index 88f97142..6e8ffa0a 100644 --- a/Robust/src/Tests/Logger.java +++ b/Robust/src/Tests/Logger.java @@ -1,31 +1,25 @@ public class Logger extends FileOutputStream { - //Logging flag + //Logger flag flag Initialize; //Constructor public Logger(){ - System.printString(" Log Object Created\n"); - //FileOutputStreamOpen("./Tests/htmlfiles/request.log"); + FileOutputStreamOpen("./Tests/htmlfiles/request.log");//Open request.log file } -/* + //Logs filename as per client requests public void logrequest(String filename){ String request = new String("\nNew Request received: "); write(request.getBytes()); write(filename.getBytes()); flush(); - close(); } public void logrequest(){ String request = new String("\nNew Request received: "); write(request.getBytes()); flush(); - closerequest(); - } -*/ - public void logtesting(){ - System.printString(" testing log object\n"); } + public void closerequest() { close(); } diff --git a/Robust/src/Tests/WebServerExample.java b/Robust/src/Tests/WebServerExample.java index 6967e847..c0e482be 100644 --- a/Robust/src/Tests/WebServerExample.java +++ b/Robust/src/Tests/WebServerExample.java @@ -1,7 +1,7 @@ /* Startup object is generated with the initialstate flag set by the * system to start the computation up */ -/* Create New ServerSocket*/ +// Create New ServerSocket task Startup(StartupObject s {initialstate}) { System.printString("W> Starting\n"); ServerSocket ss = new ServerSocket(9000); @@ -10,7 +10,7 @@ task Startup(StartupObject s {initialstate}) { taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */ } -/*Listen for a request and accept request*/ +//Listen for a request and accept request task AcceptConnection(ServerSocket ss{SocketPending}) { System.printString("W> Waiting for connection...\n"); WebServerSocket web = new WebServerSocket() {!WritePending}; @@ -18,14 +18,14 @@ task AcceptConnection(ServerSocket ss{SocketPending}) { System.printString("W> Connected... \n"); } -/* Process the incoming http request */ +// Process the incoming http request task ProcessRequest(WebServerSocket web{IOPending}) { System.printString("W> Inside ProcessRequest... \n"); web.clientrequest(); - taskexit(web {WritePending, LogPending}); + taskexit(web {WritePending, LogPending}); //Sets the WritePending and LogPending flag true } -/* Do the WriteIO on server socket and send the requested file to Client*/ +//Do the WriteIO on server socket and send the requested file to Client task SendFile(WebServerSocket web{WritePending}) { System.printString("W> Inside SendFile ... \n"); web.sendfile(); @@ -33,12 +33,10 @@ task SendFile(WebServerSocket web{WritePending}) { taskexit(web {!WritePending}); } -/* Log the Client request*/ -task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) { -//task LogRequest(Logger log{Initialize}) { -// System.printString("L > Inside logrequest"); -// log.logrequest(); - log.logtesting(); +// Log the Client request +task LogRequest(WebServerSocket web{LogPending}, Logger log{Initialize}) {//Task fired when both + // LogPending and Initialize flags are true + System.printString("L > Inside logrequest\n"); + log.logrequest(web.filename); taskexit(web {!LogPending}); -// taskexit(log {!Initialize}); } diff --git a/Robust/src/Tests/WebServerSocket.java b/Robust/src/Tests/WebServerSocket.java index 02d3d029..3ef5589c 100644 --- a/Robust/src/Tests/WebServerSocket.java +++ b/Robust/src/Tests/WebServerSocket.java @@ -2,6 +2,7 @@ public class WebServerSocket extends Socket { // Websocket flag flag LogPending; flag WritePending; + //Filename requested by the client String filename; //Constructor @@ -21,32 +22,34 @@ public class WebServerSocket extends Socket { // Send the html file , read from file one byte at a time public void sendfile() { - //String filepath = new String("./Tests/htmlfiles/index1.html"); StringBuffer req_file = new StringBuffer("./Tests/htmlfiles/"); req_file.append(filename); String filepath = new String(req_file); FileInputStream def_file = new FileInputStream(filepath); - int status = def_file.getfd(); + int status = def_file.getfd();//Checks if the file is present in + //current directory httpresponse(); if (status == -1){ - StringBuffer response = new StringBuffer("404: not found: "); + StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if + // file not found response.append(filename); String buffer = new String(response); write(buffer.getBytes()); - //System.printString("File does not exist"); def_file.close(); return; } byte buf[] = new byte[16]; int ret; - while ((ret = def_file.read(buf)) > 0) { + while ((ret = def_file.read(buf)) > 0) {// Read from file and write + // one byte at a time into the socket + byte tosend[] = new byte[ret]; for (int i = 0; i < ret; i++) { tosend[i] = buf[i]; } write(tosend); - String str = new String(tosend); + //String str = new String(tosend); } def_file.close(); } @@ -54,14 +57,11 @@ public class WebServerSocket extends Socket { //Discover what the client wants and handle their request public int clientrequest(){ byte b1[] = new byte[1024]; - read(b1); + read(b1);//Read client request from web server socket String clientreq = new String(b1); - int index = clientreq.indexOf('/'); + int index = clientreq.indexOf('/');//Parse the GET client request to find filename int end = clientreq.indexOf('H'); filename = clientreq.subString((index+1), (end-1)); - System.printString("DEBUG -> Client requested: "); - System.printString(filename); - System.printString("\n"); return 0; } } -- 2.34.1