-
Notifications
You must be signed in to change notification settings - Fork 5
/
DistributedVerticle.java
214 lines (197 loc) · 8.36 KB
/
DistributedVerticle.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
package org.genericsystem.watch.gamma;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import org.genericsystem.cv.PdfToPngConverter;
import org.genericsystem.watch.beta.RoundRobin;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.EventBusOptions;
import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.cluster.ClusterManager;
import io.vertx.spi.cluster.hazelcast.HazelcastClusterManager;
public class DistributedVerticle extends AbstractVerticle {
private static final long REGISTER_PERIODICITY = 1000;
private static final long ROUNDROBIN_PERIODICITY = 5000;
private static final String PUBLIC_ADDRESS = "publicAddress";
public static final String BASE_PATH = System.getenv("HOME") + "/git/genericsystem2015/gs-cv/";
private static final String FILENAME = "filename";
private static final String OK = "OK";
private static final String KO = "KO";
private static final String ID = "ID";
private static final String TYPE = "type";
private static final String IP = "IP";
private final String PRIVATE_ADDRESS;
private final String PRIVATE_PATH;
private final String DOWNLOAD = "download";
private final String PDF_TO_PNG = "pdfToPng";
private static final DeliveryOptions TIMEOUT = new DeliveryOptions().setSendTimeout(500);
private final RoundRobin roundrobin = new RoundRobin();
private List<JsonObject> messages = new ArrayList<>();
private List<JsonObject> tasks = new ArrayList<>();
private final String ip;
private DistributedVerticle(String ip) {
this.ip = ip;
this.PRIVATE_ADDRESS = ip + ":" + hashCode();
this.PRIVATE_PATH = System.getenv("HOME") + "/copy/" + PRIVATE_ADDRESS + "/";
System.out.println("Ip : " + ip);
long id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image.pdf").put(IP, ip).put(TYPE, DOWNLOAD)));
id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image2.pdf").put(IP, ip).put(TYPE, DOWNLOAD)));
id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, "pdf/image3.pdf").put(IP, ip).put(TYPE, DOWNLOAD)));
}
@Override
public void start() throws Exception {
vertx.eventBus().consumer(PRIVATE_ADDRESS + ":" + DOWNLOAD, handler -> {
System.out.println("Receive from : " + (String) handler.body() + " on : " + PRIVATE_ADDRESS + " " + Thread.currentThread());
JsonObject task = new JsonObject((String) handler.body()).getJsonObject("task");
tasks.add(task);
handler.reply(OK);
String fileName = task.getString(FILENAME);
File file = new File(PRIVATE_PATH + fileName);
if (!file.exists()) {
vertx.executeBlocking(future -> download(future, fileName, task.getString(IP)), result -> {
if (result.failed()) {
System.out.println(result.cause());
throw new IllegalStateException(result.cause());
} else {
System.out.println("Download successful " + fileName);
long id = System.currentTimeMillis();
messages.add(new JsonObject().put(ID, id).put("task", new JsonObject().put(ID, id).put(FILENAME, fileName).put(IP, ip).put(TYPE, PDF_TO_PNG)));
}
System.out.println("Blocking task callback on thread : " + Thread.currentThread());
System.out.println("Task " + task.encodePrettily() + " is done, removing " + Thread.currentThread());
tasks.remove(task);
});
} else
System.out.println("File : " + fileName + " is already dowloaded");
});
vertx.eventBus().consumer(PRIVATE_ADDRESS + ":" + PDF_TO_PNG, handler -> {
System.out.println("PDF to PNG converter");
System.out.println("Receive from : " + (String) handler.body() + " on : " + PRIVATE_ADDRESS + " " + Thread.currentThread());
JsonObject task = new JsonObject((String) handler.body()).getJsonObject("task");
tasks.add(task);
handler.reply(OK);
String fileName = task.getString(FILENAME);
File file = new File(PRIVATE_PATH + fileName);
vertx.executeBlocking(future -> {
List<Path> createdPngs = PdfToPngConverter.convertPdfToImages(file, new File("png"));
future.complete(createdPngs);
}, res -> {
if (res.succeeded()) {
for (Path newPng : (List<Path>) res.result())
System.out.println("New PNG file : " + newPng);
System.out.println("Blocking task callback on thread : " + Thread.currentThread());
System.out.println("Task " + task.encodePrettily() + " is done, removing " + Thread.currentThread());
tasks.remove(task);
}
});
});
vertx.eventBus().consumer(PUBLIC_ADDRESS, message -> {
System.out.println(roundrobin);
roundrobin.register((String) message.body());
});
vertx.setPeriodic(REGISTER_PERIODICITY, h -> {
// System.out.println("Periodic publish : "+PRIVATE_ADDRESS+" " +
// Thread.currentThread());
vertx.eventBus().publish(PUBLIC_ADDRESS, PRIVATE_ADDRESS);
});
// vertx.setPeriodic(ROUNDROBIN_PERIODICITY, h -> {
// for (JsonObject json : new ArrayList<>(messages)) {
// String robin = roundrobin.getNextAddress();
// if (robin != null) {
// System.out.println("Periodic round robin from " + PRIVATE_ADDRESS + " to " + robin + " " + Thread.currentThread());
// vertx.eventBus().send(robin, json.encodePrettily(), TIMEOUT, replyHandler -> {
// if (replyHandler.failed())
// throw new IllegalStateException(replyHandler.cause());
// System.out.println("Receive response : " + replyHandler.result().body() + " " + Thread.currentThread());
// if (OK.equals(replyHandler.result().body()))
// messages.remove(json);
// });
// }
// }
// });
}
private <T> void download(Future<T> future, String fileName, String ip) {
// System.out.println("Execute blocking task : " +
// Thread.currentThread());
BlockingQueue<byte[]> blockingQueue = new ArrayBlockingQueue<>(1);
HttpClient httpClient = vertx.createHttpClient().getNow(8084, ip, fileName, resp -> resp.bodyHandler(body -> {
// System.out.println("get Bytes : " + Thread.currentThread());
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 file = new File(PRIVATE_PATH + fileName);
new File(PRIVATE_PATH + fileName.substring(0, fileName.lastIndexOf("/"))).mkdirs();
fos = new FileOutputStream(file);
fos.write(bytes);
fos.close();
} catch (IOException e) {
e.printStackTrace();
future.fail(e);
return;
}
// System.out.println("Blocking task complete on thread : " +
// Thread.currentThread());
future.complete();
}
public static void main(String[] args) {
ClusterManager mgr = new HazelcastClusterManager();
VertxOptions vertxOptions = new VertxOptions().setClustered(true).setClusterManager(mgr);
vertxOptions.setEventBusOptions(new EventBusOptions()).setClustered(true);
String ip = LocalNet.getIpAddress();
vertxOptions.setClusterHost(ip);
vertxOptions.setMaxWorkerExecuteTime(Long.MAX_VALUE);
Vertx.clusteredVertx(vertxOptions, res -> {
if (res.failed())
throw new IllegalStateException(res.cause());
Vertx vertx = res.result();
vertx.deployVerticle(new HttpServerVerticle(), complete -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
vertx.deployVerticle(new DistributedVerticle(ip), result -> {
if (complete.failed())
throw new IllegalStateException(complete.cause());
});
});
});
}
}