-
Notifications
You must be signed in to change notification settings - Fork 5
/
ActionVerticle.java
112 lines (97 loc) · 3.45 KB
/
ActionVerticle.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
package org.genericsystem.watch;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
public abstract class ActionVerticle extends AbstractVerticle {
private final String ip;
public ActionVerticle(String ip) {
this.ip = ip;
}
public String getIp() {
return ip;
}
public String getAction() {
throw new IllegalStateException("The getAction method must be overridden by extending classes.");
}
@Override
public void start() throws Exception {
vertx.eventBus().consumer(getAction(), message -> {
JsonObject task = new JsonObject((String) message.body());
if (DistributedVerticle.getMaxExecutions() <= DistributedVerticle.getExecutionsCount()) {
message.reply(Dispatcher.KO);
return;
}
DistributedVerticle.incrementExecutions();
message.reply(Dispatcher.OK);
vertx.executeBlocking(future -> {
download(future, task);
if (!future.failed())
handle(future, task);
else
throw new IllegalStateException("Impossible to download file " + task.getString(DistributedVerticle.FILENAME), future.cause());
}, res -> {
handleResult(res, task);
if (res.succeeded())
vertx.eventBus().send(Dispatcher.ADDRESS + ":updateState", new JsonObject().put(Dispatcher.TASK, task).put(Dispatcher.NEW_STATE, Dispatcher.FINISHED).encodePrettily());
else {
vertx.eventBus().send(Dispatcher.ADDRESS + ":updateState", new JsonObject().put(Dispatcher.TASK, task).put(Dispatcher.NEW_STATE, Dispatcher.ABORTED).encodePrettily());
System.out.println("Task aborted, cause: " + res.cause().getMessage());
}
DistributedVerticle.decrementExecutions();
});
});
}
protected abstract void handle(Future<Object> future, JsonObject task);
protected abstract void handleResult(AsyncResult<Object> res, JsonObject task);
private void download(Future<Object> future, JsonObject task) {
String fileName = task.getString(DistributedVerticle.FILENAME);
File file = new File(DistributedVerticle.BASE_PATH + fileName);
if (!file.exists()) {
BlockingQueue<byte[]> blockingQueue = new ArrayBlockingQueue<>(1);
HttpClient httpClient = vertx.createHttpClient().getNow(8084, task.getString(DistributedVerticle.IP), fileName, resp -> resp.bodyHandler(body -> {
try {
blockingQueue.put(body.getBytes());
} catch (InterruptedException e) {
future.fail(e);
return;
}
}));
httpClient.close();
byte[] bytes;
try {
bytes = blockingQueue.take();
} catch (InterruptedException e) {
e.printStackTrace();
future.fail(e);
return;
}
FileOutputStream fos;
try {
file.getParentFile().mkdirs();
fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
future.fail(e);
return;
}
} else {
System.out.println("File : " + fileName + " is already downloaded");
}
}
public void addTask(String fileName, String ip, String type) {
JsonObject task = new JsonObject().put(Dispatcher.STATE, Dispatcher.TODO)
.put(DistributedVerticle.IP, ip)
.put(DistributedVerticle.FILENAME, fileName)
.put(DistributedVerticle.TYPE, type);
vertx.eventBus().publish(Dispatcher.ADDRESS + ":add", task.encodePrettily());
}
}