From 0c2068c58fa7f27972567f45e306fee9d189bedb Mon Sep 17 00:00:00 2001 From: adash Date: Sun, 5 Nov 2006 21:43:47 +0000 Subject: [PATCH] Hash implementation of Webserver working version 1.1 --- Robust/src/Tests/Inventory.java | 105 +++++++++++-------------- Robust/src/Tests/ItemInfo.java | 8 ++ Robust/src/Tests/WebServerExample.java | 56 ++++++++++--- Robust/src/Tests/WebServerSocket.java | 19 ++--- 4 files changed, 107 insertions(+), 81 deletions(-) create mode 100644 Robust/src/Tests/ItemInfo.java diff --git a/Robust/src/Tests/Inventory.java b/Robust/src/Tests/Inventory.java index 94d79e63..3072c52d 100644 --- a/Robust/src/Tests/Inventory.java +++ b/Robust/src/Tests/Inventory.java @@ -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) { + 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 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("
"); + 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(); diff --git a/Robust/src/Tests/WebServerSocket.java b/Robust/src/Tests/WebServerSocket.java index f94bba60..69fd1c68 100644 --- a/Robust/src/Tests/WebServerSocket.java +++ b/Robust/src/Tests/WebServerSocket.java @@ -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; } } -- 2.34.1