1 | package com.gs3.testGXT.server;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 |
|
---|
5 | import com.google.gwt.user.server.rpc.RemoteServiceServlet;
|
---|
6 | import com.gs3.testGXT.client.services.ProgressBarHandshakeService;
|
---|
7 | import com.gs3.testGXT.server.Greenstone.GWTFileQueue;
|
---|
8 |
|
---|
9 | @SuppressWarnings("serial")
|
---|
10 | public class ProgressBarHandshakeServiceImpl extends RemoteServiceServlet implements ProgressBarHandshakeService {
|
---|
11 |
|
---|
12 | private static String DONE = "Completed";
|
---|
13 | private static String STOP = "Cancelled";
|
---|
14 | private static String WAITING = "Non-Completed";
|
---|
15 | private static String PROGRESS_TOKEN = "Token";
|
---|
16 |
|
---|
17 | private final int CHUNK_SIZE = 150;
|
---|
18 | private final int SLIDING_WINDOW = 450;
|
---|
19 |
|
---|
20 | //the synchronized object to be used for keeping track of job progress/long polling
|
---|
21 | private Object monitor = GWTFileQueue.monitor;
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public LinkedList<String[]> shakeHands() {
|
---|
25 | // TODO Auto-generated method stub
|
---|
26 | synchronized (monitor) {
|
---|
27 | /** check that either the queue itself has contents,
|
---|
28 | * or that there is a currently active job in progress.
|
---|
29 | * The two part check is needed because items get culled
|
---|
30 | * from the queue before the job actually starts,
|
---|
31 | * rather than when the job is completed.
|
---|
32 | */
|
---|
33 | if(GWTFileQueue.files_in_queue != 0 || GWTFileQueue.isQueueBusy()) { //file hasn't finished copying
|
---|
34 | //System.err.println("Files in queue!);
|
---|
35 | GWTFileQueue.setAllowance(CHUNK_SIZE);
|
---|
36 | monitor.notify();
|
---|
37 | try {
|
---|
38 | monitor.wait(); //I think I have some sync issues: this should hopefully fix it
|
---|
39 | } catch (InterruptedException e) {
|
---|
40 | e.printStackTrace();
|
---|
41 | }
|
---|
42 | } else {
|
---|
43 | monitor.notify();
|
---|
44 | }
|
---|
45 |
|
---|
46 | int full_count = GWTFileQueue.job_size;
|
---|
47 | int done_count = GWTFileQueue.done_size;
|
---|
48 |
|
---|
49 | //ternary needed to account for the case where we get f(0)/f(0) => NaN - ie, empty files
|
---|
50 | double progress = full_count == 0 ? 0 : (float)done_count/(float)full_count;
|
---|
51 |
|
---|
52 | LinkedList<String[]> str = new LinkedList<String[]>();
|
---|
53 |
|
---|
54 | if(GWTFileQueue.files_in_queue == 0 && !GWTFileQueue.isQueueBusy()) {
|
---|
55 | System.err.println("Zero files in queue!");
|
---|
56 | GWTFileQueue.job_size = GWTFileQueue.done_size = 0;
|
---|
57 | /*if(GWTFileQueue.wasCancelled()) {
|
---|
58 | System.err.println("Cancelled!");
|
---|
59 | str.add(new String[] {STOP, "" + 0});
|
---|
60 | }
|
---|
61 | else {*/
|
---|
62 | //TODO: fix
|
---|
63 | str.add(new String[] {DONE, "1.0"});
|
---|
64 | System.err.println("Done with files (Completed)");
|
---|
65 | //}
|
---|
66 | return str;
|
---|
67 | }
|
---|
68 | else {
|
---|
69 | str.add(new String[] {WAITING, "" + progress});
|
---|
70 | }
|
---|
71 |
|
---|
72 | //str -> [action] [source] (target)
|
---|
73 | //ie: copying myfile to mylocation
|
---|
74 | //ie: deleting myfile from myfolder
|
---|
75 | //ie: moving myfolder to myotherfolder
|
---|
76 | String token;
|
---|
77 | if((token = GWTFileQueue.currentJob) != null) {
|
---|
78 | token = "{0}%: " + token;
|
---|
79 | }
|
---|
80 | else
|
---|
81 | token = "Transfer in progress - {0}% Complete";
|
---|
82 |
|
---|
83 | str.add(new String[] {PROGRESS_TOKEN, token});
|
---|
84 |
|
---|
85 | System.err.println("Progress: " + progress);
|
---|
86 |
|
---|
87 | if(GWTFileQueue.files_in_queue != 0) {
|
---|
88 | GWTFileQueue.setAllowance(SLIDING_WINDOW);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return str;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|