aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m7/src/Node.java
blob: 2a5d56a0a9eed743f081162919ef2c2ffb8b2e9f (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.Semaphore;

import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.InputStream;

class NodeAddr {
	public int port;
	public InetAddress addr;

	public NodeAddr(String addr, int port) throws UnknownHostException {
		this.port = port;
		this.addr = InetAddress.getByName(addr);
	}

	public NodeAddr(String fulladdr) throws UnknownHostException {
		String fields[] = fulladdr.split(":");
		this.port = Integer.parseInt(fields[1]);
		this.addr = InetAddress.getByName(fields[0]);
	}
}

public class Node {
	private NodeAddr bind;
	private ArrayList<NodeAddr> neighbors;

	private HashSet<String> seenMsgs;
	private Semaphore lock;

	public String name;

	public Node(String bind, String name) throws UnknownHostException {
		this.bind = new NodeAddr(bind);
		this.neighbors = new ArrayList<>();
		this.name = name;
		this.seenMsgs = new HashSet<>();
		this.lock = new Semaphore(1);
	}
	
	public void addNeighbor(String addr) throws UnknownHostException {
		this.neighbors.add(new NodeAddr(addr));
	}

	public void start() throws IOException {
		ServerSocket socket = null;
		try {
			socket = new ServerSocket(this.bind.port, 50, this.bind.addr);

			while (true) {
				Socket conn = socket.accept();
				Handler h = new Handler(conn, this);
				h.start();
			}
		} finally {
			if (socket != null) {
				socket.close();
			}
		}
	}
	
	public void sendMsgLocal(Message msg) throws Exception {
		// Kind of hacky but thats how we do it
		msg.id = ThreadLocalRandom.current().nextInt();
		
		if (!this.checkMsg(msg)) {
			this.sendMsg(msg);
		}
	}
	
	private boolean checkMsg(Message msg) throws InterruptedException {
		String msgid = String.format("%s%d", msg.from, msg.id);
		this.lock.acquire();

		if (this.seenMsgs.contains(msgid)) {
			this.lock.release();
			return true;
		}
		this.seenMsgs.add(msgid);
		this.lock.release();
		return false;
	}

	private void sendMsg(Message msg) throws Exception {
		for (NodeAddr n : this.neighbors) {
			Socket conn = null;
			OutputStream out = null;
			try {
				conn = new Socket(n.addr, n.port);
				out = conn.getOutputStream();

				msg.send(out);

			} finally {
				if (conn != null) {
					conn.close();
				}
				if (out != null) {
					out.close();
				}
			}
		}
	}
	
	public void handleMsg(Message msg) throws Exception {
		if (this.checkMsg(msg)) {
			return;
		}
		if (!msg.to.equals(this.name)) {
			msg.trace(this.name);

			// Forward
			this.sendMsg(msg);
			
			if (!msg.to.equals("")) {
				return;
			}
		}
		
		msg.handle(this);
	}
}

class Handler extends Thread {
	private Socket conn;
	private Node node;

	public Handler(Socket conn, Node node) {
		this.conn = conn;
		this.node = node;
	}

	public void run() {
		InputStream in = null;
		ObjectInputStream objin = null;
		try {
			in = this.conn.getInputStream();
			objin = new ObjectInputStream(in);
			Object obj = objin.readObject();
			if (!(obj instanceof Message)) {
				throw new Exception("Received object is not a message");
			}
			this.node.handleMsg((Message) obj);
		} catch (Exception e) {
			System.err.printf("Client err: %s%n", e);
		}
	}
}