-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterServerThread.java
More file actions
122 lines (106 loc) · 4.5 KB
/
Copy pathMasterServerThread.java
File metadata and controls
122 lines (106 loc) · 4.5 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
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
// sends the Tasks from a certain client to the slave with least connections and returns the completed task to the client
public class MasterServerThread implements Runnable {
private Socket clientSocket;
private Thread task;
private ArrayList<Socket> slaveSocketsList = new ArrayList<>();
public MasterServerThread(Socket clientSocket, ArrayList<Socket> slaveSocketsList) {
this.clientSocket = clientSocket;
this.slaveSocketsList = slaveSocketsList;
}
@Override
public void run() {
try {
ObjectOutputStream outputToClient = new ObjectOutputStream(clientSocket.getOutputStream()); // used to send
// things
// to client
ObjectInputStream inputFromClient = new ObjectInputStream(clientSocket.getInputStream()); // used to get
// things from
// client
boolean run = true;
while (run) {
System.out.println("running"); // TODO for testing purposes
// the following line waits until the client sends a task
task = (Thread) inputFromClient.readObject();
System.out.println("read task"); // TODO for testing purposes
int slaveNumWithLeastCon = findLeastConnection(); // 'a' will be the task with fewest tasks
ObjectOutputStream outputToSlave = new ObjectOutputStream(
slaveSocketsList.get(slaveNumWithLeastCon).getOutputStream()); // used to send things
// to slave #'a'
System.out.println("created OOS for slave with least con"); // TODO for testing purposes
ObjectInputStream inputFromSlave = new ObjectInputStream(
slaveSocketsList.get(slaveNumWithLeastCon).getInputStream()); // used to get things from
// slave #'a'
System.out.println("created OIS for slave with least con"); // TODO for testing purposes
outputToSlave.writeObject(task); // send the task to the slave
System.out.println("wrote to slave"); // TODO for testing purposes
// TODO the following needs to be in a separate thread. It will block waiting
// for the slave to send it the completed task
task = (Thread) inputFromSlave.readObject(); // gets the completed task from the slave
System.out.println("got input back from slave"); // TODO for testing purposes
outputToClient.writeObject(task); // sends the completed task back to the client
System.out.println("Wrote to client !!---!!");
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// the following try/catch block closes resource leaks
try {
for (int i = 0; i < slaveSocketsList.size(); i++) {
slaveSocketsList.get(i).close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// Finds the slave with the least connections
public int findLeastConnection() throws Exception {
int x = 0; // the number of the slave with fewest tasks
int min = Integer.MAX_VALUE; // the fewest number of tasks
int temp = -1;
for (int i = 0; i < slaveSocketsList.size() && min != 0; i++) // for each slave
{
ObjectOutputStream out = new ObjectOutputStream(slaveSocketsList.get(i).getOutputStream()); // used to send
// to slave
ObjectInputStream in = new ObjectInputStream(slaveSocketsList.get(i).getInputStream()); // used to get from
// slave
out.writeObject(0); // sending an int tells the slave to tell us how many tasks it has
System.out.println("block"); // TODO for testing purposes
// the following while loop does nothing until the slave tells us how many tasks
// it has
boolean a = true;
while (a) {
System.out.print("."); // TODO for testing purposes
try {
temp = (int) in.readObject();
a = false;
} catch (EOFException e) {
// intentionally empty. although, maybe this should sleep for a short time.
}
}
System.out.println("block done"); // TODO for testing purposes
// if temp is -1, something has gone wrong
if (temp == -1)
throw new Exception();
if (min > temp) // if this slave has fewer task than any previous slave
{
min = temp; // our number of tasks is now the fewest
x = i; // our slave is now the one with the fewest tasks
}
}
System.out.println("flc done"); // TODO for testing purposes
return x;
}
}