-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
executable file
·90 lines (81 loc) · 2.26 KB
/
Client.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
/**class of the client
*
* @author Tiago Pais
*
*/
public class Client {
/** client that sends tasks to scheduler and waits for results
* @param args[0] ip:port
* @param args[1] file with tasks
*/
public static void main(String[] args) {
//parses ip and port
String [] direction= args[0].split(":");
List <String> tasks = new ArrayList<String>();
int id=0;
String result;
String [] parsedResult;
//interval to poll results from scheduler
final int POLLINT=5*1000;
try{
String line;
//open the file with the tasks
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[1])));
//start reading different tasks (each one on one line)
while((line=br.readLine())!=null){
//take 'sleep ' out of the instruction, only get the number
line=line.replace("sleep ", "");
//add them to list (id and time)
tasks.add(id+":"+line);
//update id
id++;
}
}catch(Exception e){
e.printStackTrace();
}
//establish connection with scheduler
try{
//create connection
Socket s= new Socket(direction[0],Integer.parseInt(direction[1]));
DataOutputStream o= new DataOutputStream(s.getOutputStream());
//send each task to scheduler
for(int k=0; k<tasks.size();k++){
o.writeUTF(tasks.get(k));
}
//say that there are no more tasks and end connection
o.writeUTF("END");
s.close();
//poll for results in a given interval
int completed=0;
while(completed<tasks.size()){
//wait interval
Thread.sleep(POLLINT);
//establish connection
s= new Socket(direction[0],Integer.parseInt(direction[1]));
DataInputStream i= new DataInputStream(s.getInputStream());
//get the responses
while(true){
//read result
result=i.readUTF();
//if there are no more results end
if(result.equals("END")){
break;
//else print result and update number of completed
}else {
parsedResult=result.split(":");
System.out.println("Task "+parsedResult[0]+": sleep "+ parsedResult[1]+" - "+ parsedResult[2]);
completed++;
}
}
//close connection
s.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
}