public static void main(String[] args) { ReplicatedTree tree=null; HashMap m=new HashMap(); String props=null; 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",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",37); System.out.println("info for for \"/a/b/c\" is " + tree.print("/a/b/c")); tree.remove("/a/b"); System.out.println(tree); tree.stop(); } catch(Exception ex) { System.err.println(ex); } }
/** Callback. Process the contents of the message; typically an _add() or _set() request */ public void receive(Message msg) { Request req=null; if(msg == null || msg.getLength() == 0) return; try { req=msg.getObject(); String fqn=req.fqn; switch(req.type) { case Request.PUT: if(req.key != null && req.value != null) _put(fqn, req.key, req.value); else _put(fqn, req.data); break; case Request.REMOVE: if(req.key != null) _remove(fqn, req.key); else _remove(fqn); break; default: if(log.isErrorEnabled()) log.error("type " + req.type + " unknown"); break; } } catch(Exception ex) { if(log.isErrorEnabled()) log.error("failed unmarshalling request: " + ex); } }
/** * Checks whether a given node exists in the tree * @param fqn The fully qualified name of the node * @return boolean Whether or not the node exists */ public boolean exists(String fqn) { return fqn != null && findNode(fqn) != null; }
/** 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 void _remove(String fqn) { Node n; StringHolder child_name=new StringHolder(); if(fqn == null) return; if(fqn.equals(SEPARATOR)) { root.removeAll(); notifyNodeRemoved(fqn); return; } n=findParentNode(fqn, child_name, false); if(n == null) return; n.removeChild(child_name.getValue(), fqn); notifyNodeRemoved(fqn); }
public void _put(String fqn, HashMap data) { Node n; StringHolder child_name=new StringHolder(); boolean child_exists=false; if(fqn == null) return; n=findParentNode(fqn, child_name, true); // create all nodes if they don't exist if(child_name.getValue() != null) { child_exists=n.childExists(child_name.getValue()); n.createChild(child_name.getValue(), fqn, n, data); } else { child_exists=true; n.setData(data); } if(child_exists) notifyNodeModified(fqn); else notifyNodeAdded(fqn); }
/** * Removes the node from the tree. * @param fqn The fully qualified name of the node. */ public void remove(String fqn) { if(!remote_calls) { _remove(fqn); return; } //Changes done by <aos> //if true, propagate action to the group if(send_message) { if(channel == null) { if(log.isErrorEnabled()) log.error("channel is null, cannot broadcast REMOVE request"); return; } try { channel.send(new Message(null, new Request(Request.REMOVE, fqn))); } catch(Exception ex) { if(log.isErrorEnabled()) log.error("failure bcasting REMOVE request: " + ex); } } else { _remove(fqn); } }
/** * Fetches all data from underlying tree model and display it graphically */ void init() { Vector mbrship=null; addGuiNode(SEP); mbrship=tree != null && tree.getMembers() != null ? (Vector)tree.getMembers().clone() : null; if(mbrship != null) { tree._put(SEP, "members", mbrship); tree._put(SEP, "coordinator", mbrship.firstElement()); } }
public void setState(InputStream istream) throws Exception { Object obj=Util.objectFromStream(new DataInputStream(istream)); root=(Node)((Node)obj).clone(); notifyAllNodesCreated(root); }
/** * Returns the node at fqn. This method should not be used by clients (therefore it is package-private): * it is only used internally (for navigation). C++ 'friend' would come in handy here... * @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) || fqn != null && fqn.isEmpty()) return root; if(n == null || child_name == null) return null; else return n.getChild(child_name); }
tree=new ReplicatedTree("ReplicatedTreeDemo-Group", props, 10000, jmx); new ReplicatedTreeView(tree, tree.getLocalAddress());
public ReplicatedTreeView(ReplicatedTree tree, Object title) throws Exception { this.tree=tree; tree.addReplicatedTreeListener(this);
public void _put(String fqn, String key, Object value) { Node n; StringHolder child_name=new StringHolder(); boolean child_exists=false; if(fqn == null || key == null || value == null) return; n=findParentNode(fqn, child_name, true); if(child_name.getValue() != null) { child_exists=n.childExists(child_name.getValue()); n.createChild(child_name.getValue(), fqn, n, key, value); } else { child_exists=true; n.setData(key, value); } if(child_exists) notifyNodeModified(fqn); else notifyNodeAdded(fqn); }
/** 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 void _remove(String fqn) { Node n; StringHolder child_name=new StringHolder(); if(fqn == null) return; if(fqn.equals(SEPARATOR)) { root.removeAll(); notifyNodeRemoved(fqn); return; } n=findParentNode(fqn, child_name, false); if(n == null) return; n.removeChild(child_name.getValue(), fqn); notifyNodeRemoved(fqn); }
/** * Removes {@code key} from the node's hashmap * @param fqn The fullly qualified name of the node * @param key The key to be removed */ public void remove(String fqn, String key) { if(!remote_calls) { _remove(fqn, key); return; } //Changes done by <aos> //if true, propagate action to the group if(send_message) { if(channel == null) { if(log.isErrorEnabled()) log.error("channel is null, cannot broadcast REMOVE request"); return; } try { channel.send(new Message(null, new Request(Request.REMOVE, fqn, key))); } catch(Exception ex) { if(log.isErrorEnabled()) log.error("failure bcasting REMOVE request: " + ex); } } else { _remove(fqn, key); } }
public void setState(InputStream istream) throws Exception { Object obj=Util.objectFromStream(new DataInputStream(istream)); root=(Node)((Node)obj).clone(); notifyAllNodesCreated(root); }
/** * Returns the node at fqn. This method should not be used by clients (therefore it is package-private): * it is only used internally (for navigation). C++ 'friend' would come in handy here... * @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); }