public <T extends Protocol> T getBottomProtocol() { T curr_prot=(T)this; while(curr_prot != null && curr_prot.getDownProtocol() !=null) curr_prot=curr_prot.getDownProtocol(); return curr_prot; }
/** Returns all protocols in a list, from top to bottom. <em>These are not copies of protocols, so modifications will affect the actual instances !</em> */ public List<Protocol> getProtocols() { List<Protocol> v=new ArrayList<>(15); Protocol p=top_prot; while(p != null) { v.add(p); p=p.getDownProtocol(); } return v; }
public <T extends Protocol> T findProtocol(Class<? extends Protocol> clazz) { Protocol tmp=top_prot; while(tmp != null) { Class<?> protClass=tmp.getClass(); if(clazz.isAssignableFrom(protClass)) return (T)tmp; tmp=tmp.getDownProtocol(); } return null; }
/** * Removes all events provided by the protocol below protocol from events * @param protocol * @param events */ protected static void removeProvidedUpServices(Protocol protocol, List<Integer> events) { if(protocol == null || events == null) return; for(Protocol prot=protocol.getDownProtocol(); prot != null && !events.isEmpty(); prot=prot.getDownProtocol()) { List<Integer> provided_up_services=prot.providedUpServices(); if(provided_up_services != null && !provided_up_services.isEmpty()) events.removeAll(provided_up_services); } }
/** Returns a given protocol or null if not found */ public <T extends Protocol> T findProtocol(String name) { T tmp=(T)top_prot; String prot_name; while(tmp != null) { prot_name=tmp.getName(); if(Objects.equals(prot_name, name)) return tmp; tmp=tmp.getDownProtocol(); } return null; }
public <T extends Protocol> List<T> findProtocols(String regexp) { List<T> retval=null; Pattern pattern=Pattern.compile(regexp); for(T prot=(T)top_prot; prot != null; prot=prot.getDownProtocol()) { String prot_name=prot.getName(); if(pattern.matcher(prot_name).matches()) { if(retval == null) retval=new ArrayList<>(); retval.add(prot); } } return retval; }
public void init() throws Exception { super.init(); // sanity check: we cannot have any discovery protocols _above_ us for(Protocol p=up_prot; p != null; p=p.getUpProtocol()) if(p instanceof Discovery) throw new IllegalStateException(String.format("found %s above %s: this is invalid; all discovery " + "protocols must be placed below %s", p.getClass().getSimpleName(), getClass().getSimpleName(), getClass().getSimpleName())); // add all discovery protocols below us to the discovery_protocols list for(Protocol p=down_prot; p != null; p=p.getDownProtocol()) if(p instanceof Discovery) discovery_protocols.add((Discovery)p); }
/** * @return Map<String,Map<key,val>> */ public Map<String,Object> dumpStats() { Map<String,Object> retval=new HashMap<>(); // no need to be sorted, we need order of protocols as in the config! for(Protocol p=top_prot; p != null; p=p.getDownProtocol()) { String prot_name=p.getName(); if(prot_name == null) continue; Map<String,Object> tmp=new TreeMap<>(); dumpStats(p, tmp, log); if(!tmp.isEmpty()) retval.put(prot_name, tmp); } return retval; }
/** * Adds a protocol at the tail of the protocol list * @param prot * @return * @since 2.11 */ public ProtocolStack addProtocol(Protocol prot) { if(prot == null) return this; prot.setProtocolStack(this); prot.setUpProtocol(this); if(bottom_prot == null) { top_prot=bottom_prot=prot; return this; } prot.setDownProtocol(top_prot); prot.getDownProtocol().setUpProtocol(prot); top_prot=prot; return this; }
/** * Replaces one protocol instance with another. Should be done before the stack is connected * @param existing_prot * @param new_prot */ public void replaceProtocol(Protocol existing_prot, Protocol new_prot) throws Exception { Protocol up_neighbor=existing_prot.getUpProtocol(), down_neighbor=existing_prot.getDownProtocol(); new_prot.setUpProtocol(existing_prot.getUpProtocol()); new_prot.setDownProtocol(existing_prot.getDownProtocol()); up_neighbor.setDownProtocol(new_prot); if(down_neighbor != null) down_neighbor.setUpProtocol(new_prot); existing_prot.setDownProtocol(null); existing_prot.setUpProtocol(null); existing_prot.stop(); existing_prot.destroy(); if(new_prot.getUpProtocol() == this) top_prot=new_prot; callAfterCreationHook(new_prot, afterCreationHook()); new_prot.init(); }
public void insertProtocolInStack(Protocol prot, Protocol neighbor, Position position) { // connect to the protocol layer below and above if(position == Position.BELOW) { prot.setUpProtocol(neighbor); Protocol below=neighbor.getDownProtocol(); prot.setDownProtocol(below); if(below != null) below.setUpProtocol(prot); neighbor.setDownProtocol(prot); } else { // ABOVE is default Protocol above=neighbor.getUpProtocol(); checkAndSwitchTop(neighbor, prot); prot.setUpProtocol(above); if(above != null) above.setDownProtocol(prot); prot.setDownProtocol(neighbor); neighbor.setUpProtocol(prot); } }
public <T extends Protocol> T removeProtocol(T prot) { if(prot == null) return null; Protocol above=prot.getUpProtocol(), below=prot.getDownProtocol(); checkAndSwitchTop(prot, below); if(above != null) above.setDownProtocol(below); if(below != null) below.setUpProtocol(above); prot.setUpProtocol(null); prot.setDownProtocol(null); try { prot.stop(); } catch(Throwable t) { log.error(Util.getMessage("FailedStopping") + prot.getName() + ": " + t); } try { prot.destroy(); } catch(Throwable t) { log.error(Util.getMessage("FailedDestroying") + prot.getName() + ": " + t); } return prot; }
public static Protocol getBottommostProtocol(Protocol prot_stack) { Protocol tmp=null, curr_prot=prot_stack; while(true) { if((tmp=curr_prot.getDownProtocol()) == null) break; curr_prot=tmp; } return curr_prot; }
public <T extends Protocol> T getBottomProtocol() { T curr_prot=(T)this; while(curr_prot != null && curr_prot.getDownProtocol() !=null) curr_prot=curr_prot.getDownProtocol(); return curr_prot; }
private final Protocol getBottomProtocol(Protocol top) { Protocol tmp; if(top == null) return null; tmp=top; while(tmp.getDownProtocol() != null) tmp=tmp.getDownProtocol(); return tmp; }
/** Returns all protocols in a list, from top to bottom. <em>These are not copies of protocols, so modifications will affect the actual instances !</em> */ public Vector<Protocol> getProtocols() { Protocol p; Vector<Protocol> v=new Vector<Protocol>(); p=top_prot; while(p != null) { v.addElement(p); p=p.getDownProtocol(); } return v; }
/** Returns a given protocol or null if not found */ public Protocol findProtocol(String name) { Protocol tmp=top_prot; String prot_name; while(tmp != null) { prot_name=tmp.getName(); if(prot_name != null && prot_name.equals(name)) return tmp; tmp=tmp.getDownProtocol(); } return null; }
public static void startProtocolStack(Protocol prot) throws Exception { while(prot != null) { prot.start(); prot=prot.getDownProtocol(); } }
public static void stopProtocolStack(Protocol prot) { while(prot != null) { prot.stop(); prot=prot.getDownProtocol(); } }
public static void destroyProtocolStack(Protocol start_prot) { while(start_prot != null) { start_prot.destroy(); start_prot=start_prot.getDownProtocol(); } }