check in changes
authorbdemsky <bdemsky>
Thu, 22 Feb 2007 16:36:22 +0000 (16:36 +0000)
committerbdemsky <bdemsky>
Thu, 22 Feb 2007 16:36:22 +0000 (16:36 +0000)
Robust/src/Benchmarks/ChatJava/ChatServer.java [new file with mode: 0644]
Robust/src/Benchmarks/ChatJava/ChatThread.java [new file with mode: 0644]
Robust/src/Benchmarks/ChatJava/Room.java [new file with mode: 0644]
Robust/src/Benchmarks/ChatJava/RoomObject.java [new file with mode: 0644]
Robust/src/Benchmarks/WebServer/Inventory.java
Robust/src/Benchmarks/WebServerJava/Inventory.java

diff --git a/Robust/src/Benchmarks/ChatJava/ChatServer.java b/Robust/src/Benchmarks/ChatJava/ChatServer.java
new file mode 100644 (file)
index 0000000..3c72421
--- /dev/null
@@ -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 (file)
index 0000000..7500abe
--- /dev/null
@@ -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 (file)
index 0000000..d43d83c
--- /dev/null
@@ -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 (file)
index 0000000..015a402
--- /dev/null
@@ -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;
+    }
+}
index fafc29c08f43b2e7d6e51d3db76054cf1ed02f05..ca8cb3a5134c2331d11fec45eb8f38700aa325a1 100644 (file)
@@ -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();   
+    }  
 }
index 5755b7396d6c9dbe58bb205ef648f659d1f19c7a..50a9ce6b668a2edb63db837d6a25cccfce7f6d9b 100644 (file)
@@ -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();   
+    }  
 }