start of new file
[IRC.git] / Robust / src / Benchmarks / ChatJava / ChatThread.java
1 public class ChatThread extends Thread {
2     Room room;
3     String roomrequest;
4     Socket sock;
5     RoomObject ro;
6
7     public ChatThread(Socket sock, RoomObject ro) {
8         this.sock=sock;
9         this.ro=ro;
10     }
11
12     public void run() {
13         sock.write("Please choose a chatroom".getBytes());
14         ReadRequest();
15         ProcessRoom();
16         while(true)
17             Message();
18     }
19
20     public void ReadRequest() {
21         while (!processRead())
22             ;
23     }
24
25     private void ProcessRoom() {
26         processRoom(ro);
27     }
28
29     public void Message() {
30         byte buffer[]=new byte[1024];
31         int length=sock.read(buffer);
32         if (length>0) {
33         String st=(new String(buffer)).subString(0, length);
34         room.sendToRoom(this, st.getBytes());
35         }
36     }
37     
38     public boolean processRead() {
39         byte buffer[]=new byte[1024];
40         int length=sock.read(buffer);
41         String st=new String(buffer);
42         String curr=st.subString(0, length);
43         if (roomrequest!=null) {
44             StringBuffer sb=new StringBuffer(roomrequest);
45             sb.append(curr);
46             curr=sb.toString();
47         }
48         roomrequest=curr;
49         if (roomrequest.indexOf("\n")>=0) {
50             return true;
51         }
52         return false;
53     }
54     public void processRoom(RoomObject ro) {
55         ro.getChatRoom(roomrequest).addParticipant(this);
56     }
57 }