From 296426028cff16922553cfa207e5ac02f09d78b4 Mon Sep 17 00:00:00 2001 From: bdemsky Date: Fri, 3 Nov 2006 06:53:54 +0000 Subject: [PATCH] Chat Server Benchmark --- Robust/src/Benchmarks/Chat/ChatServer.java | 30 +++++++++++++++++++++ Robust/src/Benchmarks/Chat/ChatSocket.java | 31 ++++++++++++++++++++++ Robust/src/Benchmarks/Chat/Room.java | 22 +++++++++++++++ Robust/src/Benchmarks/Chat/RoomObject.java | 13 +++++++++ 4 files changed, 96 insertions(+) create mode 100644 Robust/src/Benchmarks/Chat/ChatServer.java create mode 100644 Robust/src/Benchmarks/Chat/ChatSocket.java create mode 100644 Robust/src/Benchmarks/Chat/Room.java create mode 100644 Robust/src/Benchmarks/Chat/RoomObject.java diff --git a/Robust/src/Benchmarks/Chat/ChatServer.java b/Robust/src/Benchmarks/Chat/ChatServer.java new file mode 100644 index 00000000..aed388ea --- /dev/null +++ b/Robust/src/Benchmarks/Chat/ChatServer.java @@ -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 index 00000000..f278c10b --- /dev/null +++ b/Robust/src/Benchmarks/Chat/ChatSocket.java @@ -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 index 00000000..c644b523 --- /dev/null +++ b/Robust/src/Benchmarks/Chat/Room.java @@ -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 index 00000000..9844d3f6 --- /dev/null +++ b/Robust/src/Benchmarks/Chat/RoomObject.java @@ -0,0 +1,13 @@ +public class RoomObject { + flag Initialized; + Room[] rooms; + public RoomObject() { + rooms=new Room[10]; + for(int i=0;i