Search in sources :

Example 16 with NodeDescriptor

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);
    }
}
Also used : NodeDescriptor(services.moleculer.transporter.tcp.NodeDescriptor) FastBuildTree(services.moleculer.util.FastBuildTree) Tree(io.datatree.Tree)

Example 17 with NodeDescriptor

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);
}
Also used : NodeDescriptor(services.moleculer.transporter.tcp.NodeDescriptor) Tree(io.datatree.Tree)

Aggregations

NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)17 Tree (io.datatree.Tree)12 FastBuildTree (services.moleculer.util.FastBuildTree)9 CommonUtils.readTree (services.moleculer.util.CommonUtils.readTree)5 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Color (java.awt.Color)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 Test (org.junit.Test)1 ServiceBroker (services.moleculer.ServiceBroker)1 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)1 RemoteAddress (services.moleculer.transporter.tcp.RemoteAddress)1 TcpReader (services.moleculer.transporter.tcp.TcpReader)1 TcpWriter (services.moleculer.transporter.tcp.TcpWriter)1 UDPLocator (services.moleculer.transporter.tcp.UDPLocator)1