From 4cd4e72df287ee865df5a2d3ba145ba830f660fe Mon Sep 17 00:00:00 2001 From: bdemsky Date: Thu, 22 Feb 2007 16:36:22 +0000 Subject: [PATCH] check in changes --- .../src/Benchmarks/ChatJava/ChatServer.java | 17 ++ .../src/Benchmarks/ChatJava/ChatThread.java | 55 +++++++ Robust/src/Benchmarks/ChatJava/Room.java | 22 +++ .../src/Benchmarks/ChatJava/RoomObject.java | 14 ++ .../src/Benchmarks/WebServer/Inventory.java | 152 ++++++++--------- .../Benchmarks/WebServerJava/Inventory.java | 153 +++++++++--------- 6 files changed, 269 insertions(+), 144 deletions(-) create mode 100644 Robust/src/Benchmarks/ChatJava/ChatServer.java create mode 100644 Robust/src/Benchmarks/ChatJava/ChatThread.java create mode 100644 Robust/src/Benchmarks/ChatJava/Room.java create mode 100644 Robust/src/Benchmarks/ChatJava/RoomObject.java diff --git a/Robust/src/Benchmarks/ChatJava/ChatServer.java b/Robust/src/Benchmarks/ChatJava/ChatServer.java new file mode 100644 index 00000000..3c724215 --- /dev/null +++ b/Robust/src/Benchmarks/ChatJava/ChatServer.java @@ -0,0 +1,17 @@ +public class ChatServer { + + public static int main(String arg[]) { + System.printString("Chat Server Benchmark"); + RoomObject ro=new RoomObject(); + ServerSocket ss=new ServerSocket(8000); + acceptConnection(ss,ro); + } + + public static void acceptConnection(ServerSocket ss, RoomObject ro) { + while(true) { + Socket s=ss.accept(); + ChatThread cs=new ChatThread(s, ro); + cs.start(); + } + } +} diff --git a/Robust/src/Benchmarks/ChatJava/ChatThread.java b/Robust/src/Benchmarks/ChatJava/ChatThread.java new file mode 100644 index 00000000..7500abe9 --- /dev/null +++ b/Robust/src/Benchmarks/ChatJava/ChatThread.java @@ -0,0 +1,55 @@ +public class ChatThread extends Thread { + Room room; + String roomrequest; + Socket sock; + RoomObject ro; + + public ChatThread(Socket sock, RoomObject ro) { + this.sock=sock; + this.ro=ro; + } + + public void run() { + sock.write("Please choose a chatroom".getBytes()); + ReadRequest(); + ProcessRoom(); + while(true) + Message(); + } + + public void ReadRequest() { + while (!processRead()) + ; + } + + private void ProcessRoom() { + processRoom(ro); + } + + public void Message() { + byte buffer[]=new byte[1024]; + int length=sock.read(buffer); + String st=(new String(buffer)).subString(0, length); + room.sendToRoom(this, st.getBytes()); + } + + public boolean processRead() { + byte buffer[]=new byte[1024]; + int length=sock.read(buffer); + String st=new String(buffer); + String curr=st.subString(0, length); + if (roomrequest!=null) { + StringBuffer sb=new StringBuffer(roomrequest); + sb.append(curr); + curr=sb.toString(); + } + roomrequest=curr; + if (roomrequest.indexOf("\n")>=0) { + return true; + } + return false; + } + public void processRoom(RoomObject ro) { + ro.getChatRoom(roomrequest).addParticipant(this); + } +} diff --git a/Robust/src/Benchmarks/ChatJava/Room.java b/Robust/src/Benchmarks/ChatJava/Room.java new file mode 100644 index 00000000..d43d83c2 --- /dev/null +++ b/Robust/src/Benchmarks/ChatJava/Room.java @@ -0,0 +1,22 @@ +public class Room { + String name; + HashSet participants; + public Room(String n) { + name=n; + participants=new HashSet(); + } + + synchronized void addParticipant(ChatThread cs) { + participants.add(cs); + cs.room=this; + } + + synchronized void sendToRoom(ChatThread caller, byte [] message) { + HashMapIterator hmi=participants.iterator(); + while(hmi.hasNext()) { + ChatThread cs=(ChatThread) hmi.next(); + if (cs!=caller) + cs.sock.write(message); + } + } +} diff --git a/Robust/src/Benchmarks/ChatJava/RoomObject.java b/Robust/src/Benchmarks/ChatJava/RoomObject.java new file mode 100644 index 00000000..015a402a --- /dev/null +++ b/Robust/src/Benchmarks/ChatJava/RoomObject.java @@ -0,0 +1,14 @@ +public class RoomObject { + HashMap rooms; + public RoomObject() { + rooms=new HashMap(); + } + + synchronized Room getChatRoom(String name) { + if (rooms.containsKey(name)) + return (Room) rooms.get(name); + Room r=new Room(name); + rooms.put(name, r); + return r; + } +} diff --git a/Robust/src/Benchmarks/WebServer/Inventory.java b/Robust/src/Benchmarks/WebServer/Inventory.java index fafc29c0..ca8cb3a5 100644 --- a/Robust/src/Benchmarks/WebServer/Inventory.java +++ b/Robust/src/Benchmarks/WebServer/Inventory.java @@ -1,79 +1,87 @@ public class Inventory { - // Inventory flags - flag TransInitialize; - - // Transaction variables - int numitems; - HashMap map; + // Inventory flags + flag TransInitialize; + + // Transaction variables + int numitems; + HashMap map; + int balance=100000; + + // Constructor + public Inventory(){ + map = new HashMap(); + } + + 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; - // Constructor - public Inventory(){ - map = new HashMap(); + // 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); } - - public Inventory(int howmany) { - numitems = howmany;// howmany keeps track of the number of items - // in the inventory - map = new HashMap(); + 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; + } - // Add item to a list of inventory - public int additem(String name, int quantity, int price){ - ItemInfo newitem = new ItemInfo(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); - return i.price; - } - } - return 0; + //Display the inventory list + public String inventory(){ + 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 + StringBuffer sb = new StringBuffer(""); + int totalvalue=balance; + while (i.hasNext() == true) { + 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; } - - //Display the inventory list - public String inventory(){ - 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 - StringBuffer sb = new StringBuffer(""); - while (i.hasNext() == true) { - 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"); - } - String item = new String(sb); - return item; - } + sb.append("Total value: "); + sb.append(new Integer(totalvalue)); + sb.append("\n"); + return sb.toString(); + } } diff --git a/Robust/src/Benchmarks/WebServerJava/Inventory.java b/Robust/src/Benchmarks/WebServerJava/Inventory.java index 5755b739..50a9ce6b 100644 --- a/Robust/src/Benchmarks/WebServerJava/Inventory.java +++ b/Robust/src/Benchmarks/WebServerJava/Inventory.java @@ -1,79 +1,88 @@ public class Inventory { - // Inventory flags - flag TransInitialize; - - // Transaction variables - int numitems; - HashMap map; + // Inventory flags + flag TransInitialize; + + // 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 int additem(String name, int quantity, int price){ + ItemInfo newitem = new ItemInfo(quantity, price); + balance-=quantity*price; - // Constructor - public Inventory(){ - map = new HashMap(); + // 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); } - - public Inventory(int howmany) { - numitems = howmany;// howmany keeps track of the number of items - // in the inventory - map = new HashMap(); + return 0; + } + + // 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; + } } + return 0; + } - // Add item to a list of inventory - public synchronized int additem(String name, int quantity, int price){ - ItemInfo newitem = new ItemInfo(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 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); - return i.price; - } - } - return 0; + //Display the inventory list + public synchronized String inventory(){ + 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 + StringBuffer sb = new StringBuffer(""); + int totalvalue=balance; + while (i.hasNext() == true) { + 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; } - - //Display the inventory list - public synchronized String inventory(){ - 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 - StringBuffer sb = new StringBuffer(""); - while (i.hasNext() == true) { - 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"); - } - String item = new String(sb); - return item; - } + sb.append("Total value: "); + sb.append((new Integer(totalvalue)).toString()); + sb.append("\n"); + return sb.toString(); + } } -- 2.34.1