communication between verticles
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
eclipse.preferences.version=1
|
eclipse.preferences.version=1
|
||||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
|
||||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=23
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=23
|
||||||
|
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||||
|
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
|
||||||
org.eclipse.jdt.core.compiler.release=disabled
|
org.eclipse.jdt.core.compiler.release=enabled
|
||||||
org.eclipse.jdt.core.compiler.source=1.8
|
org.eclipse.jdt.core.compiler.source=23
|
||||||
|
|||||||
@@ -10,5 +10,10 @@
|
|||||||
<artifactId>vertx-core</artifactId>
|
<artifactId>vertx-core</artifactId>
|
||||||
<version>4.5.13</version>
|
<version>4.5.13</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.12.1</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package net.miarma.contaminus;
|
||||||
|
|
||||||
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
|
||||||
|
public class ConsumerVerticle1 extends AbstractVerticle {
|
||||||
|
@Override
|
||||||
|
public void start(Promise<Void> startPromise) {
|
||||||
|
getVertx().eventBus().consumer("__addr_ConsumerBox", message -> {
|
||||||
|
String customMessage = (String) message.body();
|
||||||
|
System.out.println("[1] Mensaje recibido (" + message.address() + "): " + customMessage);
|
||||||
|
String replyMessage = "[1] Mensaje recibido: \"" + message.body().toString() + "\"";
|
||||||
|
message.reply(replyMessage);
|
||||||
|
});
|
||||||
|
startPromise.complete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopPromise) throws Exception {
|
||||||
|
super.stop(stopPromise);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package net.miarma.contaminus;
|
||||||
|
|
||||||
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
|
||||||
|
public class ConsumerVerticle2 extends AbstractVerticle {
|
||||||
|
@Override
|
||||||
|
public void start(Promise<Void> startPromise) {
|
||||||
|
getVertx().eventBus().consumer("__addr_ConsumerBox", message -> {
|
||||||
|
String customMessage = (String) message.body();
|
||||||
|
System.out.println("[2] Mensaje recibido (" + message.address() + "): " + customMessage);
|
||||||
|
String replyMessage = "[2] Mensaje recibido: \"" + message.body().toString() + "\"";
|
||||||
|
message.reply(replyMessage);
|
||||||
|
});
|
||||||
|
startPromise.complete();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopPromise) throws Exception {
|
||||||
|
super.stop(stopPromise);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +1,59 @@
|
|||||||
package net.miarma.contaminus;
|
package net.miarma.contaminus;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
import io.vertx.core.AbstractVerticle;
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.DeploymentOptions;
|
||||||
import io.vertx.core.Promise;
|
import io.vertx.core.Promise;
|
||||||
|
import io.vertx.core.json.JsonObject;
|
||||||
|
import net.miarma.contaminus.p2p.VerticleConfig;
|
||||||
|
|
||||||
public class Main extends AbstractVerticle {
|
public class Main extends AbstractVerticle {
|
||||||
|
|
||||||
|
private Gson gson;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Promise<Void> startFuture) {
|
public void start(Promise<Void> promise) {
|
||||||
vertx.createHttpServer().requestHandler(r -> {
|
gson = new Gson();
|
||||||
String file = r.path().equals("/") ? "index.html" : r.path().substring(1);
|
|
||||||
r.response().sendFile("webroot/" + file);
|
VerticleConfig config = new VerticleConfig();
|
||||||
}).listen(80, result -> {
|
config.setNum(2);
|
||||||
|
config.setName("[MESSAGE] ");
|
||||||
|
|
||||||
|
DeploymentOptions options = new DeploymentOptions();
|
||||||
|
options.setConfig(new JsonObject(gson.toJson(config)));
|
||||||
|
|
||||||
|
String consumer1Name = ConsumerVerticle1.class.getName();
|
||||||
|
getVertx().deployVerticle(consumer1Name, options, result -> {
|
||||||
if (result.succeeded()) {
|
if (result.succeeded()) {
|
||||||
startFuture.complete();
|
System.out.println(consumer1Name + " (" + result.result() + ") ha sido desplegado correctamente");
|
||||||
} else {
|
} else {
|
||||||
startFuture.fail(result.cause());
|
result.cause().printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
String consumer2Name = ConsumerVerticle1.class.getName();
|
||||||
|
getVertx().deployVerticle(consumer2Name, options, result -> {
|
||||||
|
if (result.succeeded()) {
|
||||||
|
System.out.println(consumer2Name + " (" + result.result() + ") ha sido desplegado correctamente");
|
||||||
|
} else {
|
||||||
|
result.cause().printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
String senderName = SenderVerticle.class.getName();
|
||||||
|
getVertx().deployVerticle(senderName, options, result -> {
|
||||||
|
if (result.succeeded()) {
|
||||||
|
System.out.println(senderName + " (" + result.result() + ") ha sido desplegado correctamente");
|
||||||
|
} else {
|
||||||
|
result.cause().printStackTrace();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop(Promise<Void> stopFuture) throws Exception {
|
||||||
|
getVertx().undeploy(ConsumerVerticle1.class.getName());
|
||||||
|
getVertx().undeploy(SenderVerticle.class.getName());
|
||||||
|
super.stop(stopFuture);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package net.miarma.contaminus;
|
||||||
|
|
||||||
|
import io.vertx.core.AbstractVerticle;
|
||||||
|
import io.vertx.core.Promise;
|
||||||
|
import io.vertx.core.eventbus.EventBus;
|
||||||
|
import io.vertx.core.eventbus.Message;
|
||||||
|
|
||||||
|
public class SenderVerticle extends AbstractVerticle {
|
||||||
|
String verticleID = "";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start(Promise<Void> promise) {
|
||||||
|
EventBus eventBus = vertx.eventBus();
|
||||||
|
|
||||||
|
vertx.setPeriodic(4000, _id -> {
|
||||||
|
String message = "Hola papu";
|
||||||
|
eventBus.request("__addr_ConsumerBox", message, reply -> {
|
||||||
|
Message<Object> res = reply.result();
|
||||||
|
verticleID = res.address();
|
||||||
|
if(reply.succeeded()) {
|
||||||
|
String replyMsg = (String) res.body();
|
||||||
|
System.out.println("Respuesta recibida (" + res.address() + "): " + replyMsg + "\n\n\n");
|
||||||
|
} else {
|
||||||
|
System.out.println("No ha habido respuesta");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package net.miarma.contaminus.p2p;
|
||||||
|
|
||||||
|
public class VerticleConfig {
|
||||||
|
private String name;
|
||||||
|
private int num;
|
||||||
|
private boolean isDup;
|
||||||
|
|
||||||
|
public VerticleConfig() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public VerticleConfig(String name, int num, boolean isDup) {
|
||||||
|
this.isDup = isDup;
|
||||||
|
this.num = num;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNum() {
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNum(int num) {
|
||||||
|
this.num = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDup() {
|
||||||
|
return isDup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDup(boolean isDup) {
|
||||||
|
this.isDup = isDup;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
2
backend/vertx/target/classes/.gitignore
vendored
2
backend/vertx/target/classes/.gitignore
vendored
@@ -1,2 +1,2 @@
|
|||||||
/META-INF/
|
|
||||||
/net/
|
/net/
|
||||||
|
/META-INF/
|
||||||
|
|||||||
Reference in New Issue
Block a user