-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultsLocal.java
57 lines (52 loc) · 1.43 KB
/
ResultsLocal.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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.*;
import java.util.*;
/** Class for the thread that returns result to client if we use local workers
*
* @author Tiago Pais
*/
public class ResultsLocal implements Runnable{
private int numTasks,completed=0;
private ServerSocket ss;
private List <String> results;
/**Constructor
*
* @param ss Server socket to accept requests
* @param numTasks total number of tasks to perform
* @param results list where to get the results
*/
public ResultsLocal(ServerSocket ss, int numTasks, List <String> results){
this.numTasks=numTasks;
this.ss=ss;
this.results=results;
}
/**Method that runs thread
*
*/
public void run(){
//until returning the result for all the tasks
while(completed<numTasks){
try{
//accept connection to client
Socket s =ss.accept();
DataOutputStream o= new DataOutputStream(s.getOutputStream());
//while we have results to return (synchronized list to avoid conflicts)
while(!results.isEmpty()){
//write them
o.writeUTF(results.get(0));
//remove them from list - New results are added to the end, so position 1 doesn't change
results.remove(0);
//update completed tasks
completed++;
}
//let client know that there aren't more results
o.writeUTF("END");
//close connection
s.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}