-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegistration_server.java
70 lines (53 loc) · 1.77 KB
/
Registration_server.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
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
/**
*
* The file contains the Registration Server
* Author: Ketan Joshi (ksj4205)
*/
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.net.*;
import java.util.concurrent.ConcurrentHashMap;
import static java.util.Objects.hash;
public class Registration_server {
static ConcurrentHashMap<Integer,Boolean> AliveNodes = new ConcurrentHashMap<>();
static ConcurrentHashMap<Integer,NodeThread> Nodes = new ConcurrentHashMap<>();
static ConcurrentHashMap<Integer,Socket>Sockets = new ConcurrentHashMap<>();
public static void main(String[] args)
{
//server socket
try {
ServerSocket server = new ServerSocket(6000);
while (true) {
Socket temp = server.accept();
// System.out.println("1 connection established!!");
new Thread(new NodeThread(temp)).start();
BroadcastAliveNodes();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void PrintHashmap()
{
System.out.println("Current Alive Nodes:\n"+AliveNodes);
}
public static void BroadcastAliveNodes()
{
int i =0;
int[] alivenodes= new int[AliveNodes.keySet().size()];
InetAddress[] ips = new InetAddress[AliveNodes.keySet().size()];
for (int nodes:AliveNodes.keySet()) {
alivenodes[i]= nodes;
ips[i] =Sockets.get(nodes).getInetAddress();
//System.out.println("+"+ips[i]);
i++;
}
for (int nodes : AliveNodes.keySet())
{
NodeThread temp = Nodes.get(nodes);
temp.SendAliveNodes(alivenodes,ips);
}
}
}