-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyClientA2.java
More file actions
139 lines (108 loc) · 4.2 KB
/
MyClientA2.java
File metadata and controls
139 lines (108 loc) · 4.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
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
import java.io.*;
import java.net.*;
public class MyClientA2 {
public static void main(String[] args) {
try{
// Creating a socket connection to the server using localhost and port number 50000
Socket mySocket = new Socket("localhost",50000);
// Creating an input stream reader to read data from the server
BufferedReader dout = new BufferedReader(new InputStreamReader(mySocket.getInputStream()));
// Creating an output stream to send data to the server
DataOutputStream dis = new DataOutputStream(mySocket.getOutputStream());
// Initiate the handshake with the server
initiateHandshake(dis, dout);
// Process jobs sent by the server
processJobs(dis, dout);
// Close the socket connection
mySocket.close();
}
catch (Exception e) {
// Print any exceptions that occur to the console
System.out.println(e);
}
}
private static void initiateHandshake(DataOutputStream dis, BufferedReader dout) throws IOException {
String str = ""; // response from the server
// Send "HELO" message to server
dis.write(("HELO\n").getBytes());
dis.flush();
// Read server's response
str = dout.readLine();
// Send "AUTH Adam" message to server
dis.write(("AUTH Adam\n").getBytes());
dis.flush();
str = dout.readLine();
}
private static void processJobs(DataOutputStream dis, BufferedReader dout) throws IOException {
String communicationStr = "";
String[] dataResult;
while(true){
// Request a new job from server
dis.write(("REDY\n").getBytes());
dis.flush();
communicationStr = dout.readLine();
// Split the server's response into an array of strings
dataResult = communicationStr.split("\\ ", 0);
// If server's response is "NONE", there are no more jobs, so break the loop
if(communicationStr.equals("NONE")){
dis.write(("QUIT\n").getBytes());
dis.close();
break;
}
// If the server has sent a job, handle it
if(dataResult[0].equals("JOBN")){
handleJob(dis, dout, communicationStr, dataResult);
}
}
}
private static void handleJob(DataOutputStream dis, BufferedReader dout, String jobDetails, String[] dataResult) throws IOException {
String communicationStr = "";
String server = "";
// Ask server for available servers that are capable of executing the job
dis.write(("GETS Avail " + dataResult[4] + " " + dataResult[5] + " " + dataResult[6] + "\n").getBytes());
communicationStr = dout.readLine();
dataResult = communicationStr.split("\\ ", 0);
// If no capable servers are available, ask the server for servers that are capable of executing the job
if(Integer.parseInt(dataResult[1]) == 0){
dis.write(("OK\n").getBytes());
dis.flush();
communicationStr = dout.readLine();
dataResult = jobDetails.split("\\ ", 0);
// Confirm that the server's response has been received
dis.write(("GETS Capable " + dataResult[4] + " " + dataResult[5] + " " + dataResult[6] + "\n").getBytes());
communicationStr = dout.readLine();
dataResult = communicationStr.split("\\ ", 0);
}
dis.write(("OK\n").getBytes());
dis.flush();
// Get the first server from the list of servers sent by the server
server = getFirstServer(dout, communicationStr, dataResult[1]);
// Confirm that the server's response has been received
dis.write(("OK\n").getBytes());
dis.flush();
communicationStr = dout.readLine();
// Schedule the job to the selected server
String[] jobResult = jobDetails.split("\\ ", 0);
dataResult = server.split("\\ ", 0);
dis.write(("SCHD " + jobResult[2] + " " + dataResult[0] + " " + dataResult[1] + "\n").getBytes());
communicationStr = dout.readLine();
}
private static String getFirstServer(BufferedReader dout, String str, String numOfServers) throws IOException {
int serverCount = Integer.parseInt(numOfServers);
String firstServer = "";
// Loop through the list of servers sent by the server
for(int i = 0; i < serverCount; i++){
if(i == 0){
str = dout.readLine();
firstServer = str;
i++;
}
// If there's more than one server in the list, continue reading the server's responses
if(serverCount != 1){
str = dout.readLine();
}
}
// Return the first server from the list
return firstServer;
}
}