This repository has been archived by the owner on Feb 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
ParallelDeviceFetcher.java
117 lines (93 loc) · 3.96 KB
/
ParallelDeviceFetcher.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
package io.particle.android.sdk.cloud;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import javax.annotation.ParametersAreNonnullByDefault;
import io.particle.android.sdk.cloud.ApiDefs.CloudApi;
import io.particle.android.sdk.cloud.Responses.Models;
import io.particle.android.sdk.cloud.Responses.Models.CompleteDevice;
import io.particle.android.sdk.cloud.Responses.Models.SimpleDevice;
import static io.particle.android.sdk.utils.Py.list;
/**
* Does parallel fetching of {@link Models.CompleteDevice}
*
* FIXME: review this solution
*/
@ParametersAreNonnullByDefault
class ParallelDeviceFetcher {
static class DeviceFetchResult {
final String deviceId;
/**
* Will be null if device could not be fetched.
*/
@Nullable
final CompleteDevice fetchedDevice;
DeviceFetchResult(String deviceId, @Nullable CompleteDevice fetchedDevice) {
this.deviceId = deviceId;
this.fetchedDevice = fetchedDevice;
}
}
// FIXME: insert slower API here
static ParallelDeviceFetcher newFetcherUsingExecutor(ExecutorService executor) {
return new ParallelDeviceFetcher(executor);
}
private final ExecutorService executor;
private ParallelDeviceFetcher(ExecutorService executor) {
this.executor = executor;
}
// FIXME: ugh, so lame. Figure out the smarter way to do per-device fetch timeouts
// without having to resort to two Retrofit API instances. look into jsr166 ForkJoinPool
// or similar (since we can't use the Java 7 one until API 21...)
/**
* Fetch the devices in parallel. Ordering of results not guaranteed to be preserved or
* respected in any way.
*/
@CheckResult
Collection<DeviceFetchResult> fetchDevicesInParallel(Collection<SimpleDevice> simpleDevices,
final CloudApi cloudApi,
int perDeviceTimeoutInSeconds) {
// Assemble the list of Callables
List<Callable<DeviceFetchResult>> callables = list();
for (final SimpleDevice device : simpleDevices) {
callables.add(() -> getDevice(cloudApi, device.id));
}
// Submit callables, receive list of Futures. invokeAll() will block until they finish.
List<Future<DeviceFetchResult>> futures = list();
try {
long timeout = perDeviceTimeoutInSeconds * simpleDevices.size();
futures = executor.invokeAll(callables, timeout, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// FIXME: think about what to do in this implausible(?) scenario, or how to avoid it.
e.printStackTrace();
}
// turn the results into something usable.
List<DeviceFetchResult> results = list();
for (Future<DeviceFetchResult> future : futures) {
try {
DeviceFetchResult result = future.get();
if (result != null) {
results.add(result);
}
} catch (InterruptedException | ExecutionException e) {
// FIXME: see above; think more about what to do in this scenario, or how to avoid it.
e.printStackTrace();
}
}
return results;
}
private DeviceFetchResult getDevice(CloudApi cloudApi, String deviceID) {
CompleteDevice device = null;
try {
device = cloudApi.getDevice(deviceID);
} catch (Exception e) {
// doesn't matter why it fails, just don't abort the whole operation because of it
}
return new DeviceFetchResult(deviceID, device);
}
}