-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlNode.java
More file actions
122 lines (99 loc) · 3.43 KB
/
Copy pathControlNode.java
File metadata and controls
122 lines (99 loc) · 3.43 KB
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
package control_node;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import TCP.Server;
import TCP.ServerThread;
import chunk_server.ChunkServer;
import messages.SendChunkCopyToAnotherChunksServer;
public class ControlNode
{
public static int CHUNK_SIZE_BYTES = 64 * 1024; // TODO May need to change this
public static int REPLICATION_LEVEL = 3;
protected static ArrayList<ChunkServerInfo> chunkServerInfos;
protected static HashMap<String, ArrayList<String>> chunkStorageInfo; // chunkname to ipadress
// TODO store chunk storage info
public static String IP_ADDRESS = (System.getProperty("os.name").startsWith("Windows") ?
"127.0.0.1" : "129.82.44.134"); // TODO Update Server address before running
public static int PORT = 5000;
private ServerSocket serverSocket;
private int serverThreadCounter;
private HeartBeatTracker heartBeatTracker;
private ChunkServerDetectorThread chunkServerDetectorThread;
private HashMap<String, ControlNodeThread> controlNodeThreadMap;
public ControlNode()
{
// TODO Try to make this thread safe
chunkServerInfos = new ArrayList<ChunkServerInfo>();
chunkStorageInfo = new HashMap<String, ArrayList<String>>();
heartBeatTracker = new HeartBeatTracker();
serverThreadCounter = 0;
controlNodeThreadMap = new HashMap<String, ControlNodeThread>();
createDeadChunkServerDetectorThread();
startControlNode();
}
private void startControlNode()
{
try
{
serverSocket = new ServerSocket(Server.PORT);
System.out.println("Control node opened ServerSocket in port " + Server.PORT);
}
catch (IOException e)
{
System.out.println("Can not create server socket for Control Node.");
e.printStackTrace();
}
while(true)
{
try
{
Socket socket = serverSocket.accept();
System.out.println("Control node accepted a connection from " + socket.getInetAddress().getHostAddress());
ControlNodeThread controlNodeThread = new ControlNodeThread(socket, ++serverThreadCounter, heartBeatTracker);
controlNodeThreadMap.put(socket.getInetAddress().getHostAddress(), controlNodeThread);
controlNodeThread.start();
}
catch (IOException e)
{
System.out.println("Tried to accept connection. But failed.");
e.printStackTrace();
}
}
}
private void createDeadChunkServerDetectorThread()
{
chunkServerDetectorThread = new ChunkServerDetectorThread(this);
chunkServerDetectorThread.start();
}
protected HeartBeatTracker getHeartBeatTracker() {
return heartBeatTracker;
}
protected void sendMessagesToChunkServers(ArrayList<SendChunkCopyToAnotherChunksServer> msgs)
{
for(SendChunkCopyToAnotherChunksServer msg : msgs)
{
sendChunkCopyMessageToChunkServer(msg);
}
}
private void sendChunkCopyMessageToChunkServer(SendChunkCopyToAnotherChunksServer msg)
{
try {
Socket socket = new Socket(msg.getSendFrom(), ChunkServer.CHUNK_SERVER_SOCKET_PORT_FOR_CONTROL_NODE_SEND_CHUNK_MSG);
ObjectOutputStream objectOutputStreamWithCS = new ObjectOutputStream(socket.getOutputStream());
objectOutputStreamWithCS.writeObject(msg);
objectOutputStreamWithCS.flush();
Thread.sleep(500);
objectOutputStreamWithCS.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}