--- /dev/null
+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();
+ }
+ }
+}
--- /dev/null
+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);
+ }
+}
--- /dev/null
+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);
+ }
+ }
+}
--- /dev/null
+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;
+ }
+}
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();
+ }
}
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();
+ }
}