📄 configurator.java
字号:
// $Id: Configurator.java,v 1.16 2006/01/14 14:00:42 belaban Exp $package org.jgroups.stack;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.jgroups.Event;import org.jgroups.util.Util;import java.util.Properties;import java.util.StringTokenizer;import java.util.Vector;/** * The task if this class is to setup and configure the protocol stack. A string describing * the desired setup, which is both the layering and the configuration of each layer, is * given to the configurator which creates and configures the protocol stack and returns * a reference to the top layer (Protocol).<p> * Future functionality will include the capability to dynamically modify the layering * of the protocol stack and the properties of each layer. * @author Bela Ban */public class Configurator { protected final Log log=LogFactory.getLog(getClass()); /** * The configuration string has a number of entries, separated by a ':' (colon). * Each entry consists of the name of the protocol, followed by an optional configuration * of that protocol. The configuration is enclosed in parentheses, and contains entries * which are name/value pairs connected with an assignment sign (=) and separated by * a semicolon. * <pre>UDP(in_port=5555;out_port=4445):FRAG(frag_size=1024)</pre><p> * The <em>first</em> entry defines the <em>bottommost</em> layer, the string is parsed * left to right and the protocol stack constructed bottom up. Example: the string * "UDP(in_port=5555):FRAG(frag_size=32000):DEBUG" results is the following stack:<pre> * * ----------------------- * | DEBUG | * |-----------------------| * | FRAG frag_size=32000 | * |-----------------------| * | UDP in_port=32000 | * ----------------------- * </pre> */ public Protocol setupProtocolStack(String configuration, ProtocolStack st) throws Exception { Protocol protocol_stack=null; Vector protocol_configs; Vector protocols; protocol_configs=parseConfigurations(configuration); protocols=createProtocols(protocol_configs, st); if(protocols == null) return null; protocol_stack=connectProtocols(protocols); return protocol_stack; } public void initProtocolStack(Protocol bottom_prot) throws Exception { while(bottom_prot != null) { bottom_prot.init(); bottom_prot=bottom_prot.getUpProtocol(); } } public void startProtocolStack(Protocol bottom_prot) { while(bottom_prot != null) { bottom_prot.startDownHandler(); bottom_prot.startUpHandler(); bottom_prot=bottom_prot.getUpProtocol(); } } public void stopProtocolStack(Protocol start_prot) { while(start_prot != null) { start_prot.stopInternal(); start_prot.destroy(); start_prot=start_prot.getDownProtocol(); } } public Protocol findProtocol(Protocol prot_stack, String name) { String s; Protocol curr_prot=prot_stack; while(true) { s=curr_prot.getName(); if(s == null) continue; if(s.equals(name)) return curr_prot; curr_prot=curr_prot.getDownProtocol(); if(curr_prot == null) break; } return null; } public 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; } /** * Creates a new protocol given the protocol specification. Initializes the properties and starts the * up and down handler threads. * @param prot_spec The specification of the protocol. Same convention as for specifying a protocol stack. * An exception will be thrown if the class cannot be created. Example: * <pre>"VERIFY_SUSPECT(timeout=1500)"</pre> Note that no colons (:) have to be * specified * @param stack The protocol stack * @return Protocol The newly created protocol * @exception Exception Will be thrown when the new protocol cannot be created */ public Protocol createProtocol(String prot_spec, ProtocolStack stack) throws Exception { ProtocolConfiguration config; Protocol prot; if(prot_spec == null) throw new Exception("Configurator.createProtocol(): prot_spec is null"); // parse the configuration for this protocol config=new ProtocolConfiguration(prot_spec); // create an instance of the protocol class and configure it prot=config.createLayer(stack); prot.init(); // start the handler threads (unless down_thread or up_thread are set to false) prot.startDownHandler(); prot.startUpHandler(); return prot; } /** * Inserts an already created (and initialized) protocol into the protocol list. Sets the links * to the protocols above and below correctly and adjusts the linked list of protocols accordingly. * @param prot The protocol to be inserted. Before insertion, a sanity check will ensure that none * of the existing protocols have the same name as the new protocol. * @param position Where to place the protocol with respect to the neighbor_prot (ABOVE, BELOW) * @param neighbor_prot The name of the neighbor protocol. An exception will be thrown if this name * is not found * @param stack The protocol stack * @exception Exception Will be thrown when the new protocol cannot be created, or inserted. */ public void insertProtocol(Protocol prot, int position, String neighbor_prot, ProtocolStack stack) throws Exception { if(neighbor_prot == null) throw new Exception("Configurator.insertProtocol(): neighbor_prot is null"); if(position != ProtocolStack.ABOVE && position != ProtocolStack.BELOW) throw new Exception("Configurator.insertProtocol(): position has to be ABOVE or BELOW"); // find the neighbors below and above // connect to the protocol layer below and above } /** * Removes a protocol from the stack. Stops the protocol and readjusts the linked lists of * protocols. * @param prot_name The name of the protocol. Since all protocol names in a stack have to be unique * (otherwise the stack won't be created), the name refers to just 1 protocol. * @exception Exception Thrown if the protocol cannot be stopped correctly. */ public void removeProtocol(String prot_name) throws Exception { } /* ------------------------------- Private Methods ------------------------------------- */ /** * Creates a protocol stack by iterating through the protocol list and connecting * adjacent layers. The list starts with the topmost layer and has the bottommost * layer at the tail. When all layers are connected the algorithms traverses the list * once more to call startInternal() on each layer. * @param protocol_list List of Protocol elements (from top to bottom) * @return Protocol stack */ private Protocol connectProtocols(Vector protocol_list) { Protocol current_layer=null, next_layer=null; for(int i=0; i < protocol_list.size(); i++) { current_layer=(Protocol)protocol_list.elementAt(i); if(i + 1 >= protocol_list.size()) break; next_layer=(Protocol)protocol_list.elementAt(i + 1); current_layer.setUpProtocol(next_layer); next_layer.setDownProtocol(current_layer); } return current_layer; } /** * Get a string of the form "P1(config_str1):P2:P3(config_str3)" and return * ProtocolConfigurations for it. That means, parse "P1(config_str1)", "P2" and * "P3(config_str3)" * @param config_str Configuration string * @return Vector of ProtocolConfigurations */ public Vector parseComponentStrings(String config_str, String delimiter) { Vector retval=new Vector(); StringTokenizer tok; String token; /*tok=new StringTokenizer(config_str, delimiter, false); while(tok.hasMoreTokens()) { token=tok.nextToken(); retval.addElement(token); }*/ // change suggested by gwoolsey tok=new StringTokenizer(config_str, delimiter, false); while(tok.hasMoreTokens()) { token=tok.nextToken(); while(token.endsWith("\\")) token=token.substring(0, token.length() - 1) + delimiter + tok.nextToken(); retval.addElement(token); } return retval; } /** * Return a number of ProtocolConfigurations in a vector * @param configuration protocol-stack configuration string * @return Vector of ProtocolConfigurations */ public Vector parseConfigurations(String configuration) throws Exception { Vector retval=new Vector(); Vector component_strings=parseComponentStrings(configuration, ":"); String component_string; ProtocolConfiguration protocol_config; if(component_strings == null) return null; for(int i=0; i < component_strings.size(); i++) { component_string=(String)component_strings.elementAt(i); protocol_config=new ProtocolConfiguration(component_string); retval.addElement(protocol_config); } return retval; } /** * Takes vector of ProtocolConfigurations, iterates through it, creates Protocol for * each ProtocolConfiguration and returns all Protocols in a vector. * @param protocol_configs Vector of ProtocolConfigurations * @param stack The protocol stack * @return Vector of Protocols */ private Vector createProtocols(Vector protocol_configs, ProtocolStack stack) throws Exception { Vector retval=new Vector(); ProtocolConfiguration protocol_config; Protocol layer; for(int i=0; i < protocol_configs.size(); i++) { protocol_config=(ProtocolConfiguration)protocol_configs.elementAt(i); layer=protocol_config.createLayer(stack); if(layer == null) return null; retval.addElement(layer); } sanityCheck(retval); return retval; } /** Throws an exception if sanity check fails. Possible sanity check is uniqueness of all protocol names. */ public void sanityCheck(Vector protocols) throws Exception { Vector names=new Vector(); Protocol prot; String name; ProtocolReq req; Vector req_list=new Vector(); int evt_type; // Checks for unique names for(int i=0; i < protocols.size(); i++) { prot=(Protocol)protocols.elementAt(i); name=prot.getName(); for(int j=0; j < names.size(); j++) { if(name.equals(names.elementAt(j))) { throw new Exception("Configurator.sanityCheck(): protocol name " + name + " has been used more than once; protocol names have to be unique !"); } } names.addElement(name); } // Checks whether all requirements of all layers are met for(int i=0; i < protocols.size(); i++) { prot=(Protocol)protocols.elementAt(i); req=new ProtocolReq(prot.getName()); req.up_reqs=prot.requiredUpServices(); req.down_reqs=prot.requiredDownServices(); req.up_provides=prot.providedUpServices(); req.down_provides=prot.providedDownServices(); req_list.addElement(req); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -