-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessageReceiver.java
40 lines (32 loc) · 953 Bytes
/
MessageReceiver.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
import java.io.*;
public class MessageReceiver extends Thread implements MessageSender<String> {
private String outputPipeName;
private String id;
private String message;
public MessageReceiver(String line) {
String[] components = line.split(":",2);
setup(components[0],components[1]);
}
public MessageReceiver(String id, String message) {
setup(id,message);
}
private void setup(String id, String message) {
this.id = id;
this.message = message;
this.outputPipeName = "/tmp/"+id;
this.start();
}
public void run() {
MessageQueue.queue.add(new MessageContainer<String>(this,message));
}
public void sendMessage(String str) {
try {
PrintWriter printWriter=new PrintWriter(new BufferedOutputStream(new FileOutputStream(outputPipeName)));
printWriter.println(str);
printWriter.flush();
printWriter.close();
} catch (Exception e) {
System.out.println("Cannot write to " + outputPipeName +".");
}
}
}