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();
}
/* 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);
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};
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();
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});
}
// Websocket flag
flag LogPending;
flag WritePending;
+ //Filename requested by the client
String filename;
//Constructor
// 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();
}
//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;
}
}