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