Chat Server Benchmark
authorbdemsky <bdemsky>
Fri, 3 Nov 2006 06:53:54 +0000 (06:53 +0000)
committerbdemsky <bdemsky>
Fri, 3 Nov 2006 06:53:54 +0000 (06:53 +0000)
Robust/src/Benchmarks/Chat/ChatServer.java [new file with mode: 0644]
Robust/src/Benchmarks/Chat/ChatSocket.java [new file with mode: 0644]
Robust/src/Benchmarks/Chat/Room.java [new file with mode: 0644]
Robust/src/Benchmarks/Chat/RoomObject.java [new file with mode: 0644]

diff --git a/Robust/src/Benchmarks/Chat/ChatServer.java b/Robust/src/Benchmarks/Chat/ChatServer.java
new file mode 100644 (file)
index 0000000..aed388e
--- /dev/null
@@ -0,0 +1,30 @@
+task Startup(StartupObject s{initialstate}) {
+    System.printString("Chat Server Benchmark");
+    RoomObject ro=new RoomObject() {Initialized};
+    ServerSocket ss=new ServerSocket(8000);
+    taskexit(s{!initialstate});
+}
+
+task AcceptConnection(ServerSocket ss{SocketPending}) {
+    ChatSocket cs=new ChatSocket() {Initialized};
+    ss.accept(cs);
+    cs.write("Please choose a chatroom".getBytes());
+}
+
+task ReadRequest(ChatSocket cs{Initialized && IOPending}) {
+    if (cs.processRead()) {
+       taskexit(cs{!Initialized, ProcessRoom});
+    }
+}
+
+task ProcessRoom(ChatSocket cs{ProcessRoom}, RoomObject ro{Initialized}) {
+    cs.processRoom(ro);
+    taskexit(cs{!ProcessRoom, InRoom});
+}
+
+task Message(ChatSocket cs{InRoom && IOPending}) {
+    byte buffer[]=new byte[1024];
+    int length=cs.read(buffer);
+    String st=(new String(buffer)).subString(0, length);
+    cs.room.sendToRoom(cs, st.getBytes());
+}
diff --git a/Robust/src/Benchmarks/Chat/ChatSocket.java b/Robust/src/Benchmarks/Chat/ChatSocket.java
new file mode 100644 (file)
index 0000000..f278c10
--- /dev/null
@@ -0,0 +1,31 @@
+public class ChatSocket extends Socket {
+    flag Initialized;
+    flag ProcessRoom;
+    flag InRoom;
+    Room room;
+    String roomrequest;
+
+    public ChatSocket() {
+    }
+
+    public boolean processRead() {
+        byte buffer[]=new byte[1024];
+        int length=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) {
+       int i=Integer.parseInt(roomrequest);
+       ro.getChatRoom(i).addParticipant(this);
+    }
+}
diff --git a/Robust/src/Benchmarks/Chat/Room.java b/Robust/src/Benchmarks/Chat/Room.java
new file mode 100644 (file)
index 0000000..c644b52
--- /dev/null
@@ -0,0 +1,22 @@
+public class Room {
+    String name;
+    HashSet participants;
+    public Room(int i) {
+       name=String.valueOf(i);
+       participants=new HashSet();
+    }
+    
+    void addParticipant(ChatSocket cs) {
+       participants.add(cs);
+       cs.room=this;
+    }
+
+    void sendToRoom(ChatSocket caller, byte [] message) {
+       HashMapIterator hmi=participants.iterator();
+       while(hmi.hasNext()) {
+           ChatSocket cs=(ChatSocket) hmi.next();
+           if (cs!=caller)
+               cs.write(message);
+       }
+    }
+}
diff --git a/Robust/src/Benchmarks/Chat/RoomObject.java b/Robust/src/Benchmarks/Chat/RoomObject.java
new file mode 100644 (file)
index 0000000..9844d3f
--- /dev/null
@@ -0,0 +1,13 @@
+public class RoomObject {
+    flag Initialized;
+    Room[] rooms;
+    public RoomObject() {
+       rooms=new Room[10];
+       for(int i=0;i<rooms.length;i++)
+           rooms[i]=new Room(i);
+    }
+
+    Room getChatRoom(int i) {
+       return rooms[i];
+    }
+}