forked from walidbosso/Java_Task_Scheduling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskSchedulerServer.java
More file actions
106 lines (80 loc) · 3.2 KB
/
TaskSchedulerServer.java
File metadata and controls
106 lines (80 loc) · 3.2 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
package Classes;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.Socket;
import java.rmi.Naming;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TaskSchedulerServer {
private static final int NUM_WORKER_THREADS = 8;
private static ExecutorService threadPool = Executors.newFixedThreadPool(NUM_WORKER_THREADS);
public static Taskqueue taskQueue = new Taskqueue();
static ArrayList<TaskResult> BD = new ArrayList<>();
public static List<Integer> slavesPorts = new ArrayList<Integer>();
public static List<String> slavesIP = new ArrayList<String>();
public static List<Socket> sockets = new ArrayList<Socket>();
public static File file;
// Constuct
public TaskSchedulerServer() {
threadPool = Executors.newFixedThreadPool(NUM_WORKER_THREADS);
taskQueue = new Taskqueue();
}
public static void start() throws InterruptedException {
// Start worker threads to process tasks from the queue
for (int i = 0; i < NUM_WORKER_THREADS; i++) {
threadPool.submit(new WorkerThread(taskQueue, BD));
}
}
// ------------ read master comfig file
public static void readConfigFile() {
BufferedReader bufferedReader = null;
String line = "";
if (file.exists()) {
try {
bufferedReader = new BufferedReader(new FileReader(file));
System.out.println("start reading ");
while ((line = bufferedReader.readLine()) != null) {
String[] row = line.split(";");
if ("SlaveIp".equals(row[0])) {
slavesIP.add(row[1]);
} else {
System.exit(-1);
}
line = bufferedReader.readLine();
row = line.split(";");
if ("SlavePort".equals(row[0])) {
slavesPorts.add(Integer.parseInt(row[1]));
} else {
System.exit(-1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("file doesn't exist");
System.exit(-1);
}
}
public static void main(String[] args) throws InterruptedException {
try {
TaskSchedulerInterface skeleton = new TaskScheduler(taskQueue, BD);
Naming.rebind("rmi://localhost:13190/task", skeleton);
System.out.println("Server is ready ...");
String path = "C:\\Users\\User\\Desktop\\";
path += args[0];
file = new File(path);
readConfigFile();
for (int i = 0; i < slavesPorts.size(); i++) {
sockets.add(new Socket(slavesIP.get(i) , slavesPorts.get(i)));
System.out.println(slavesIP.get(i)+" "+slavesPorts.get(i));
}
start();
} catch (Exception e) {
System.out.println(e);
}
}
}