use of services.moleculer.transporter.tcp.NodeDescriptor 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 services.moleculer.transporter.tcp.NodeDescriptor in project moleculer-java by moleculer-java.
the class TcpTransporterTest method createOnlineDescriptorWithInfo.
protected NodeDescriptor createOnlineDescriptorWithInfo(boolean local, String nodeID) {
Tree info = new Tree();
info.put("seq", 1);
info.put("port", 1);
info.put("hostname", nodeID);
info.putList("services").putMap("test");
return new NodeDescriptor(nodeID, true, local, info);
}
Aggregations