-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientReceiver.java
44 lines (39 loc) · 1.41 KB
/
ClientReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package clients;
import java.io.BufferedReader;
import java.util.concurrent.LinkedBlockingQueue;
public class ClientReceiver extends Thread
{
private BufferedReader messageReader;
private LinkedBlockingQueue<String> serverMessageList;
private boolean isRunning;
public ClientReceiver(BufferedReader messageReader, LinkedBlockingQueue<String> serverMessageList) {
this.messageReader = messageReader;
this.serverMessageList = serverMessageList;
isRunning = true;
}
@Override
public void interrupt() {
super.interrupt();
isRunning = false;
}
// Receive messages and puts them in a queue.
@Override
public void run()
{
String serverMessage;
String[] splittedMessage;
while(isRunning){
try{
serverMessage = messageReader.readLine();
splittedMessage = serverMessage.split("%", 3);
serverMessage = new String( splittedMessage[0] + "\n" + splittedMessage[1] + "\n" + splittedMessage[2] );
serverMessageList.put(serverMessage);
System.out.println("Received Message from Server: " + serverMessage);
}
catch(Exception e) {
System.out.println("ERROR: Couldn't listen to server's messages.");
isRunning = false;
}
}
}
}