use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class Transporter method broadcastNodeConnected.
// --- INTERNAL MOLECULER EVENTS ---
protected void broadcastNodeConnected(Tree info, boolean reconnected) {
if (info != null) {
Tree msg = new Tree();
msg.putObject("node", info);
msg.put("reconnected", reconnected);
eventbus.broadcast("$node.connected", msg, null, true);
}
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class Transporter method broadcastNodeDisconnected.
protected void broadcastNodeDisconnected(Tree info, boolean unexpected) {
if (info != null) {
Tree msg = new Tree();
msg.putObject("node", info);
msg.put("unexpected", unexpected);
eventbus.broadcast("$node.disconnected", msg, null, true);
}
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class Transporter method broadcastNodeUpdated.
protected void broadcastNodeUpdated(Tree info) {
if (info != null) {
Tree msg = new Tree();
msg.putObject("node", info);
eventbus.broadcast("$node.updated", msg, null, true);
}
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class Transporter method updateNodeInfo.
protected void updateNodeInfo(String sender, Tree info) throws Exception {
boolean connected = false;
boolean reconnected = false;
boolean updated = false;
NodeDescriptor node = nodes.get(sender);
if (node == null) {
// New, unknown node
connected = true;
node = new NodeDescriptor(sender, preferHostname, false, info);
nodes.put(sender, node);
} else {
node.writeLock.lock();
try {
// Node is registered
if (node.seq == 0) {
// Node connected (it was offline)
connected = true;
node.markAsOnline(info);
} else {
// Try to update current node
Tree prevInfo = node.info;
boolean wasOnline = node.offlineSince == 0;
if (node.markAsOnline(info)) {
// Store new node info
if (prevInfo != null && wasOnline) {
Tree s1 = prevInfo.get("services");
if (s1 != null) {
Tree s2 = node.info.get("services");
if (s2 != null && s1.equals(s2)) {
// Service blocks are equal
return;
}
}
}
if (wasOnline) {
updated = true;
} else {
reconnected = true;
}
} else {
// New info block is older than ours
return;
}
}
} finally {
node.writeLock.unlock();
}
}
// Register actions and listeners
if (connected || reconnected || updated) {
if (updated) {
// Remove actions and listeners
registry.removeActions(sender);
eventbus.removeListeners(sender);
}
Tree services = info.get("services");
if (services != null && services.size() > 0) {
for (Tree service : services) {
// Register actions and listeners
registry.addActions(service);
eventbus.addListeners(service);
}
}
}
// Notify local listeners
if (updated) {
// Node updated
logger.info("Node \"" + sender + "\" updated.");
broadcastNodeUpdated(info);
} else if (connected || reconnected) {
// Node connected or reconnected
if (connected) {
logger.info("Node \"" + sender + "\" connected.");
} else {
logger.info("Node \"" + sender + "\" reconnected.");
}
broadcastNodeConnected(info, reconnected);
}
}
use of io.datatree.Tree in project moleculer-java by moleculer-java.
the class SerializerTest method check.
@Override
protected void check(String json, String key, String[] keys, int maxKeyLength) throws Exception {
json = json.replace('\'', '\"');
Tree v1 = new Tree(json, "JsonBuiltin");
byte[] bytes = serializer.write(v1);
Tree v2 = serializer.read(bytes);
String json2 = v2.toString(false);
assertJsonEquals(json, json2);
}
Aggregations