aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m7/src/InputHandler.java
blob: c6b87d30c7b23ab3f2615dc5da502c76153c8faf (plain)
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
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class InputHandler extends Thread {
	private Node node;

	public InputHandler(Node node) {
		this.node = node;
	}
	
	public void run() {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		try {
			String line;
			while ((line = in.readLine()) != null) {
				char cmd = line.charAt(0);
				Message msg = null;
				if (cmd == '#' || cmd == '!') {
					msg = new PingMessage(this.node.name, line.substring(1), cmd == '!');
				} else {
					msg = new TextMessage(this.node.name, "", line);
				}
				this.node.sendMsgLocal(msg);
			}
		} catch (Exception e){
			System.err.printf("Send err: %s%n", e);
		}
	}
}