distributedtree.java

来自「JGRoups源码」· Java 代码 · 共 739 行 · 第 1/2 页

JAVA
739 行
字号
        while(tok.hasMoreTokens()) {            child_name=tok.nextToken();            tmp_fqn=tmp_fqn + '/' + child_name;            n=curr.findChild(child_name);            if(n == null) {                n=new Node(child_name, null);                curr.addChild(n);                if(!tok.hasMoreTokens()) {                    n.element=element;                    notifyNodeAdded(tmp_fqn, element);                    return;                }                else                    notifyNodeAdded(tmp_fqn, null);            }            curr=n;        }        curr.element=element;        notifyNodeModified(fqn, null, element);    }    public void _remove(String fqn) {        Node curr, n;        StringTokenizer tok;        String child_name=null;        if(fqn == null || root == null)            return;        curr=root;        tok=new StringTokenizer(fqn, "/");        while(tok.countTokens() > 1) {            child_name=tok.nextToken();            n=curr.findChild(child_name);            if(n == null) // node does not exist                return;            curr=n;        }        try {            child_name=tok.nextToken();            if(child_name != null) {                n=curr.removeChild(child_name);                if(n != null)                    notifyNodeRemoved(fqn);            }        }        catch(Exception ex) {        }    }    public void _set(String fqn, Serializable element) {        Node n;        Serializable old_el=null;        if(fqn == null || element == null) return;        n=findNode(fqn);        if(n == null) {            if(log.isErrorEnabled()) log.error("node " + fqn + " not found");            return;        }        old_el=n.element;        n.element=element;        notifyNodeModified(fqn, old_el, element);    }    /** similar to set, but does not error if node does not exist, but rather does an add instead */    public void _reset(String fqn, Serializable element) {        Node n;        Serializable old_el=null;        if(fqn == null || element == null) return;        n=findNode(fqn);        if(n == null) {            _add(fqn, element);        }        old_el=n.element;        n.element=element;        notifyNodeModified(fqn, old_el, element);    }    /*----------------- End of  Callbacks ----------------------*/    /*-------------------- State Exchange ----------------------*/    public void receive(Message msg) {    }    /** Return a copy of the tree */    public byte[] getState() {        Object copy=root != null? root.copy() : null;        try {            return Util.objectToByteBuffer(copy);        }        catch(Throwable ex) {            if(log.isErrorEnabled()) log.error("exception marshalling state: " + ex);            return null;        }    }    public void setState(byte[] data) {        Object new_state;        try {            new_state=Util.objectFromByteBuffer(data);        }        catch(Throwable ex) {            if(log.isErrorEnabled()) log.error("exception unmarshalling state: " + ex);            return;        }        if(new_state == null) return;        if(!(new_state instanceof Node)) {            if(log.isErrorEnabled()) log.error("object is not of type 'Node'");            return;        }        root=((Node)new_state).copy();    }    /*------------------- Membership Changes ----------------------*/    public void viewAccepted(View new_view) {        Vector new_mbrs=new_view.getMembers();        if(new_mbrs != null) {            sendViewChangeNotifications(new_mbrs, members); // notifies observers (joined, left)            members.removeAllElements();            for(int i=0; i < new_mbrs.size(); i++)                members.addElement(new_mbrs.elementAt(i));        }		//if size is bigger than one, there are more peers in the group		//otherwise there is only one server.        if(members.size() > 1) {            send_message=true;        }        else {            send_message=false;        }    }    /** Called when a member is suspected */    public void suspect(Address suspected_mbr) {    }    /** Block sending and receiving of messages until ViewAccepted is called */    public void block() {    }    void sendViewChangeNotifications(Vector new_mbrs, Vector old_mbrs) {        Vector joined, left;        Object mbr;        if(view_listeners.size() == 0 || old_mbrs == null || new_mbrs == null)            return;        // 1. Compute set of members that joined: all that are in new_mbrs, but not in old_mbrs        joined=new Vector();        for(int i=0; i < new_mbrs.size(); i++) {            mbr=new_mbrs.elementAt(i);            if(!old_mbrs.contains(mbr))                joined.addElement(mbr);        }        // 2. Compute set of members that left: all that were in old_mbrs, but not in new_mbrs        left=new Vector();        for(int i=0; i < old_mbrs.size(); i++) {            mbr=old_mbrs.elementAt(i);            if(!new_mbrs.contains(mbr))                left.addElement(mbr);        }        notifyViewChange(joined, left);    }    Node findNode(String fqn) {        Node curr=root;        StringTokenizer tok;        String child_name;        if(fqn == null || root == null) return null;        if("/".equals(fqn) || "".equals(fqn))            return root;        tok=new StringTokenizer(fqn, "/");        while(tok.hasMoreTokens()) {            child_name=tok.nextToken();            curr=curr.findChild(child_name);            if(curr == null) return null;        }        return curr;    }    void notifyNodeAdded(String fqn, Serializable element) {        for(int i=0; i < listeners.size(); i++)            ((DistributedTreeListener)listeners.elementAt(i)).nodeAdded(fqn, element);    }    void notifyNodeRemoved(String fqn) {        for(int i=0; i < listeners.size(); i++)            ((DistributedTreeListener)listeners.elementAt(i)).nodeRemoved(fqn);    }    void notifyNodeModified(String fqn, Serializable old_element, Serializable new_element) {        for(int i=0; i < listeners.size(); i++)            ((DistributedTreeListener)listeners.elementAt(i)).nodeModified(fqn, old_element, new_element);    }    /** Generates NodeAdded notifications for all nodes of the tree. This is called whenever the tree is     initially retrieved (state transfer) */    void notifyAllNodesCreated(Node curr, String tmp_fqn) {        Node n;        if(curr == null) return;        if(curr.name == null) {            if(log.isErrorEnabled()) log.error("curr.name is null");            return;        }        if(curr.children != null) {            for(int i=0; i < curr.children.size(); i++) {                n=(Node)curr.children.elementAt(i);                System.out.println("*** nodeCreated(): tmp_fqn is " + tmp_fqn);                notifyNodeAdded(tmp_fqn, n.element);                notifyAllNodesCreated(n, tmp_fqn + '/' + n.name);            }        }    }    void notifyViewChange(Vector new_mbrs, Vector old_mbrs) {        for(int i=0; i < view_listeners.size(); i++)            ((ViewListener)view_listeners.elementAt(i)).viewChange(new_mbrs, old_mbrs);    }    private static class Node implements Serializable {        String name=null;        Vector children=null;        Serializable element=null;        Node() {        }        Node(String name, Serializable element) {            this.name=name;            this.element=element;        }        void addChild(String relative_name, Serializable element) {            if(relative_name == null)                return;            if(children == null)                children=new Vector();            else {                if(!children.contains(relative_name))                    children.addElement(new Node(relative_name, element));            }        }        void addChild(Node n) {            if(n == null) return;            if(children == null)                children=new Vector();            if(!children.contains(n))                children.addElement(n);        }        Node removeChild(String rel_name) {            Node n=findChild(rel_name);            if(n != null)                children.removeElement(n);            return n;        }        Node findChild(String relative_name) {            Node child;            if(children == null || relative_name == null)                return null;            for(int i=0; i < children.size(); i++) {                child=(Node)children.elementAt(i);                if(child.name == null) {                    if(log.isErrorEnabled()) log.error("child.name is null for " + relative_name);                    continue;                }                if(child.name.equals(relative_name))                    return child;            }            return null;        }        public boolean equals(Object other) {            return other != null && ((Node)other).name != null && name != null && name.equals(((Node)other).name);        }        Node copy() {            Node ret=new Node(name, element);            if(children != null)                ret.children=(Vector)children.clone();            return ret;        }        String print(int indent) {            StringBuffer sb=new StringBuffer();            boolean is_root=name != null && "/".equals(name);            for(int i=0; i < indent; i++)                sb.append(' ');            if(!is_root) {                if(name == null)                    sb.append("/<unnamed>");                else {                    sb.append('/' + name);                    // if(element != null) sb.append(" --> " + element);                }            }            sb.append('\n');            if(children != null) {                if(is_root)                    indent=0;                else                    indent+=4;                for(int i=0; i < children.size(); i++)                    sb.append(((Node)children.elementAt(i)).print(indent));            }            return sb.toString();        }        public String toString() {            if(element != null)                return "[name: " + name + ", element: " + element + ']';            else                return "[name: " + name + ']';        }    }}

⌨️ 快捷键说明

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