-
Notifications
You must be signed in to change notification settings - Fork 13
/
ProgressActions.java
553 lines (485 loc) · 12.1 KB
/
ProgressActions.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package jrm.server.shared.actions;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.time.DurationFormatUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jrm.aui.progress.ProgressHandler;
import jrm.aui.progress.ProgressInputStream;
import jrm.misc.Log;
import jrm.server.shared.actions.ProgressActions.SetFullProgress.Data.PB;
public class ProgressActions implements ProgressHandler
{
private static final String HH_MM_SS = "HH:mm:ss";
private static final String S_OF_S = "%s / %s";
private static final String HH_MM_SS_OF_HH_MM_SS_NONE = "--:--:-- / --:--:--";
private ActionsMgr ws;
private final List<String> errors = new ArrayList<>();
/** The thread id offset. */
private final Map<Long, Integer> threadIdOffset = new HashMap<>();
/** The cancel. */
private boolean cancel = false;
private boolean canCancel = true;
private Gson gson;
static final class SetFullProgress
{
static final class Data
{
static final class PB
{
boolean visibility = false;
boolean stringPainted = false;
boolean indeterminate = false;
int max = 100;
int val = 0;
double perc = 0;
String msg = null;
String timeleft;
transient long startTime = 0; //NOSONAR
}
/** Current thread cnt */
int threadCnt = 1;
Boolean multipleSubInfos = false;
String[] infos = { null };
String[] subinfos = { null };
final PB pb1 = new PB();
final PB pb2 = new PB();
final PB pb3 = new PB();
}
static final String cmd = "Progress.setFullProgress"; //NOSONAR
final Data params;
SetFullProgress(Data data)
{
this.params = data;
}
}
static final class SetInfos
{
static final String cmd = "Progress.setInfos"; //NOSONAR
final Data params;
static final class Data
{
/** Current thread cnt */
int threadCnt = 1;
Boolean multipleSubInfos = false;
Data(int threadCnt, Boolean multipleSubInfos)
{
this.threadCnt = threadCnt;
this.multipleSubInfos = multipleSubInfos;
}
}
SetInfos(Data data)
{
this.params = data;
}
}
static final class CanCancel
{
static final String cmd = "Progress.canCancel"; //NOSONAR
final Data params;
static final class Data
{
final boolean canCancel;
Data(boolean canCancel)
{
this.canCancel = canCancel;
}
}
CanCancel(boolean canCancel)
{
this.params = new Data(canCancel);
}
}
static final class ClearInfos
{
private ClearInfos()
{
}
static final String cmd = "Progress.clearInfos"; //NOSONAR
}
static final class Open
{
private Open()
{
}
static final String cmd = "Progress"; //NOSONAR
}
static final class Close
{
static final String cmd = "Progress.close"; //NOSONAR
final Data params;
static final class Data
{
String[] errors = null;
public Data(List<String> errors)
{
this.errors = errors.toArray(String[]::new);
}
}
public Close(List<String> errors)
{
this.params = new Data(errors);
}
}
private final SetFullProgress.Data data = new SetFullProgress.Data();
public ProgressActions(ActionsMgr ws)
{
this.ws = ws;
this.gson = new GsonBuilder().excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT).create();
sendOpen();
}
private void sendOpen()
{
try
{
if (ws.isOpen())
ws.send(gson.toJson(new Open())); //NOSONAR
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
public void reload(ActionsMgr ws)
{
this.ws = ws;
sendOpen();
sendSetInfos();
sendSetProgress(0, true);
}
private synchronized void cleanup()
{
final var ct = Thread.currentThread();
if (!threadIdOffset.containsKey(ct.getId()))
return;
final var tg = ct.getThreadGroup(); //NOSONAR
if (threadIdOffset.size() == tg.activeCount())
return;
final var tl = new Thread[tg.activeCount()];
final int tl_count = tg.enumerate(tl, false);
final var itr = threadIdOffset.entrySet().iterator();
while (itr.hasNext())
{
final var e = itr.next();
if (!isOffsetExist(e, tl, tl_count))
{
data.infos[e.getValue()] = "";
if (data.infos.length == data.subinfos.length)
data.subinfos[e.getValue()] = "";
itr.remove();
}
}
}
private void sendSetProgress(final int pb, final boolean force)
{
try
{
if (pb == 1)
cleanup();
if (force)
ws.send(gson.toJson(new SetFullProgress(data)));
else if (!data.pb1.visibility && !data.pb2.visibility && !data.pb3.visibility)
ws.send(gson.toJson(new SetFullProgress(data)));
else if (pb == 1 && data.pb1.visibility && !data.pb1.indeterminate && data.pb1.val > 0 && data.pb1.max == data.pb1.val)
ws.send(gson.toJson(new SetFullProgress(data)));
else if (pb == 2 && data.pb2.visibility && !data.pb2.indeterminate && data.pb2.val > 0 && data.pb2.max == data.pb2.val)
ws.send(gson.toJson(new SetFullProgress(data)));
else if (pb == 3 && data.pb3.visibility && !data.pb3.indeterminate && data.pb3.val > 0 && data.pb3.max == data.pb3.val)
ws.send(gson.toJson(new SetFullProgress(data)));
else
ws.sendOptional(gson.toJson(new SetFullProgress(data)));
data.pb1.msg = null;
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
@Override
public synchronized void setInfos(int threadCnt, Boolean multipleSubInfos)
{
threadIdOffset.clear();
this.data.threadCnt = threadCnt <= 0 ? Runtime.getRuntime().availableProcessors() : threadCnt;
this.data.multipleSubInfos = multipleSubInfos;
this.data.infos = new String[this.data.threadCnt];
if(multipleSubInfos == null)
this.data.subinfos = new String[0];
else
this.data.subinfos = new String[multipleSubInfos.booleanValue() ? this.data.threadCnt : 1];
sendSetInfos();
}
private void sendSetInfos()
{
try
{
if (ws.isOpen())
{
ws.send(gson.toJson(new SetInfos(new SetInfos.Data(data.threadCnt, data.multipleSubInfos))));
}
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
@Override
public void clearInfos()
{
for (var i = 0; i < data.infos.length; i++)
data.infos[i] = null;
for (var i = 0; i < data.subinfos.length; i++)
data.subinfos[i] = null;
data.pb2.msg = null;
sendClearInfos();
}
private void sendClearInfos()
{
try
{
if (ws.isOpen())
ws.send(gson.toJson(new ClearInfos())); //NOSONAR
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
@Override
public void setProgress(String msg, Integer val, Integer max, String submsg)
{
int offset = getOffset();
if (msg != null)
data.infos[offset] = msg;
var force = false;
if (val != null)
{
if (val < 0 && data.pb1.visibility)
{
data.pb1.visibility = false;
}
else if (val >= 0 && !data.pb1.visibility)
{
data.pb1.visibility = true;
}
data.pb1.stringPainted = val != 0;
data.pb1.indeterminate = val == 0;
force = computeProgress(data.pb1, val, max, force);
showDuration(data.pb1, val);
}
if (submsg != null || (val != null && val == -1))
{
if (data.subinfos.length == 1)
data.subinfos[0] = submsg;
else if (data.subinfos.length > 1)
data.subinfos[offset] = submsg;
}
sendSetProgress(1, force);
}
@Override
public void setProgress2(String msg, Integer val, Integer max)
{
var force = false;
if (msg != null && val != null)
{
if (!data.pb2.visibility)
data.pb2.visibility = true;
data.pb2.stringPainted = true/*msg != null || val > 0*/;
data.pb2.msg = msg;
data.pb2.indeterminate = val == 0 && msg == null;
force = computeProgress(data.pb2, val, max, force);
showDuration(data.pb2, val);
}
else if (data.pb2.visibility)
data.pb2.visibility = false;
sendSetProgress(2, force);
}
@Override
public void setProgress3(String msg, Integer val, Integer max)
{
var force = false;
if (msg != null && val != null)
{
if (!data.pb3.visibility)
data.pb3.visibility = true;
data.pb3.stringPainted = true/*msg != null || val > 0*/;
data.pb3.msg = msg;
data.pb3.indeterminate = val == 0 && msg == null;
force = computeProgress(data.pb3, val, max, force);
showDuration(data.pb3, val);
}
else if (data.pb3.visibility)
data.pb3.visibility = false;
sendSetProgress(3, force);
}
/**
* @param val
* @param max
* @param force
* @return
*/
private boolean computeProgress(final PB pb, final Integer val, final Integer max, boolean force)
{
if (max != null)
pb.max = max;
if (val >= 0)
pb.val = val;
if (val == 0)
pb.startTime = System.currentTimeMillis();
if (pb.val >= 0 && pb.max > 0)
{
final var perc = pb.val * 100.0f / pb.max;
force = (int) pb.perc != (int) perc;
pb.perc = perc;
}
return force;
}
/**
* @return
*/
private synchronized int getOffset()
{
if (!threadIdOffset.containsKey(Thread.currentThread().getId()))
{
if (threadIdOffset.size() < data.threadCnt)
threadIdOffset.put(Thread.currentThread().getId(), threadIdOffset.size());
else
{
final var tg = Thread.currentThread().getThreadGroup(); //NOSONAR
final var tl = new Thread[tg.activeCount()];
final var tl_count = tg.enumerate(tl, false);
if (!isOffsetFound(tl, tl_count))
threadIdOffset.put(Thread.currentThread().getId(), 0);
}
}
return threadIdOffset.get(Thread.currentThread().getId());
}
/**
* @param tl
* @param tl_count
* @return
*/
private boolean isOffsetFound(final Thread[] tl, final int tl_count)
{
var found = false;
for (final var entry : threadIdOffset.entrySet())
{
if (!isOffsetExist(entry, tl, tl_count))
{
threadIdOffset.remove(entry.getKey());
threadIdOffset.put(Thread.currentThread().getId(), entry.getValue());
found = true;
break;
}
}
return found;
}
/**
* @param entry
* @param tl
* @param tl_count
* @return
*/
private boolean isOffsetExist(final Entry<Long, Integer> entry, final Thread[] tl, final int tl_count)
{
var exists = false;
for (var i = 0; i < tl_count; i++)
{
if (entry.getKey() == tl[i].getId())
{
exists = true;
break;
}
}
return exists;
}
/**
* @param val
*/
private void showDuration(PB pb, int val)
{
if (val > 0)
{
if (pb.msg == null)
pb.msg = String.format("%.02f%%", pb.perc);
final String left = DurationFormatUtils.formatDuration((System.currentTimeMillis() - pb.startTime) * (pb.max - val) / val, HH_MM_SS); //$NON-NLS-1$
final String total = DurationFormatUtils.formatDuration((System.currentTimeMillis() - pb.startTime) * pb.max / val, HH_MM_SS); //$NON-NLS-1$
pb.timeleft = String.format(S_OF_S, left, total); //$NON-NLS-1$
}
else
pb.timeleft = HH_MM_SS_OF_HH_MM_SS_NONE; //$NON-NLS-1$
}
@Override
public int getValue()
{
return data.pb1.val;
}
@Override
public int getValue2()
{
return data.pb2.val;
}
@Override
public int getValue3()
{
return data.pb3.val;
}
@Override
public boolean isCancel()
{
return cancel;
}
@Override
public void cancel()
{
cancel = true;
}
@Override
public InputStream getInputStream(InputStream in, Integer len)
{
return new ProgressInputStream(in, len, this);
}
@Override
public void close()
{
try
{
if (ws.isOpen())
ws.send(gson.toJson(new Close(errors)));
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
public boolean canCancel()
{
return canCancel;
}
public void canCancel(boolean canCancel)
{
this.canCancel = canCancel;
sendCanCancel();
}
private void sendCanCancel()
{
try
{
if (ws.isOpen())
ws.send(gson.toJson(new CanCancel(canCancel)));
}
catch (IOException e)
{
Log.err(e.getMessage(), e);
}
}
@Override
public void addError(String error)
{
errors.add(error);
}
}