Hash implementation of Webserver working version 1.1
authoradash <adash>
Sun, 5 Nov 2006 21:43:47 +0000 (21:43 +0000)
committeradash <adash>
Sun, 5 Nov 2006 21:43:47 +0000 (21:43 +0000)
Robust/src/Tests/Inventory.java
Robust/src/Tests/ItemInfo.java [new file with mode: 0644]
Robust/src/Tests/WebServerExample.java
Robust/src/Tests/WebServerSocket.java

index 94d79e63fc2191d4e605d4acb762ffe8e59f3b62..3072c52dcd923c5e4a285ba1a4d20b84996b11f6 100644 (file)
@@ -4,88 +4,77 @@ public class Inventory {
 
        // Transaction variables
        int numitems;
-       int current;//keeps track of current position
-       int totalvalue;
+       HashMap map;
        
-       // Item properties
-       String item_name[];
-       int item_quantity[];
-       int item_price[];
-
        // Constructor
        public Inventory(){
-               current = 0;
        }
 
        public Inventory(int howmany) {
                numitems = howmany;// howmany keeps track of the number of items 
                                   // in the inventory
-               current = 0;
-               item_name = new String[howmany];
-               item_quantity = new int [howmany];
-               item_price = new int [howmany];
-               for (int i = 0; i < howmany; i++) {
-                       item_name[i] = "";
-                       item_quantity[i] = 0;
-                       item_price[i] = 0;
-               }
+               map = new HashMap(numitems);
        }
 
        // Add item to a list of inventory
        public int additem(String name, int quantity, int price){
-               //check current value
-               if(current>=numitems){
-                       System.printString("Cannot add any further items");
-                       return -1;
-               }
-               // Search thru existing items
-               for(int i=0; i<numitems;i++){
-                       if(item_name[i]== name){
-                               item_quantity[i]+= quantity;
-                               return 0;
+               ItemInfo newitem = new ItemInfo(quantity, price);
+               
+               // Get the item from hash
+               if (map.containsKey(name) == false) {
+                       if (map.size() > numitems) {
+                               System.printString("Error - Items overflow");
+                               return -1;
                        }
+                       map.put(name, newitem);
+               } else {
+                       ItemInfo i = map.get(name);
+                       i.quantity += quantity;
+                       i.price = price;
+                       map.put(name, i);
                }
-               // Add new item if not found
-               item_name[current]= name;
-               item_quantity[current]= quantity;
-               item_price[current]= price;
-               current++;
+               
                return 0;
        }       
 
        // Buy item from a given list of inventory      
        public int buyitem(String name, int quantity, int price){
-               //Search through existing items 
-               for(int i=0; i<numitems;i++){
-                       if(item_name[i]== name){
-                               item_quantity[i]-= quantity;
-                               if (item_quantity[i]<=0){// if the quantity falls 
-                                                       // to zero 
-                                       current--;
-                               }
-                               totalvalue = quantity*price;
-                               return 0;
+               if (map.containsKey(name) == false) {
+                       System.printString(name);
+                       System.printString("Error - Item does not exist");
+                       return -1;
+               } else {
+                       ItemInfo i = map.get(name);
+                       if (i.quantity == 0) {
+                               System.printString("Error - Item unavailable");
+                               return -1;
                        }
+                       i.quantity -= quantity;
+                       map.put(name, i);
+                       return 0;
                }
-               System.printString("Cannot find the item in the inventory");
-               return -1;
-                       
        }
 
        //Display the inventory list
-       public int inventory(){
-               Integer tmp;
-               for(int i=0; i<current; i++){
-                       System.printString(" The items are ");
-                       System.printString(item_name[i]);       
-               //      System.printString(" The sale is ");
-                       System.printString(" The quantity of item is ");
-                       tmp = new Integer(item_quantity[i]);
-                       System.printString(tmp.toString());
-                       System.printString(" The price of item is ");
-                       tmp = new Integer(item_price[i]);
-                       System.printString(tmp.toString());
+       public String inventory(){
+               HashMapIterator i = new HashMapIterator(map, 0);
+               HashMapIterator j = new HashMapIterator(map, 1);
+               StringBuffer sb = new StringBuffer("");
+               while (i.hasNext() == true) {
+                       Object o = i.next();
+                       String name = o.toString();
+                       System.printString(name);
+                       ItemInfo oo = 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");
                }
-               return 0;
+               String item = new String(sb);   
+               return item;    
        }       
 }
diff --git a/Robust/src/Tests/ItemInfo.java b/Robust/src/Tests/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;
+       }
+}
index b43a0a26b6911883c611afbb52dc6458f7e9eee2..4bc3b4e588330ef0b8b2748d29e9789232e9e54a 100644 (file)
@@ -7,7 +7,7 @@ task Startup(StartupObject s {initialstate}) {
        ServerSocket ss = new ServerSocket(9000);
        System.printString("W> Creating ServerSocket\n");
        Logger log = new Logger() {Initialize};
-       Inventory inventorylist = new Inventory(3){TransInitialize};
+       Inventory inventorylist = new Inventory(4){TransInitialize};
        taskexit(s {!initialstate}); /* Turns initial state flag off, so this task won't refire */
 }
 
@@ -55,21 +55,59 @@ task Transaction(WebServerSocket web{TransPending}, Inventory inventorylist{Tran
        // Parse
        web.parseTransaction();
        // Check for the kind of operation
+       System.printString("DEBUG > After Web parse transaction call\n");
        op = web.parsed[0].toCharArray();
        if (op[0] == 'a') {
-               Integer qty = new Integer(web.parsed[1]);
-               Integer price = new Integer(web.parsed[2]);
-               inventorylist.additem(web.parsed[0], qty.intValue(), price.intValue());
+               System.printString("DEBUG > Calling add transaction\n");
+               Integer qty = new Integer(web.parsed[2]);
+               Integer price = new Integer(web.parsed[3]);
+               int ret = inventorylist.additem(web.parsed[1], qty.intValue(), price.intValue());
+               if (ret == 0) {
+                       web.httpresponse();
+                       StringBuffer s = new StringBuffer("Added Item ");
+                       s.append(web.parsed[1]);
+                       s.append(" Quantity ");
+                       s.append(web.parsed[2]);
+                       s.append(" Price ");
+                       s.append(web.parsed[3]);
+                       s.append("\n");
+                       String towrite = new String(s);
+                       web.write(towrite.getBytes());
+               } else {
+                       web.httpresponse();
+                       String s = new String("Error encountered");
+                       web.write(s.getBytes());
+               }
        } else if (op[0] == 'b') {
-               Integer qty = new Integer(web.parsed[1]);
-               Integer price = new Integer(web.parsed[2]);
-               inventorylist.buyitem(web.parsed[0], qty.intValue(), price.intValue());
+               System.printString("DEBUG > Calling buy transaction\n");
+               Integer qty = new Integer(web.parsed[2]);
+               Integer price = new Integer(web.parsed[3]);
+               int ret = inventorylist.buyitem(web.parsed[1], qty.intValue(), price.intValue());
+               if (ret == 0) {
+                       web.httpresponse();
+                       StringBuffer s = new StringBuffer("Bought item ");
+                       s.append(web.parsed[1]);
+                       s.append(" Quantity ");
+                       s.append(web.parsed[2]);
+                       s.append(" Price ");
+                       s.append(web.parsed[3]);
+                       s.append("<br />");
+                       String towrite = new String(s);
+                       web.write(towrite.getBytes());
+               } else {
+                       web.httpresponse();
+                       String s = new String("Error encountered");
+                       web.write(s.getBytes());
+               }
        } else if (op[0] == 'i') {
-               inventorylist.inventory();
+               System.printString("DEBUG > Calling inventory transaction\n");
+               //inventorylist.inventory();
+               String towrite = inventorylist.inventory();     
+               web.write(towrite.getBytes());
+               
        } else {
                System.printString("T > Error - Unknown transaction\n");
        }
-
        //Invoke operations
 
        web.close();
index f94bba605ed42b9482b09588a58daf99082099e0..69fd1c689e0d68ec29cd066890a19ab6227f8f45 100644 (file)
@@ -46,7 +46,6 @@ public class WebServerSocket extends Socket {
                
                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];
@@ -81,24 +80,14 @@ public class WebServerSocket extends Socket {
                        return false;
                }
        }
+
        
        //Process special request
-       public void parseTransaction() {
+       public int parseTransaction() {
                //System.printString("DEBUG -> Inside parseTransaction");       
                int start = filename.indexOf('_');
                String s = filename.subString(start+1);
                int n = 4;
-/*
-               if (s.startsWith("add")== true) {
-                       n = 4;
-               }
-               if (s.startsWith("buy")== true) {
-                       n = 3;
-               }
-               if (s.startsWith("inventory")== true) {
-                       n = 1;
-               }
-*/
                for (int i = 0; i < n; i++) {
                        int index;
                        if (i == n-1) 
@@ -113,6 +102,8 @@ public class WebServerSocket extends Socket {
                        System.printString("\n Parsing : ");
                        System.printString(parsed[i]);
                }
-               return;
+               System.printString(" DEBUG > INSIDE PARSE TRANSACTION");
+
+               return 0;
        }       
 }