From 1b4ae1fad6157118d28f1c43acb967af47240684 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Sat, 10 Nov 2007 23:16:32 +0000 Subject: [PATCH] add java version --- .../Benchmarks/Conglomerator/Java/Google.java | 10 ++ .../Conglomerator/Java/Inventory.java | 88 ++++++++++++ .../Conglomerator/Java/ItemInfo.java | 8 ++ .../Benchmarks/Conglomerator/Java/Logger.java | 28 ++++ .../Benchmarks/Conglomerator/Java/Lookup.java | 37 +++++ .../Benchmarks/Conglomerator/Java/Stock.java | 10 ++ .../Conglomerator/Java/Weather.java | 9 ++ .../Conglomerator/Java/WebServerExample.java | 93 ++++++++++++ .../Conglomerator/Java/WebServerSocket.java | 135 ++++++++++++++++++ 9 files changed, 418 insertions(+) create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Google.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Inventory.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/ItemInfo.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Logger.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Lookup.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Stock.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/Weather.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/WebServerExample.java create mode 100644 Robust/src/Benchmarks/Conglomerator/Java/WebServerSocket.java diff --git a/Robust/src/Benchmarks/Conglomerator/Java/Google.java b/Robust/src/Benchmarks/Conglomerator/Java/Google.java new file mode 100644 index 00000000..496017ad --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Google.java @@ -0,0 +1,10 @@ +public class Google extends Lookup{ + public Google() { + url=""; + hostname="www.google.com"; + start=""; + end=""; + exclusive=true; + } + +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/Inventory.java b/Robust/src/Benchmarks/Conglomerator/Java/Inventory.java new file mode 100644 index 00000000..4cfcdf45 --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Inventory.java @@ -0,0 +1,88 @@ +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 int 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); + } + return 0; + } + + // Buy item from a given list of inventory + public 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; + } + } + return 0; + } + + //Display the inventory list + //Display the inventory list + public synchronized void inventory(Socket s){ + HashMapIterator i = new HashMapIterator(map, 0);// Gets key from the hashmap= name of item + HashMapIterator j = new HashMapIterator(map, 1);//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; + s.write(sb.toString().getBytes()); + } + StringBuffer sb=new StringBuffer(""); + sb.append("Total value: "); + sb.append((new Integer(totalvalue)).toString()); + sb.append("\n"); + s.write(sb.toString().getBytes()); + } +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/ItemInfo.java b/Robust/src/Benchmarks/Conglomerator/Java/ItemInfo.java new file mode 100644 index 00000000..18a61a59 --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/ItemInfo.java @@ -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/Conglomerator/Java/Logger.java b/Robust/src/Benchmarks/Conglomerator/Java/Logger.java new file mode 100644 index 00000000..25f2d4d6 --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Logger.java @@ -0,0 +1,28 @@ +public class Logger { + //Logger flag + FileOutputStream fos; + + + //Constructor + public Logger(){ + fos=new FileOutputStream("request.log");//Open request.log file + } + + //Logs filename as per client requests + public void logrequest(String filename){ + String request = new String("\nNew Request received: "); + fos.write(request.getBytes()); + fos.write(filename.getBytes()); + fos.flush(); + } + + public void logrequest(){ + String request = new String("\nNew Request received: "); + fos.write(request.getBytes()); + fos.flush(); + } + + public void closerequest() { + fos.close(); + } +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/Lookup.java b/Robust/src/Benchmarks/Conglomerator/Java/Lookup.java new file mode 100644 index 00000000..75640db6 --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Lookup.java @@ -0,0 +1,37 @@ +public class Lookup { + boolean exclusive; + String url; + String hostname; + String data; + String start; + String end; + Socket s; + + public void fix() { + int istart=data.indexOf(start); + int iend=data.indexOf(end); + if (exclusive) + data=data.substring(istart+start.length(), iend); + else + data=data.substring(istart, iend+end.length()); + String m1="src=\"/"; + String m2="src=\'/"; + String m3="href=\"/"; + boolean cnt=true; + while(cnt) { + if (data.indexOf(m1)!=-1) { + int index=data.indexOf(m1)-1; + data=data.substring(0,index+m1.length())+"http://"+hostname+data.substring(index+m1.length(),data.length()); + } else if (data.indexOf(m2)!=-1) { + int index=data.indexOf(m2)-1; + data=data.substring(0,index+m2.length())+"http://"+hostname+data.substring(index+m2.length(),data.length()); + } else if (data.indexOf(m3)!=-1) { + int index=data.indexOf(m3)-1; + data=data.substring(0,index+m3.length())+"http://"+hostname+data.substring(index+m3.length(),data.length()); + } else cnt=false; + } + + } + + +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/Stock.java b/Robust/src/Benchmarks/Conglomerator/Java/Stock.java new file mode 100644 index 00000000..f9f6ac8e --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Stock.java @@ -0,0 +1,10 @@ +public class Stock extends Lookup { + public Stock() { + url="q?s=%5EDJI"; + hostname="finance.yahoo.com"; + start=""; + end=""; + exclusive=true; + } + +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/Weather.java b/Robust/src/Benchmarks/Conglomerator/Java/Weather.java new file mode 100644 index 00000000..ea4d7850 --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/Weather.java @@ -0,0 +1,9 @@ +public class Weather extends Lookup{ + public Weather() { + url="warnings.php?wfo=sgx&zone=CAZ042&pil=XXXHWOSGX&productType=Hazardous+Weather+Outlook"; + hostname="www.wrh.noaa.gov"; + start="Hazardous Weather Outlook"; + end="$$"; + } + +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/WebServerExample.java b/Robust/src/Benchmarks/Conglomerator/Java/WebServerExample.java new file mode 100644 index 00000000..f18f198e --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/WebServerExample.java @@ -0,0 +1,93 @@ +public class WebServerExample extends Thread { +/* Startup object is generated with the initialstate flag set by the + * system to start the computation up */ + Logger log; + WebServerSocket web; + Socket s; + + public WebServerExample(WebServerSocket web, Socket s, Logger log) { + this.web=web; + this.s=s; + this.log=log; + } + + public static void main(String x[]) { + ServerSocket ss = new ServerSocket(9000); + Logger log = new Logger(); + while(true) { + Socket s=ss.accept(); + WebServerSocket web = new WebServerSocket(); + WebServerExample wse=new WebServerExample(web, s, log); + wse.start(); + } + } + + public void doLookup(Lookup l) { + String query="GET /"+l.url+" HTTP/1.1\r\nConnection: close\r\nHost:"+l.hostname+"\r\n\r\n"; + l.s=new Socket(l.hostname, 80); + l.s.write(query.getBytes()); + } + + public void ReceiveQuery(Lookup l) { + byte[] buffer=new byte[1024]; + boolean cnt=true; + + while(cnt) { + int numchars=l.s.read(buffer); + if (numchars<=0) { + l.fix(); + l.s.close(); + return; + } + String str=new String(buffer, 0, numchars); + if (l.data==null) { + l.data=str; + } else + l.data=l.data+str; + } + } + + public void run() { + ProcessRequest(); + } + + public void ProcessRequest() { + if (web.clientrequest(s)) { + if(web.checktrans()==false) { + LogRequest(); + SendFile(); + } else { + Weather w=new Weather(); + Google g=new Google(); + Stock st=new Stock(); + doLookup(w); + ReceiveQuery(w); + doLookup(g); + ReceiveQuery(g); + doLookup(st); + ReceiveQuery(st); + LogRequest(); + Transaction(w, g, st); + } + } + } + + public void SendFile() { + web.sendfile(s); + s.close(); + } + + public void LogRequest() { + log.logrequest(web.filename); + } + + public void Transaction(Weather weather, Google g, Stock st){ + web.httpresponse(s); + s.write(("").getBytes()); + s.write(weather.data.getBytes()); + s.write(g.data.getBytes()); + s.write(st.data.getBytes()); + s.write(("").getBytes()); + s.close(); + } +} diff --git a/Robust/src/Benchmarks/Conglomerator/Java/WebServerSocket.java b/Robust/src/Benchmarks/Conglomerator/Java/WebServerSocket.java new file mode 100644 index 00000000..354b48dc --- /dev/null +++ b/Robust/src/Benchmarks/Conglomerator/Java/WebServerSocket.java @@ -0,0 +1,135 @@ +public class WebServerSocket { + //Filename requested by the client + String filename; + String[] parsed; + String prefix; + + //Constructor + public WebServerSocket(){ + parsed = new String[4]; + } + + //Send the http header for web browser display + public void httpresponse(Socket s){ + 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); + s.write(temp_str.getBytes()); + return; + + } + + // Send the html file , read from file one byte at a time + public void sendfile(Socket s) { + StringBuffer req_file = new StringBuffer("./htmlfiles/"); + req_file.append(filename); + String filepath = new String(req_file); + FileInputStream def_file = new FileInputStream(filepath); + int status = def_file.getfd();//Checks if the file is present in + //current directory + httpresponse(s); + if (status == -1){ + StringBuffer response = new StringBuffer("404: not found: ");//Send 404 error if + // file not found + response.append(filename); + String buffer = new String(response); + s.write(buffer.getBytes()); + def_file.close(); + return; + } + byte buf[] = new byte[16]; + int ret; + + 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]; + } + s.write(tosend); + //String str = new String(tosend); + } + def_file.close(); + } + + //Read the client request and extract the filename from it + public boolean clientrequest(Socket s){ + byte b1[] = new byte[1024]; + int numbytes=s.read(b1);//Read client request from web server socket + 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)); + 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("portal") == 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; + } +} -- 2.34.1