java sources
authorbdemsky <bdemsky>
Fri, 2 Mar 2007 20:51:24 +0000 (20:51 +0000)
committerbdemsky <bdemsky>
Fri, 2 Mar 2007 20:51:24 +0000 (20:51 +0000)
Robust/src/Benchmarks/WebServerJ/Inventory.java [new file with mode: 0644]
Robust/src/Benchmarks/WebServerJ/ItemInfo.java [new file with mode: 0644]
Robust/src/Benchmarks/WebServerJ/Logger.java [new file with mode: 0644]
Robust/src/Benchmarks/WebServerJ/WebServerExample.java [new file with mode: 0644]
Robust/src/Benchmarks/WebServerJ/WebServerThread.java [new file with mode: 0644]

diff --git a/Robust/src/Benchmarks/WebServerJ/Inventory.java b/Robust/src/Benchmarks/WebServerJ/Inventory.java
new file mode 100644 (file)
index 0000000..570ac6b
--- /dev/null
@@ -0,0 +1,94 @@
+import java.util.*;
+import java.net.*;
+import java.io.*;
+
+public class Inventory {
+    // Transaction variables
+    int numitems;
+    HashMap map;
+    int balance;
+    
+    // Constructor
+    public Inventory(){
+       map = new HashMap();
+       balance=100000;
+    }
+    
+    public Inventory(int howmany) {
+       numitems = howmany;// howmany keeps track of the number of items 
+       // in the inventory
+       map = new HashMap();
+    }
+    
+    // Add item to a list of inventory
+    public synchronized void additem(String name, int quantity, int price){
+       ItemInfo newitem = new ItemInfo(quantity, price);
+       balance-=quantity*price;
+       
+       // Get the item from hash
+       if (map.containsKey(name) == false) {
+           map.put(name, newitem);
+       } else {
+           ItemInfo i = (ItemInfo) map.get(name);
+           i.quantity += quantity;
+           i.price = price;
+           map.put(name, i);
+       }
+    }  
+    
+    // Buy item from a given list of inventory 
+    public synchronized int buyitem(String name, int quantity){
+       if (map.containsKey(name) == false) {
+           //          System.printString("Error - Item does not exist");
+           return -1;
+       } else {
+           ItemInfo i = (ItemInfo) map.get(name);
+           if (i.quantity == 0) {
+               //                      System.printString("Error - Item unavailable");
+               return -1;
+           }
+           if ((i.quantity-quantity) < 0 ) {
+               //                      System.printString("Error - Available qty is less: Cannot Buy\n");
+               return -1;
+           } else {
+               i.quantity -= quantity;
+               map.put(name, i);
+               balance+=quantity*i.price;
+               return i.price;
+           }
+       }
+    }
+
+    //Display the inventory list
+    public synchronized void inventory(Socket s){
+       try {
+           OutputStream sockout=s.getOutputStream();
+       Iterator i = map.keySet().iterator();// Gets key from the hashmap= name of item
+       Iterator j = map.values().iterator();//Gets the value from hashmap 
+       int totalvalue=balance;
+       while (i.hasNext() == true) {
+           StringBuffer sb = new StringBuffer("");
+           Object o = i.next();
+           String name = o.toString();
+           ItemInfo oo = (ItemInfo) j.next();
+           sb.append(name);
+           sb.append(" ");
+           Integer q = new Integer(oo.quantity);
+           sb.append(q.toString());
+           sb.append(" ");
+           Integer p = new Integer(oo.price);
+           sb.append(p.toString());
+           sb.append("\n");
+           totalvalue+=oo.quantity*oo.price;
+           sockout.write(sb.toString().getBytes());
+       }
+       StringBuffer sb=new StringBuffer("");
+       sb.append("Total value: ");
+       sb.append((new Integer(totalvalue)).toString());
+       sb.append("\n");
+       sockout.write(sb.toString().getBytes());
+       } catch (Exception e) {
+           e.printStackTrace();
+       }       
+    }
+}
diff --git a/Robust/src/Benchmarks/WebServerJ/ItemInfo.java b/Robust/src/Benchmarks/WebServerJ/ItemInfo.java
new file mode 100644 (file)
index 0000000..18a61a5
--- /dev/null
@@ -0,0 +1,8 @@
+class ItemInfo {
+       int quantity;
+       int price;
+       ItemInfo(int x, int y) { 
+               quantity = x;
+               price = y;
+       }
+}
diff --git a/Robust/src/Benchmarks/WebServerJ/Logger.java b/Robust/src/Benchmarks/WebServerJ/Logger.java
new file mode 100644 (file)
index 0000000..558c65f
--- /dev/null
@@ -0,0 +1,46 @@
+import java.io.*;
+
+public class Logger {
+    //Logger flag
+    FileOutputStream fos;
+
+
+    //Constructor
+    public Logger(){
+       try {
+       fos=new FileOutputStream("request.log");//Open request.log file 
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+
+    //Logs filename as per client requests
+    public void logrequest(String filename){
+       try {
+       String request = new String("\nNew Request received: ");
+       fos.write(request.getBytes());
+       fos.write(filename.getBytes());
+       fos.flush();
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+
+    public void logrequest(){
+       try {
+       String request = new String("\nNew Request received: ");
+       fos.write(request.getBytes());
+       fos.flush();
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+    
+    public void closerequest() {
+       try {
+           fos.close();        
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+}
diff --git a/Robust/src/Benchmarks/WebServerJ/WebServerExample.java b/Robust/src/Benchmarks/WebServerJ/WebServerExample.java
new file mode 100644 (file)
index 0000000..c485b18
--- /dev/null
@@ -0,0 +1,31 @@
+import java.net.*;
+
+public class WebServerExample {
+
+    public static void main(String arg[]) {
+       // Create New ServerSocket
+       //      System.printString("W> Starting\n");
+       ServerSocket ss=null;
+       try {
+           ss= new ServerSocket(9000);
+       } catch (Exception e){}
+       //      System.printString("W> Creating ServerSocket\n");
+       Logger log = new Logger();
+       Inventory inventorylist = new Inventory();
+       acceptConnection(ss, log, inventorylist);
+    }
+
+    //Listen for a request and accept request 
+    public static void acceptConnection(ServerSocket ss, Logger log, Inventory inventorylist) {
+       //      System.printString("W> Waiting for connection...\n");
+       while(true) {
+           Socket s=null;
+           try {
+               s=ss.accept();
+           } catch (Exception e) {}
+           WebServerThread web = new WebServerThread(s, log, inventorylist);
+           web.start();
+           //  System.printString("W> Connected... \n");
+       }
+    }
+}
diff --git a/Robust/src/Benchmarks/WebServerJ/WebServerThread.java b/Robust/src/Benchmarks/WebServerJ/WebServerThread.java
new file mode 100644 (file)
index 0000000..329af8e
--- /dev/null
@@ -0,0 +1,275 @@
+import java.io.*;
+import java.net.*;
+
+public class WebServerThread extends Thread {
+    //Filename requested by the client 
+    String filename;
+    String[] parsed; 
+    String prefix;
+    Logger log;
+    Inventory inventorylist;
+    Socket sock;
+    InputStream sockin;
+    OutputStream sockout;
+
+    //Constructor
+    public WebServerThread(Socket s, Logger log, Inventory inventory){
+       parsed = new String[4];
+       this.log=log;
+       this.sock=s;
+       try {
+           sockin=s.getInputStream();
+           sockout=s.getOutputStream();
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+       this.inventorylist=inventory;
+    }
+    
+    public void run() {
+       // Process the incoming http request 
+       while (!clientrequest()) {
+       }
+       if(checktrans()==false) {
+           // Not special transaction , do normal filesending  
+           SendFile();
+           LogRequest();
+       } else {
+           // Invoke special inventory transaction
+           Transaction();
+           LogRequest();
+       }
+    }
+    
+    //Do the WriteIO on server socket and send the requested file to Client
+    public void SendFile() {
+       sendfile();
+       try {
+           sock.close();
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+
+    // Log the Client request
+    public void LogRequest() {
+       log.logrequest(filename);
+    }
+    
+    //Transaction on Inventory
+    public void Transaction() {
+       // Parse
+       int op = parseTransaction();
+       // Check for the kind of operation
+       if (op == 0 ) { /* Add */
+           //          System.printString("DEBUG > Calling add transaction\n");
+           Integer qty = new Integer(parsed[2]);
+           Integer price = new Integer(parsed[3]);
+           inventorylist.additem(parsed[1], qty.intValue(), price.intValue());
+           httpresponse();
+           StringBuffer s = new StringBuffer("Added Item ");
+           s.append(parsed[1]);
+           s.append(" Quantity ");
+           s.append(parsed[2]);
+           s.append(" Price ");
+           s.append(parsed[3]);
+           s.append("\n");
+           String towrite = new String(s);
+           try {
+               sockout.write(towrite.getBytes());
+           } catch (Exception e) {
+               e.printStackTrace();
+           }
+       } else if (op == 1) { /* Buy */
+           //          System.printString("DEBUG > Calling buy transaction\n");
+           Integer qty = new Integer(parsed[2]);
+           int ret = inventorylist.buyitem(parsed[1], qty.intValue());
+           if (ret >= 0) {
+               httpresponse();
+               StringBuffer s = new StringBuffer("Bought item ");
+               s.append(parsed[1]);
+               s.append(" Quantity ");
+               s.append(parsed[2]);
+               s.append(" Cost ");
+               Integer cost = new Integer(ret*qty.intValue());
+               String c = cost.toString();
+               s.append(c);
+               String towrite = new String(s);
+               try {
+                   sockout.write(towrite.getBytes());
+               } catch (Exception e) {
+                   e.printStackTrace();
+               }
+           } else {
+               httpresponse();
+               String s = new String("Error encountered");
+               try {
+                   sockout.write(s.getBytes());
+               } catch (Exception e) {
+                   e.printStackTrace();
+               }
+           }
+       } else if (op == 2) { /* Inventory */
+           //          System.printString("DEBUG > Calling inventory transaction\n");
+           httpresponse();
+           inventorylist.inventory(sock);      
+       } else { /* Error */ 
+           //          System.printString("T > Error - Unknown transaction\n");
+       }
+       //Invoke close operations
+       try {
+           sock.close();
+       } catch (Exception e) {e.printStackTrace();}
+    }
+
+    
+    //Send the http header for web browser display     
+    public void httpresponse(){
+       StringBuffer header = new StringBuffer("HTTP/1.0 200 OK\n");
+       header.append("Content-type: text/html\n");
+       header.append("\n\n");
+       String temp_str = new String(header);
+       try {
+           sockout.write(temp_str.getBytes());
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+       return;
+       
+    }
+    
+    // Send the html file , read from file one byte at a time  
+    public void sendfile() {
+       StringBuffer req_file = new StringBuffer("./htmlfiles/");
+       req_file.append(filename);
+       String filepath = new String(req_file);
+       FileInputStream def_file = null;
+       try {
+           def_file=new FileInputStream(filepath);
+       } catch (FileNotFoundException e) {
+       } 
+       //      int status = def_file.getfd();//Checks if the file is present in 
+       //current directory     
+       httpresponse();
+       if (def_file == null){
+           StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if
+           // file not found
+           response.append(filename);
+           String buffer = new String(response);
+           try {
+               sockout.write(buffer.getBytes());
+           } catch (Exception e) {
+               e.printStackTrace();
+           }
+           try {
+               def_file.close();
+           } catch (Exception e) {
+               e.printStackTrace();
+           }
+           return;
+           }
+       byte buf[] = new byte[16];
+       int ret;
+       
+       try {
+       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];
+           }
+           sockout.write(tosend);
+           //String str = new String(tosend);
+       }
+       def_file.close();
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+    }
+    
+    //Read the client request and extract the filename from it 
+    public boolean clientrequest(){
+       byte b1[] = new byte[1024];
+       int numbytes=0;
+       try {
+           numbytes=sockin.read(b1);//Read client request from web server socket
+       } catch (Exception e) {
+           e.printStackTrace();
+       }
+       String curr=(new String(b1)).substring(0, numbytes);
+       if (prefix!=null) {
+           StringBuffer sb=new StringBuffer(prefix);
+           sb.append(curr);
+           curr=sb.toString();
+       }
+       prefix=curr;
+       if(prefix.indexOf("\r\n\r\n")>=0) {
+           
+           int index = prefix.indexOf('/');//Parse the GET client request to find filename
+           int end = prefix.indexOf('H');
+           filename = prefix.substring((index+1), (end-1));
+           //      System.printString("\n");
+           return true;
+       }
+       return false;
+    }
+    
+    // Parse  for the prefix in the client request
+    // This is helpful to find if the prefix is a special transaction
+    public boolean checktrans(){
+       if (filename.startsWith("trans") == true) {
+           return true;
+       } else {
+           return false;
+       }
+    }
+    
+    //Parse for the substrings in the filename and use it to obtain the
+    //kind of operation, name of item, quantity of item, price of item
+    //e.g. trans_add_car_2_10000 is the filename
+    //store in the parsed[] string , add,car,2,1000
+    public int parseTransaction(){
+       int start = filename.indexOf('_');
+       String s = filename.substring(start+1);
+       
+       if (s.startsWith("add")==true){
+           //          System.printString("DEBUG > ADD\n");
+           int i1 = s.indexOf('_');
+           parsed[0] = new String(s.substring(0,i1));
+           
+           int i2 = s.indexOf('_',i1+1);
+           parsed[1] = new String(s.substring(i1+1,i2));
+           
+           int i3 = s.indexOf('_',i2+1);
+           parsed[2] = new String(s.substring(i2+1,i3));
+           
+           String s3 = s.substring(i3+1);
+           parsed[3] = s3;
+           
+           return 0;
+           
+       }
+       if (s.startsWith("buy")==true){
+           //          System.printString("DEBUG > BUY\n");
+           int i1 = s.indexOf('_');
+           parsed[0] = s.substring(0,i1);
+           
+           int i2 = s.indexOf('_', i1+1);
+           parsed[1] = s.substring(i1+1,i2);
+           
+           String s2 = s.substring(i2+1);
+           parsed[2] = s2;
+           
+           parsed[3] = "";
+           
+           return 1;
+       }
+       if (s.startsWith("inventory")==true){
+           //          System.printString("DEBUG > INVENTORY\n");
+           return 2;
+           
+       }
+       // Error transaction
+       return -1;
+    }
+}