replicatedtree.java

来自「JGRoups源码」· Java 代码 · 共 1,098 行 · 第 1/3 页

JAVA
1,098 行
字号
     * @param fqn The fully qualified name of the node     * @return Node The node at fqn     */    Node findNode(String fqn) {        StringHolder sh=new StringHolder();        Node n=findParentNode(fqn, sh, false);        String child_name=sh.getValue();        if(fqn == null || fqn.equals(SEPARATOR) || "".equals(fqn))            return root;        if(n == null || child_name == null)            return null;        else            return n.getChild(child_name);    }    void notifyNodeAdded(String fqn) {        for(int i=0; i < listeners.size(); i++)            ((ReplicatedTreeListener)listeners.elementAt(i)).nodeAdded(fqn);    }    void notifyNodeRemoved(String fqn) {        for(int i=0; i < listeners.size(); i++)            ((ReplicatedTreeListener)listeners.elementAt(i)).nodeRemoved(fqn);    }    void notifyNodeModified(String fqn) {        for(int i=0; i < listeners.size(); i++)            ((ReplicatedTreeListener)listeners.elementAt(i)).nodeModified(fqn);    }    void notifyViewChange(View v) {        for(int i=0; i < listeners.size(); i++)            ((ReplicatedTreeListener)listeners.elementAt(i)).viewChange(v);    }    /** Generates NodeAdded notifications for all nodes of the tree. This is called whenever the tree is     initially retrieved (state transfer) */    void notifyAllNodesCreated(Node curr) {        Node n;        Map children;        if(curr == null) return;        notifyNodeAdded(curr.fqn);        if((children=curr.getChildren()) != null) {            for(Iterator it=children.values().iterator(); it.hasNext();) {                n=(Node)it.next();                notifyAllNodesCreated(n);            }        }    }    public static class Node implements Serializable {        String name=null;     // relative name (e.g. "Security")        String fqn=null;      // fully qualified name (e.g. "/federations/fed1/servers/Security")        Node parent=null;   // parent node        TreeMap children=null; // keys: child name, value: Node        HashMap data=null;     // data for current node        private static final long serialVersionUID = -3077676554440038890L;        // Address       creator=null;  // member that created this node (needed ?)        private Node(String child_name, String fqn, Node parent, HashMap data) {            name=child_name;            this.fqn=fqn;            this.parent=parent;            if(data != null) this.data=(HashMap)data.clone();        }        private Node(String child_name, String fqn, Node parent, String key, Object value) {            name=child_name;            this.fqn=fqn;            this.parent=parent;            if(data == null) data=new HashMap();            data.put(key, value);        }        void setData(Map data) {            if(data == null) return;            if(this.data == null)                this.data=new HashMap();            this.data.putAll(data);        }        void setData(String key, Object value) {            if(this.data == null)                this.data=new HashMap();            this.data.put(key, value);        }        HashMap getData() {            return data;        }        Object getData(String key) {            return data != null? data.get(key) : null;        }        boolean childExists(String child_name) {            if(child_name == null) return false;            return children != null && children.containsKey(child_name);        }        Node createChild(String child_name, String fqn, Node parent, HashMap data) {            Node child=null;            if(child_name == null) return null;            if(children == null) children=new TreeMap();            child=(Node)children.get(child_name);            if(child != null)                child.setData(data);            else {                child=new Node(child_name, fqn, parent, data);                children.put(child_name, child);            }            return child;        }        Node createChild(String child_name, String fqn, Node parent, String key, Object value) {            Node child=null;            if(child_name == null) return null;            if(children == null) children=new TreeMap();            child=(Node)children.get(child_name);            if(child != null)                child.setData(key, value);            else {                child=new Node(child_name, fqn, parent, key, value);                children.put(child_name, child);            }            return child;        }        Node getChild(String child_name) {            return child_name == null? null : children == null? null : (Node)children.get(child_name);        }        Map getChildren() {            return children;        }        void removeData(String key) {            if(data != null)                data.remove(key);        }        void removeData() {            if(data != null)                data.clear();        }        void removeChild(String child_name, String fqn) {            if(child_name != null && children != null && children.containsKey(child_name)) {                children.remove(child_name);            }        }        void removeAll() {            if(children != null)                children.clear();        }        void print(StringBuffer sb, int indent) {            printIndent(sb, indent);            sb.append(SEPARATOR).append(name);            if(children != null && children.size() > 0) {                Collection values=children.values();                for(Iterator it=values.iterator(); it.hasNext();) {                    sb.append('\n');                    ((Node)it.next()).print(sb, indent + INDENT);                }            }        }        void printIndent(StringBuffer sb, int indent) {            if(sb != null) {                for(int i=0; i < indent; i++)                    sb.append(' ');            }        }        public String toString() {            StringBuffer sb=new StringBuffer();            if(name != null) sb.append("\nname=" + name);            if(fqn != null) sb.append("\nfqn=" + fqn);            if(data != null) sb.append("\ndata=" + data);            return sb.toString();        }        public Object clone() throws CloneNotSupportedException {            Node n=new Node(name, fqn, parent != null? (Node)parent.clone() : null, data);            if(children != null) n.children=(TreeMap)children.clone();            return n;        }    }    private static class StringHolder {        String s=null;        private StringHolder() {        }        void setValue(String s) {            this.s=s;        }        String getValue() {            return s;        }    }    /**     * Class used to multicast add(), remove() and set() methods to all members.     */    private static class Request implements Serializable {        static final int PUT=1;        static final int REMOVE=2;        int type=0;        String fqn=null;        String key=null;        Object value=null;        HashMap data=null;        private static final long serialVersionUID = 7772753222127676782L;        private Request(int type, String fqn) {            this.type=type;            this.fqn=fqn;        }        private Request(int type, String fqn, HashMap data) {            this(type, fqn);            this.data=data;        }        private Request(int type, String fqn, String key) {            this(type, fqn);            this.key=key;        }        private Request(int type, String fqn, String key, Object value) {            this(type, fqn);            this.key=key;            this.value=value;        }        public String toString() {            StringBuffer sb=new StringBuffer();            sb.append(type2String(type)).append(" (");            if(fqn != null) sb.append(" fqn=" + fqn);            switch(type) {                case PUT:                    if(data != null) sb.append(", data=" + data);                    if(key != null) sb.append(", key=" + key);                    if(value != null) sb.append(", value=" + value);                    break;                case REMOVE:                    if(key != null) sb.append(", key=" + key);                    break;                default:                    break;            }            sb.append(')');            return sb.toString();        }        static String type2String(int t) {            switch(t) {                case PUT:                    return "PUT";                case REMOVE:                    return "REMOVE";                default:                    return "UNKNOWN";            }        }    }    public static void main(String[] args) {        ReplicatedTree tree=null;        HashMap m=new HashMap();        String props;        props="UDP(mcast_addr=224.0.0.36;mcast_port=55566;ip_ttl=32;" +                "mcast_send_buf_size=150000;mcast_recv_buf_size=80000):" +                "PING(timeout=2000;num_initial_members=3):" +                "MERGE2(min_interval=5000;max_interval=10000):" +                "FD_SOCK:" +                "VERIFY_SUSPECT(timeout=1500):" +                "pbcast.STABLE(desired_avg_gossip=20000):" +                "pbcast.NAKACK(gc_lag=50;retransmit_timeout=600,1200,2400,4800):" +                "UNICAST(timeout=5000):" +                "FRAG(frag_size=16000;down_thread=false;up_thread=false):" +                "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +                "shun=false;print_local_addr=true):" +                "pbcast.STATE_TRANSFER";        // "PERF(details=true)";        try {            tree=new ReplicatedTree(null, props, 10000);            // tree.setRemoteCalls(false);            tree.addReplicatedTreeListener(new MyListener());            tree.put("/a/b/c", null);            tree.put("/a/b/c1", null);            tree.put("/a/b/c2", null);            tree.put("/a/b1/chat", null);            tree.put("/a/b1/chat2", null);            tree.put("/a/b1/chat5", null);            System.out.println(tree);            m.put("name", "Bela Ban");            m.put("age", new Integer(36));            m.put("cube", "240-17");            tree.put("/a/b/c", m);            System.out.println("info for for \"/a/b/c\" is " + tree.print("/a/b/c"));            tree.put("/a/b/c", "age", new Integer(37));            System.out.println("info for for \"/a/b/c\" is " + tree.print("/a/b/c"));            tree.remove("/a/b");            System.out.println(tree);        }        catch(Exception ex) {            System.err.println(ex);        }    }    static class MyListener implements ReplicatedTreeListener {        public void nodeAdded(String fqn) {            System.out.println("** node added: " + fqn);        }        public void nodeRemoved(String fqn) {            System.out.println("** node removed: " + fqn);        }        public void nodeModified(String fqn) {            System.out.println("** node modified: " + fqn);        }        public void viewChange(View new_view) {            System.out.println("** view change: " + new_view);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?