Search in sources :

Example 46 with Tree

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

Example 47 with Tree

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

Example 48 with Tree

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

Example 49 with Tree

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

Example 50 with Tree

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);
}
Also used : Tree(io.datatree.Tree)

Aggregations

Tree (io.datatree.Tree)60 FastBuildTree (services.moleculer.util.FastBuildTree)26 Test (org.junit.Test)12 NodeDescriptor (services.moleculer.transporter.tcp.NodeDescriptor)12 CheckedTree (services.moleculer.util.CheckedTree)9 TimeoutException (java.util.concurrent.TimeoutException)6 Promise (services.moleculer.Promise)6 CommonUtils.readTree (services.moleculer.util.CommonUtils.readTree)6 RemoteException (java.rmi.RemoteException)4 LinkedHashMap (java.util.LinkedHashMap)4 CallOptions (services.moleculer.context.CallOptions)4 Context (services.moleculer.context.Context)4 Annotation (java.lang.annotation.Annotation)3 HashSet (java.util.HashSet)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 NoSuchElementException (java.util.NoSuchElementException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ServiceBrokerConfig (services.moleculer.config.ServiceBrokerConfig)3 Action (services.moleculer.service.Action)3